Question: I need help fixing the bug in line 122. (using c++) Code: #include using namespace std; class Node { public: int data; Node* next; };
I need help fixing the bug in line 122. (using c++) Code:
#include
class Node { public: int data; Node* next; };
class List { private: Node * tailNode; Node *headNode; bool isEmpty() { return (headNode == NULL); }
public: List() { headNode = NULL; tailNode = NULL; }
//Copy Constructor List(List &newList) { if (newList.headNode == NULL || newList.tailNode == NULL) { headNode = NULL; tailNode = NULL; } else { Node *temp = new Node; while (newList.headNode != NULL) { temp->data = newList.headNode->data; temp = newList.headNode->next; } headNode = temp; delete temp; temp = new Node; while (newList.tailNode != NULL) { temp->data = newList.tailNode->data; temp = newList.tailNode->next; } tailNode = temp; delete temp; } }
//desturctor ~List() { Node *current = headNode; while (current != NULL) { Node *temp = current; current = current->next; delete temp; } delete this->headNode; delete this->tailNode; } //========================== // add at frist //========================== void newNodeAtFirst(int value) { Node *temp = new Node; temp->data = value; if (isEmpty()) { temp->next = NULL; tailNode = temp; } else temp->next = headNode; headNode = temp; }
//========================== // add from back //========================== void newNodeAtEnd(int value) { Node *temp = new Node; temp->data = value; temp->next = NULL; if (isEmpty()) { headNode = temp; tailNode = temp; } else { tailNode->next = temp; tailNode = temp; } }
//========================== // Delete from first //========================== void deleteAtFirst(int value) { Node *temp = headNode; Node *prevNode; while (temp->next != NULL && temp->data != value) { prevNode = temp; temp = temp->next; } if (temp->data == value) { prevNode->next = temp->next; delete temp; } else if (temp->next == NULL) { cout << "[Error:]" << value << "Not found..." << endl; } }
//========================== // Delete from back //========================== void deleteAtBack(int value) { Node *temp = tailNode; Node *prevNode; while (temp->next != NULL && temp->data != value) { prevNode = temp; temp = temp->next; } if (temp->data == value) { prevNode->next = temp->next; delete temp; } else if (temp->next == NULL) { cout << "[Error:]" << value << "Not found..." << endl; } } //========================== //Search Node //========================== void searchNode(int value) { int idx = 0; bool isFound = false; Node *temp = headNode; while (temp != NULL) { if (temp->data == value) { cout << "Found at position " << idx << endl; isFound = true; break; } idx++; temp = temp->next; } if (!isFound) { cout << value << " not found in list" << endl; } }
//========================== //sorting //========================== void sortedInsert(Node* new_node) { Node* current; if (headNode == NULL || (headNode)->data >= new_node->data) { new_node->next = headNode; headNode = new_node; } else { current = headNode; while (current->next != NULL && current->next->data < new_node->data) { current = current->next; } new_node->next = current->next; current->next = new_node; } }
void printList() { Node *temp = headNode; while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } } };
int main() { List l; cout << "Insert Operations : " << endl;
cout<<"1. 1 at end of list"< cout<<"List after insertion operations : "; l.printList(); cout < cout<<"Search elements in list if present return their postion : "< cout<<"List after deletion operation : "; l.printList(); cout << endl< cout << "List after Sorting Insert Operation: "; Node *t = new Node; t->data = 0; l.sortedInsert(t); l.printList(); cout << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
