#include <iostream>
#include "CLQueue.h"
struct LNode {
int item;
LNode* next;
};
CLQueue::CLQueue(){
QRear = NULL;
}
CLQueue::~CLQueue() {
makeEmpty();
}
void CLQueue::Enqueue(int newitem) {
LNode* newNode;
if (QRear == NULL) {
newNode = new LNode;
newNode->item = newitem;
QRear = newNode;
newNode->next = newNode;
}
else {
newNode = new LNode;
newNode->item = newitem;
newNode->next = QRear->next;
QRear->next = newNode;
}
}
void CLQueue::Dequeue(int deleteItem) {
}
bool CLQueue::isEmpty(){
if (QRear == NULL) {
return true;
}
else {
return false;
}
}
void CLQueue::makeEmpty() {
LNode* tempPtr;
if (QRear == NULL) { //If queue is empty already
std::cout << "The queue is empty.";
}
while (QRear != NULL) {
tempPtr = QRear->next;
QRear = tempPtr->next;
delete tempPtr;
}
}
void CLQueue::printList() {
LNode* printPtr;
std::cout << "(";
if (QRear == NULL) {
std::cout << "This queue is empty.";
}
else {
printPtr = QRear->next;
std::cout << printPtr->item;
printPtr = printPtr->next;
while (printPtr != QRear->next) {
std::cout << ", " << printPtr->item;
printPtr = printPtr->next;
}
}
std::cout << ")" << std::endl;
}
I get a breakpoint error with "delete tempPtr" when trying to run makeEmpty() in my driver file. I'm trying to make a circular queue with a single pointer for a class and it is, of course, still a work in progress. I'm still hazy on how to handle deleting nodes in linked lists in the first place.
Thanks!
EDIT: This is the error I'm getting:
The thread 0x6a38 has exited with code 0 (0x0).
HEAP[DSHW2.exe]: Invalid address specified to RtlValidateHeap( 00C30000, 00C3FC48 )
DSHW2.exe has triggered a breakpoint.
while (QRear != NULL) {
tempPtr = QRear->next;
QRear = tempPtr->next;
You have no certitude that QRear->next
aka tempPtr
isn't null. This makes tempPtr->next
UB.
You'd want
while (QRear != NULL) {
tempPtr = QRear;
QRear = QRear->next;
delete tempPtr;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.