Question: I am working on a linked list project. I can insert, append, and delete nodes. I'm working on the destructor that destroys the list and
I am working on a linked list project. I can insert, append, and delete nodes. I'm working on the destructor that destroys the list and how to use it properly. Whenever I run it in the program, and then try to to add a node by appending or inserting, the program runs forever and I'm not sure why.
Also, I need to overload the [] operator based on the following and I am having a very hard time with that. This will give the linked list the ability to access nodes using a subscript, like an array. The subscript 0 will reference the first node in the list, the subscript 1 will reference the second node in the list, and so forth. The subscript of the last node will be the number of nodes minus 1. If an invalid subscript is used, the function should throw an exception. I have copied and pasted my code below. Please feel free to make any corrections or suggestions. As of now, the code compiles and runs.
Int_List.h
#ifndef INT_LIST_H #define INT_LIST_H #define CLEAR_SCREEN cout << "\033[2J\033[1;1h" #include
//declare a structure for the linked list
struct ListNode { int value; struct ListNode *next; };
struct ListNode *head; public:
//constructur Int_List() { head = NULL; } //destructuor ~Int_List(); //Linked list operations
void appendNode(int); void insertNode(int); void deleteNode(int); void display() const; };
#endif
Int_List.cpp
#include "Int_List.h" #include
void Int_List::appendNode(int num) { ListNode *newNode; ListNode *nodePtr;
//alocate a nre node newNode = new ListNode;
//store in num memory newNode->value = num; newNode->next = NULL;
//If there are no nodes in the list make newNode the first node, otherwise, insert newNode at end if (!head ) head = newNode;
else { nodePtr = head;
while (nodePtr->next) nodePtr = nodePtr->next; nodePtr->next = newNode; } display(); }
//Method defivition of insertNode to find new nodes propert position in the list and inserting it
void Int_List::insertNode( int num) { ListNode *newNode; ListNode *nodePtr; ListNode *previousNode = NULL;
// allocate a new node and store data in the num mode
newNode = new ListNode; newNode->value = num; newNode->next = NULL;
//If there are no nodes in the list make newNode the first node, otherwise, insert newNode
if (!head) head = newNode; else { //Position nodePtr at the head of list nodePtr = head; //initialize previousNode to Null previousNode = NULL;
//Find the first node whose value is less than num variable to the new value, or the end // of the list then insert the new node before the //found node, or at the end of the list
while (nodePtr != NULL && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; }
if (previousNode == NULL) { head = newNode; newNode->next = nodePtr; } else { previousNode->next = newNode; newNode->next = nodePtr; } } display(); }
//Method definition of deleteNode to search for a node with num as its value. The node, if found is deleted //from the list and from memory
void Int_List::deleteNode(int num) {
ListNode *nodePtr; ListNode *previousNode;
if (!head) { cout << "Failed to delete as list is empty."; return; } if (head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } else { nodePtr = head;
while (nodePtr != NULL && nodePtr->value != num) { previousNode = nodePtr; nodePtr = nodePtr->next; } if ( nodePtr) { previousNode->next = nodePtr->next; delete nodePtr; } else cout << "Failed to delete as " << num << " could not be found in the list. " << endl; }
display(); }
//method definition of destructor of LinkedList to delete every node in the list
Int_List::~Int_List() { ListNode *nodePtr = head; //while nodePtr is not at the end of the list
while (nodePtr != NULL) { //garbage keeps track of node to delete ListNode *garbage = nodePtr; // move to next node nodePtr = nodePtr->next; //delete node delete garbage; } }
void Int_List::display() const { ListNode *nodePtr;
nodePtr = head; while (nodePtr) { cout << nodePtr->value << endl; nodePtr = nodePtr->next; } }
Linked_List_Demo.cpp
#include "Int_List.cpp"
using namespace std;
int main() {
//declare variables
char choice, ans; int n; CLEAR_SCREEN; Int_List myList; cout << "********************************************************" << endl; cout << "A program to demonstrate a Linked List Class: " << endl; cout << "********************************************************" << endl << endl; do { cout << "A. Append a Node. " << endl; cout << "B. Insert a Node. " << endl; cout << "C. Delete a Node. " << endl; cout << "D. Delete Entire List. " << endl; cout << "E. Exit. " << endl; cout << "*************************************************************" << endl << endl;
cout << "Please enter your choice: "; cin >> choice; cout << endl; CLEAR_SCREEN; switch ( choice ) { case 'A': case 'a': cout << "Enter an integer: "; cin >> n;
//Call the method to append the number into list
myList.appendNode (n); break; case 'B': case 'b': cout << "Enter an integer: "; cin >> n;
// call the method to insert the number into list
myList.insertNode (n); break; case 'C': case 'c': cout << "Enter an integer: "; cin >> n;
//call the method to delete the number from list myList.deleteNode (n); break; case 'D': case 'd': cout << "Are you sure you want to delete entire list? (Y/N) "; cin >> ans; ans = toupper (ans); if ( ans = 'Y') { myList.~Int_List(); break; } else break; case 'E': case 'e': return 0; default: cout << "You have entered an incorrect choice. Please try again. " << endl; break; } } while (choice != 'E' && choice != 'e'); return 0; }
Thank you in advance for any help.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
