写出函数moveNthFront的定义,该函数将参数作为正整数n。该函数将队列的第n个元素移动到前面。其余元素的顺序保持不变。例如,假设:
queue = {5,11,34,67,43,55}和n = 3.
调用函数moveNthFront之后:
queue = {34,5,11,67,43,55}。
将此函数添加到类queueType中。另外,编写一个程序来测试你的方法。
这是我的标题
#ifndef queueType_H
#define queueType_H
#include<iostream>
class queueType
{
private:
class Queue
{
friend class queueType;
int value;
Queue *next;
Queue(int value1, Queue *next1 = NULL)
{
value = value1;
next = next1;
}
};
// These track the front and rear of the queue
Queue *front;
Queue *rear;
Queue *head;
public:
void moveNthFront(int);
这是标题的cpp。
#include"queueType.h"
#include<iostream>
using namespace std;
queueType::queueType() {
front = NULL;
rear = NULL;
}
queueType::~queueType() {
clear();
}
void queueType::enqueue(int num)
{
if (isEmpty())
{
front = new Queue(num);
rear = front;
}
else
{
rear->next = new Queue(num);
rear = rear->next;
}
}
void queueType::dequeue(int &num)
{
Queue *temp;
if (isEmpty())
{
cout << "The queue is empty.\n";
exit(1);
}
else
{
num = front->value;
temp = front;
front = front->next;
delete temp;
}
}
bool queueType::isEmpty() const
{
if (front == NULL)
return true;
else
return false;
}
int queueType::search(int x)
{
if (front == NULL)
return -1;
else
{
int count = 0;
Queue *aptr = front;
while (aptr != NULL)
{
if (aptr->value == x)
return count;
aptr = aptr->next;
count++;
}
return -1;
}
}
void queueType::clear()
{
int value; // Dummy variable for dequeue
while (!isEmpty())
dequeue(value);
}
void queueType::remove(int pos)
{
if (front == NULL)
return;
else if (pos == 0)
front = front->next;
else
{
int count = 0;
Queue *now = front, *past;
while (now != NULL && count != pos)
{
past = now;
now = now->next;
count++;
}
if (now)
{
past->next = now->next;
delete now;
}
}
}
void queueType::insert(int x, int pos )
{
Queue *now, *past;
if (front == NULL)
front = new Queue(x);
else if (now != NULL && pos == 0)
{
now = front;
;
for (int i = 0; i < 5; i++)
{
past = now;
now = now->next;
}
past->next = new Queue(x, now);
}
else
{
now = front;
int count = 0;
while (now != NULL && count != pos)
{
past = now;
now = now->next;
count++;
}
past->next = new Queue(x, now);
}
}
这是我的主要
#include"queueType.h"
#include<iostream>
using namespace std;
int main() {
queueType intqueue;
int input, temp, x = 0;
for (int i = 0; i < 5; i++) {
intqueue.enqueue(i*i);
}
cout << "The values in the queue were:\n";
while (!intqueue.isEmpty())
{
int value;
intqueue.dequeue(value);
cout << value << " ";
}
for (int i = 0; i < 5; i++) {
intqueue.enqueue(i*i);
}
cout << "\nEnter the value to find:" << endl;
cin >> input;
intqueue.search(input);
temp = intqueue.search(input);
intqueue.remove(temp);
intqueue.insert(input, 0);
cout << "\nThe values after change was made:\n";
while (!intqueue.isEmpty())
{
int value;
intqueue.dequeue(value);
cout << value << " ";
}
return 0;
}
答案 0 :(得分:1)
在插入方法中
else if (now != NULL && pos == 0)
{
这部分永远不会运行,因为现在它是null,因为它从未被初始化。
然后控制转到其他部分
其中如果pos = 0 *过去从未初始化。
现在首先初始化*。
否则你可以删除其他部分,只是在while循环之外初始化*过去,即
else
{
now = front;
int count = 0;//here initialize past once past=now;
while (now != NULL && count != pos)
{
past = now;
now = now->next;
count++;
}
past->next = new Queue(x, now);
}
答案 1 :(得分:0)
我编辑了它。它适用于除0 pos之外的所有位置。 我需要元素转到队列的前面。
<div class="icon"></div>
.icon:after {
content: "";
display: inline-block;
background-color: gold;
background-image: url(http://orig00.deviantart.net/1110/f/2014/143/9/b/mega_man_hd_sprites__transparent_background__by_lunodevan-d7jgruq.png);
background-position: -129px -40px;
height: 100px;
width: 100px;
}
.icon {
margin: auto;
margin-top: 100px;
position: relative;
width: 100px;
}
.icon:before {
content: "";
display: inline-block;
background: gold;
position: absolute;
top: -50px;
left: -50px;
width: 200px;
height: 200px;
z-index: -1;
border-radius: 100%;
}
}