LinkedList/Stack/Queue - 帮助出队
我必须编写一个链表,然后将其转换为动态堆栈,然后将其转换为动态队列。好吧,除了“出队”之外,一切似乎都正常,就在程序即将完成时,它给了我一个错误:“LinkedList_Stack_BNS11.exe [4972] 中发生了未处理的 win32 异常。”。
我只是假设这是出列,因为当我单步执行和/或运行程序时,它会顺利运行到该部分,所以也许我发送了一个错误的指针或其他什么?
输出:
查询 5 项... // Finsihes
队列中的值是(出队):
0
1
2
// 队列中的数字正确,但是...
// 程序在此处给出该错误。当它应该完成并关闭时。
如果我包含太多代码,请告诉我,我会将其缩减为仅“出队”(位于下面所有内容的中间),
提前感谢您的帮助!我只是没有看到我做错了什么。认为这可能与“头”指向的地方有关?我不知道。
头文件:
class NumberList
{
private:
//
struct ListNode
{
int value; // Value in this node
struct ListNode *next; // Pointer to the next node
};
ListNode *head; // List head pointer
ListNode *rear;
public:
//Constructor
NumberList()
{ head = NULL; rear = NULL; }
//Destructor
~NumberList();
//Stack operations
bool isEmpty();
//Queue operations
void enqueue(int);
void dequeue(int &);
};
#endif
List_Stack_Queue.cpp:
bool NumberList::isEmpty()
{
bool status;
if(!head)
status = true;
else
status = false;
return status;
}
void NumberList::enqueue(int num)
{
ListNode *newNode; // Point to a new node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
//If there are no nodes in the list
// make newNode the first node.
if(isEmpty())
{
head = newNode;
rear = head;
//newNode->next = NULL;
}
else
{
rear->next = newNode;
rear = rear->next;
//newNode->next = head;
//head = newNode;
}
}
void NumberList::dequeue(int &num)
{
ListNode *temp;
if(isEmpty())
cout << "The queue is empty.\n";
else
{
num = head->value;
temp = head;
head = head->next;
delete temp;
}
}
主文件:
const int MAX_VALUES = 3;
// Create a DynIntQueue object.
NumberList iQueue;
// Enqueue a series of numbers.
cout << "Enqueuing " << MAX_VALUES << " items...\n";
for (int x = 0; x < MAX_VALUES; x++)
iQueue.enqueue(x);
cout << endl;
//Dequeue and retrieve all numbers in the queue
cout << "The values in the queue were (Dequeuing):\n";
while(!iQueue.isEmpty())
{
int value;
iQueue.dequeue(value);
cout << value << endl;
}
return 0;
I had to write a linked list, then turn it into a dynamic Stack, then turn that into a dynamic Queue. Well everything seems to work except the "dequeuing", right as the programs about to finish, it gives me an error: "An unhandled win32 exception occured in LinkedList_Stack_BNS11.exe [4972].".
I'm only assuming it's the dequeuing, because as I step through and/or run the program, it runs smoothly up till that part, so maybe I sent one of the pointers wrong or something?
Output:
Enquing 5 items....
// Finsihes
The values in the queue were (Dequeuing):
0
1
2
// Correct number in que but...
//Program gives that error right here. When it should finish and close.
If I included too much code let me know and I'll chop it down to just the "Dequeuing" (Which is in the very middle of all the stuff below)
Thanks in advance for the help!! I'm just not seeing what I did wrong. Thinking it maybe has something to do with where "head" is pointing? Idk.
Header File:
class NumberList
{
private:
//
struct ListNode
{
int value; // Value in this node
struct ListNode *next; // Pointer to the next node
};
ListNode *head; // List head pointer
ListNode *rear;
public:
//Constructor
NumberList()
{ head = NULL; rear = NULL; }
//Destructor
~NumberList();
//Stack operations
bool isEmpty();
//Queue operations
void enqueue(int);
void dequeue(int &);
};
#endif
List_Stack_Queue.cpp:
bool NumberList::isEmpty()
{
bool status;
if(!head)
status = true;
else
status = false;
return status;
}
void NumberList::enqueue(int num)
{
ListNode *newNode; // Point to a new node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
//If there are no nodes in the list
// make newNode the first node.
if(isEmpty())
{
head = newNode;
rear = head;
//newNode->next = NULL;
}
else
{
rear->next = newNode;
rear = rear->next;
//newNode->next = head;
//head = newNode;
}
}
void NumberList::dequeue(int &num)
{
ListNode *temp;
if(isEmpty())
cout << "The queue is empty.\n";
else
{
num = head->value;
temp = head;
head = head->next;
delete temp;
}
}
MAIN:
const int MAX_VALUES = 3;
// Create a DynIntQueue object.
NumberList iQueue;
// Enqueue a series of numbers.
cout << "Enqueuing " << MAX_VALUES << " items...\n";
for (int x = 0; x < MAX_VALUES; x++)
iQueue.enqueue(x);
cout << endl;
//Dequeue and retrieve all numbers in the queue
cout << "The values in the queue were (Dequeuing):\n";
while(!iQueue.isEmpty())
{
int value;
iQueue.dequeue(value);
cout << value << endl;
}
return 0;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
链表中最后一个节点的下一个元素应设置为NULL。所以,
对我来说,
Numberlist::isEmpty();
成员函数似乎有些问题。您如何确定列表是否为空?显示它的定义。Last nodes's next element should be set to NULL in a linked list. So,
To, me it seems some thing is wrong with
Numberlist::isEmpty();
member function. How you are deciding whether the list is empty or not ? Show the definition of it.