使用双向链表的 Queue 中的 Remove() 函数
Remove() function in Queue using a doubly linked list
我已经编写了下面的程序,总的来说它似乎工作正常...除了 remove() 函数不断删除队列中的所有值,甚至没有被调用...
#include <iostream>
#include <string>
using namespace std;
//Create a node struct
struct Node {
int data;
Node *next;
Node *prev;
};
class Queue {
private:
Node *head;
Node *tail;
int size;
public:
Queue();
~Queue();
void add(int d);
int remove();
bool isEmpty();
void printQueue(bool o);
};
//set to NULL
Queue::Queue() {
head = tail = NULL;
size = 0;
}
//destructor
//call remove until empty
Queue::~Queue() {
while (!isEmpty())
remove();
}
//adds a node with the given data at the back of the queue
void Queue::add(int d) {
Node *temp = new Node;
temp->data = d;
temp->next = NULL;
temp->prev = tail;
if (isEmpty()) {
//add to head
head = temp;
tail = temp;
cout << "Added: " << tail->data << endl;
} else {
//append
tail->next = temp;
tail = temp;
cout << "Added: " << tail->data << endl;
}
size++;
}
//removes the node at the head of the queue and returns its data
int Queue::remove() {
//TODO DOESNT WORK PROPERLY
if (isEmpty()) {
tail = NULL;
cout << "The queue is empty." << endl;
return 0;
} else {
Node *temp = head;
int value = head->data;
cout << "Removed: " << head->data << endl;
//moves pointer to next node
head = head->next;
if (head)
head->prev = NULL;
size--;
delete temp;
return value;
}
}
//determines if the queue is empty
bool Queue::isEmpty() {
return (size == 0);
}
//prints the contents of the queue from front to back, or front
//to back, depending on the value of the parameter
void Queue::printQueue(bool o) {
if (isEmpty()) {
cout << "The queue is empty." << endl;
} else {
Node *p;
if (o == true) {
p = head;
cout << "Printing front to back:" << endl;
//print front to back
while(p != NULL) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
} else if (o == false) {
p = tail;
cout << "Printing back to front:" << endl;
//print back to front
while (p != NULL) {
cout << p->data << " ";
p = p->prev;
}
cout << endl;
}
}
}
int main() {
Queue q;
q.add(9);
q.add(10);
q.add(11);
q.add(12);
q.add(13);
q.add(14);
q.add(15);
q.add(16);
q.printQueue(true);
q.printQueue(false);
return 0;
}
大体上,甚至没有使用 remove(),但输出是
Added: 9
Added: 10
Added: 11
Added: 12
Added: 13
Added: 14
Added: 15
Added: 16
Printing front to back:
9 10 11 12 13 14 15 16
Printing back to front:
16 15 14 13 12 11 10 9
Removed: 9
Removed: 10
Removed: 11
Removed: 12
Removed: 13
Removed: 14
Removed: 15
Removed: 16
为什么这种情况一直发生?我能做些什么来修复它?请帮忙:(
提前致谢!
问题实际上可能不在代码中,乍一看它表现正常。
您将 Queue q;
声明为 main 中的局部变量。这意味着当 main 完成它的执行时,q 超出范围,并且调用析构函数。
希望这对此事有所启发....
我已经编写了下面的程序,总的来说它似乎工作正常...除了 remove() 函数不断删除队列中的所有值,甚至没有被调用...
#include <iostream>
#include <string>
using namespace std;
//Create a node struct
struct Node {
int data;
Node *next;
Node *prev;
};
class Queue {
private:
Node *head;
Node *tail;
int size;
public:
Queue();
~Queue();
void add(int d);
int remove();
bool isEmpty();
void printQueue(bool o);
};
//set to NULL
Queue::Queue() {
head = tail = NULL;
size = 0;
}
//destructor
//call remove until empty
Queue::~Queue() {
while (!isEmpty())
remove();
}
//adds a node with the given data at the back of the queue
void Queue::add(int d) {
Node *temp = new Node;
temp->data = d;
temp->next = NULL;
temp->prev = tail;
if (isEmpty()) {
//add to head
head = temp;
tail = temp;
cout << "Added: " << tail->data << endl;
} else {
//append
tail->next = temp;
tail = temp;
cout << "Added: " << tail->data << endl;
}
size++;
}
//removes the node at the head of the queue and returns its data
int Queue::remove() {
//TODO DOESNT WORK PROPERLY
if (isEmpty()) {
tail = NULL;
cout << "The queue is empty." << endl;
return 0;
} else {
Node *temp = head;
int value = head->data;
cout << "Removed: " << head->data << endl;
//moves pointer to next node
head = head->next;
if (head)
head->prev = NULL;
size--;
delete temp;
return value;
}
}
//determines if the queue is empty
bool Queue::isEmpty() {
return (size == 0);
}
//prints the contents of the queue from front to back, or front
//to back, depending on the value of the parameter
void Queue::printQueue(bool o) {
if (isEmpty()) {
cout << "The queue is empty." << endl;
} else {
Node *p;
if (o == true) {
p = head;
cout << "Printing front to back:" << endl;
//print front to back
while(p != NULL) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
} else if (o == false) {
p = tail;
cout << "Printing back to front:" << endl;
//print back to front
while (p != NULL) {
cout << p->data << " ";
p = p->prev;
}
cout << endl;
}
}
}
int main() {
Queue q;
q.add(9);
q.add(10);
q.add(11);
q.add(12);
q.add(13);
q.add(14);
q.add(15);
q.add(16);
q.printQueue(true);
q.printQueue(false);
return 0;
}
大体上,甚至没有使用 remove(),但输出是
Added: 9
Added: 10
Added: 11
Added: 12
Added: 13
Added: 14
Added: 15
Added: 16
Printing front to back:
9 10 11 12 13 14 15 16
Printing back to front:
16 15 14 13 12 11 10 9
Removed: 9
Removed: 10
Removed: 11
Removed: 12
Removed: 13
Removed: 14
Removed: 15
Removed: 16
为什么这种情况一直发生?我能做些什么来修复它?请帮忙:(
提前致谢!
问题实际上可能不在代码中,乍一看它表现正常。
您将 Queue q;
声明为 main 中的局部变量。这意味着当 main 完成它的执行时,q 超出范围,并且调用析构函数。
希望这对此事有所启发....