Schreiben Sie die Definition der Funktion moveNthFront, die als Parameter eine positive Ganzzahl, n. Die Funktion verschiebt das n-te Element der Warteschlange nach vorne. Die Reihenfolge der übrigen Elemente bleibt unverändert. Beispiel: Angenommen:Programm bricht ab
queue = {5, 11, 34, 67, 43, 55} und n = 3.
Nach einem Aufruf der Funktion moveNthFront:
queue = {34, 5, 11, 67, 43, 55}.
Fügen Sie diese Funktion der Klasse queueType hinzu. Schreiben Sie auch ein Programm, um Ihre Methode zu testen.
Hier ist mein Kopf
#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);
Hier ist die CPP für den Header.
#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);
}
}
Hier ist meine Haupt
#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;
}
ich versuche, Insert-Funktion von Position wie dieser Hohlraumeinsatz (int x, int pos) zu verwenden; aber es bricht, wenn ich 0 für Pos –
setzen Welchen Fehler bekommen Sie? – Jay
Time Check Failure –