COMP280 Programming Assignment 3 (List Solution //header file #ifndef LINKED LIS
ID: 3648959 • Letter: C
Question
COMP280 Programming Assignment 3 (ListExplanation / Answer
//header file #ifndef LINKED LIST_H #define LINKED LIST_H #include #include typedef int ElementType; struct node { ElementType data; node * next; }; class LinkedList { public: LinkedList(); void add(ElementType A);//added an extra function of adding bool empty(); // return true if the list is empty, otherwise return false void InsertInOrder(ElementType x);//insert a value x in numerical order in the list void Delete(ElementType x); //if value x is in the list, remove x void Display();// Display the data values in the list private: node * first;//pointer to the first node in the list }; #endif //.cpp file #include "Linked List.h" #include #include using namespace std; LinkedList::LinkedList() { first=NULL; } void LinkedList::add(ElementType A) { if (first==NULL) { first=new node; first->data=A; first->next=NULL; } else { node *curr=first; node *prev=NULL; while (curr!=NULL) { prev=curr; curr=curr->next; } curr=new node; curr->data=A; curr->next=NULL; prev->next=curr; } } bool LinkedList::empty() { if (first==NULL) return true; else return false; } void LinkedList::Delete(ElementType A) { node *curr=first; node *prev=NULL; bool flag=true; if (first->data==A) { first=first->next; delete first; flag=false; } else { while (flag && curr!=NULL) { if (curr->data==A) { prev->next=curr->next; delete curr; flag=false; } prev=curr; curr=curr->next; } if (flag==true) coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.