This is the assignment on Chapter 17. It is presented as a two part process but
ID: 3545945 • Letter: T
Question
This is the assignment on Chapter 17. It is presented as a two part process but the first part is for practice purposes only. You do not need to turn it in and you do not even need to complete it if you feel comfortable with writing the class right away.
Note: You need to turn only Part 2
PART 1:
Develop a program that will maintain an ordered linked list of positive whole numbers. Your program will provide for the following options:
a. Add a number
b. Delete a number
c. Search for a number
d. Display the whole list of numbers
At all times, the program will keep the list ordered (the smallest number first and the largest number last).
At the start of the program, the list will be empty. The user will add new numbers by choosing the "add" option and will supply the number to be included in the list. The program will add the number to the list at the appropriate position in the linked list so that the list will remain ordered.
If the number being added is already in the list, the program will display a message: "Duplicate number. Not added to the list". This will ensure that the list does not contain any duplicate numbers.
For removing a number from the list, the user will choose the "delete" option and supply the number to be removed. If the number is in the list, it will be eliminated from the linked list. Otherwise, a message will be displayed: "Number is not in the list".
For looking up a number in the list, the user will choose the "search" option and will provide the number to be found. If the number is in the list, the program will display: "the number is in the list". Otherwise, it will display:
Explanation / Answer
#include #include using namespace std; struct node{ int info; struct node *next; }; class Link{ node *list; public: Link(); void insert_at_begining(int); void insert_at_end(int); void insert_before_node(); void insert_after_node(); void delete_at_begining(); void delete_at_end(); void delete_before_node(); void delete_before_after_node(int); node *find_before_node(node*); void display(); }; Link::Link(){ list = NULL; } node *Link::find_before_node(node *p){ int count = 0; int count1 = 0; node *temp = new node; temp = list; while(temp!=p){ count++; temp=temp->next; } temp = list; while(count1next; } return temp; } void Link::insert_at_begining(int data){ node *p = new node; p->info = data; p->next = list; list = p; } void Link::insert_at_end(int data){ node *p = new node; node *r = new node; node *q = new node; r = list; p->info = data; p->next = NULL; if(list == NULL){ list = p; }else{ while(r->next!=NULL){ r = r->next; } r->next = p; } } void Link::insert_before_node(){ node *p = new node; node *l = new node; node *r = new node; int data1, data2; bool isFound = false; coutdata1; p = list; while(p != NULL){ if(p->info == data1){ isFound = true; break; } l = p; p = p->next; } if(isFound){ cout data2; r->info = data2; if(p==list){ insert_at_begining(data2); }else{ l->next = r; r->next = p; } }else{ coutnext; } if(isFound){ coutdata2; r->info = data2; if(p->next == NULL){ insert_at_end(data2); }else{ l = p->next; p->next = r; r->next = l; } }else{ coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.