Question: Object oriented design C++ linked list program help Im writing a program that uses classes to append a node, add an index location, delete a
Object oriented design C++ linked list program help
Im writing a program that uses classes to append a node, add an index location, delete a location, and print. Im getting some strange errors(which I have marked) and could really just use someones advice on if my code makes sense. The entire goal of this project is to get this program to print this with the white spaces.
0 1 2 0 2 0 3 2 Index out of bounds List Empty
Here is my code, any help that leads to me finishing this project will recieve immediate positive feedback, and regardless of your answer, I will contact you before leaving anything negative. Thanks in advance
#include
using namespace std;
class node { private: int data; node* nextNode;
public: node(int d = 0, node* next = NULL) {data = d; nextNode = next;} void setData(int d) {data = d;} void setNext(node* next) {nextNode = next;} int getData() {return data;} node* getNext() {return nextNode;} };
class linkedList { private: node* head;
public: linkedList() {head = NULL;} ~linkedList(); void appendNode(int d); void addNodeIndex(int d, int index); void deleteNodeIndex(int index); void printList(); };
linkedList::~linkedList() { node* tmp = new node; node* cur = new node; tmp = head; if(tmp != NULL) { while(tmp!= NULL) { cur = tmp->next;//ERROR "CLASS NODE" HAS NO MEMBER NAMED NEXT delete(tmp); tmp = cur; } }head = tmp; }
linkedList::void appendNode(int d)//ERROR EXPECTED UNQUALIFIED-ID BEFORE VOID { node* newNode = new node(d); node* tmp; tmp = head; if(head == NULL) { head = newNode; } else { node* tmpNode = head; while(tmpNode->getNext() != NULL) { tmpNode = tmpNode->getNext(); } tmpNode->setNext(newNode); }
} linkedList::void deleteNodeIndex(int index) { int cindex = 0; node* tmp = head; node* cur = head; if(head == NULL) { cout << "List Empty /n"; } else { for(cindex = 0; cindex < index-1; cindex++) { tmp = tmp->next; if(tmp == NULL) { cout << "Index out of bounds/n"; break; } if(cindex == index-1) { while(cur->next != tmp) { cur = cur->next; } if(tmp->next == NULL) { cur->next = NULL; } else { cur->next = tmp->next; } delete(tmp); }
} } } linkedList::void addNodeIndex(int d, int index) { int cindex = 0; node* myNode = new node(d); node* tmp = head;
if(head == NULL) { head = myNode; } else { for(cindex = 0; cindex < index-1; cindex++) { tmp = tmp->next; if(tmp == NULL) { cout << "Index out of bounds /n"; break; } } if(cindex == index-1) { if(tmp->getNext() == NULL) { tmp->setNext(myNode); } else { myNode->setNext(tmp->getNext()); tmp->setNext(myNode); } } } }
linkedList:: void printList() { node* tmp = head; if(tmp == NULL) { cout << "List Empty" } else { while (tmp != NULL) { cout << tmp->val << " /n"; tmp = tmp->next; } } cout << "/n";
} int main() { linkedList LL;
LL.addNodeIndex(1); LL.addNodeIndex(2); LL.addNodeIndex(3);
LL.appendNode(0); LL.appendNode(1); LL.appendNode(2);
LL.printList();
LL.deleteNodeIndex(2);
LL.printList();
LL.deleteNodeIndex(2);
LL.addNodeIndex(2); LL.appendNode(3); LL.addNodeIndex(3); LL.appendNode(2); LL.printList();
LL.addNodeIndex(9); LL.deleteNodeIndex(3); LL.deleteNodeIndex(2); LL.deleteNodeIndex(1); LL.printList();
return 0; };
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
