Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ok this is the last time Im posting this question. I keep messing up and I\'m ne

ID: 3533782 • Letter: O

Question

Ok this is the last time Im posting this question. I keep messing up and I'm new to this website. My prof gave us a project for a Doubly Linked List. He wants any record that is given to be AUTOMATICALLY ordered in the list. In my code I have it where you choose to insert in start, middle or end. That's not what he wants. He doesnt want the user to choose, he wants the program to automatically do it. I really need help with this ASAP: Code #include #include #include /* ***************************************************************************************** * This program creates a doubly linked list and allows the user to perform options such as * * Insert, Remove, and so on. * ***************************************************************************************** */ using namespace std; struct node { int id; string name; node *next; node *prev; }; //FUNCTION HEADERS int menu(); void addnode(); void delnode(); void display(); void show(); void search(); void purge(); void modify(); //POINTER DECLARATIONS node *start=NULL, *temp1, *temp2, *temp3, *temp4; //MAIN int main() { //Function call to menu function menu(); return 0; } /* ***************************************************************************************** * Function name: addNode * * Function purpose: adds records to the doubly linked list * * Function parameters: N/A * * Function type: void * ***************************************************************************************** */ void addnode() { char r; temp1 = new node; temp4 = new node; cout << "Enter a Name and ID to store: " << endl; cout << "ID: "; cin >> temp1->id; cout << endl << "Name: "; cin.ignore(); getline(cin,temp1-> name); temp4=start; if(start!=NULL) { while(temp4 != NULL) { if(temp4->id == temp1-> id) { cout << "That record exists!" << endl; return; } temp4 = temp4->next; } } cout << "Press 's' to insert from start of list" << endl << "Press 'm' to insert from middle of list" << endl << "Press 'e' to insert from the enf of the list" << endl; cin>>r; switch (r) { case's': if(start == NULL) { start = temp1; temp1->next = NULL; temp1->prev = NULL; } else { temp2 = start; temp1->next = temp2; temp1->prev = NULL; start = temp1; temp2->prev = temp1; } break; case'e': if(start == NULL) { start=temp1; temp1->next=NULL; temp1->prev=NULL; } else { temp2=start; while(temp2->next!=NULL) temp2=temp2->next; temp2->next=temp1; temp1->prev=temp2; temp1->next=NULL; } break; case'm': int num; cout << "Enter the ID after which you want to enter: "; cin >> num; cout << endl; temp2=start; while(temp2&&temp2->id!=num) temp2=temp2->next; if(temp2) { temp1->next=temp2->next; temp1->prev=temp2; temp2->next=temp1; if(temp1->next) temp1->next->prev=temp1; else temp1->next=NULL; } else { cout<<" Node not found "<id << " " << endl; cout << "Name:" << temp3-> name << endl << endl; temp3 = temp3->next; } } } /* ***************************************************************************************** * Function name: search * * Function purpose:searches the list * * Function parameters: N/A * * Function type: void * ***************************************************************************************** */ void search() { int p; cout << endl; cout << "Enter ID to search:"; cin >> p; temp1=start; while(temp1->next != NULL) { if(temp1-> id ==p) { cout << endl; cout << "ID: " << temp1-> id << endl << "Name: " << temp1-> name << endl; return; } temp1=temp1->next; } cout<<"record not found"<>d; switch (d) { case's': if (start == NULL) { cout<<"There are no records to delete!"<next==NULL) start=NULL; else { start=start->next; start->prev=NULL; } delete temp1; } break; case'e': if(start == NULL) { cout<<"There are no records to delete!" << endl; } else { temp1=start; if(temp1->next==NULL) { start=NULL; delete temp1; } else { while(temp1->next!=NULL) { temp2=temp1; temp1=temp1->next; } temp2->next=NULL; delete temp1; } } break; case'm': int num; cout << "Enter the ID of the node you want to delete: "; cin >> num; cout << endl; temp2=start; while(temp2&&temp2->id!=num) { temp1=temp2; temp2=temp2->next; } if(temp2) { temp1->next=temp2->next; temp1->next->prev=temp1; delete temp2; } else { cout<<" record not found"<next!=NULL) { temp3=temp3->next; } while(temp3!=NULL) { cout << "ID: " << temp3-> id << endl << "Name: "<< temp3-> name<< endl << endl; temp3 = temp3 -> prev; } } } /* ***************************************************************************************** * Function name: purge * * Function purpose: purges the list * * Function parameters: N/A * * Function type: void * ***************************************************************************************** */ void purge() { if(start == NULL) { cout << "The list is empty!"; return; } while(start != NULL) { temp1=start; start = start->next; delete temp1; } cout << "List has been purged!"; system("pause"); menu(); } /* ***************************************************************************************** * Function name: Menu * * Function purpose: displays the menu and allow user to perform options on the Doubly * * Function parameters: N/A * * Function type: void * ***************************************************************************************** */ int menu() { system("CLS"); int choice; char returner; cout << "****************************************************" << endl; cout << "*************Doubly Linked List Program*************" << endl; cout << "****************************************************" << endl; cout << "" << endl; cout << "Menu Options " << endl; cout << "1. Add a name " << endl; cout << "2. Delete a name" << endl; cout << "3. Display List" << endl; cout << "4. Search for someone" << endl; cout << "5. Display list backwards" << endl; cout << "6. Modify" << endl; cout << "7. Purge list" << endl; cout << "8. Exit program" << endl << endl; cout << "Choice: "; do { cin >> choice; switch (choice) { case 1: addnode(); cout << "Want to process more y/n" << endl; cin >> returner; system("CLS"); if(returner == 'y') menu(); else return 0; break; case 2: delnode(); cout << "Want to process more y/n" << endl; cin >> returner; system("CLS"); if(returner == 'y') menu(); else return 0; break; case 3: display(); cout << "Want to process more y/n" << endl; cin >> returner; system("CLS"); if(returner == 'y') menu(); else return 0; break; case 4: search(); cout << "Want to process more y/n" << endl; cin >> returner; system("CLS"); if(returner == 'y') menu(); else return 0; break; case 5: show(); cout << "Want to process more y/n" << endl; cin >> returner; system("CLS"); if(returner == 'y') menu(); else return 0; break; case 6: modify(); cout << "Want to process more y/n" << endl; cin >> returner; system("CLS"); if(returner == 'y') menu(); else return 0; break; case 7: purge(); cout << "Want to process more y/n" << endl; cin >> returner; system("CLS"); if(returner == 'y') menu(); else return 0; break; case 8: cout << "Now exitiing" << endl; system("PAUSE"); break; // return EXIT_SUCCESS; // return 0; default: cout << "Bad input" << endl; break; } } while(choice != 8); return 0; } /* ***************************************************************************************** * Function name: modify * * Function purpose: modifies the records user wants * * Function parameters: N/A * * Function type: void * ***************************************************************************************** */ void modify() { system("CLS"); int p; cout << "Enter ID to modify:"; cin >> p; temp1=start; while(temp1) { if(temp1->id==p) { cout << "Here's the record you'd like to modify" << endl; cout << "ID: " << temp1-> id << endl << "Name: " << temp1-> name << endl << endl; cout << "Enter the modifed version: " << endl << endl; cout << "ID: " << temp1-> id << endl << "New Name: "; cin.ignore(); getline(cin,temp1-> name); return; } temp1=temp1->next; } cout<<" record not found"<

Explanation / Answer

#include <iostream>

#include <list>


using namespace std;


void add(list<int> &List)

{

cout<<"Enter number to be added : ";

int num;

cin>> num;

list<int> :: iterator it;

for(it=List.begin();it!=List.end();it++)

{

if(*it>num)

break;

}

List.insert(it,num);

}


void pop(list<int> &List)

{

cout<<"Enter number to be deleted : ";

int num;

cin>> num;

list<int> :: iterator it;

for(it=List.begin();it!=List.end();it++)

{

if(*it==num)

break;

}

if(it!=List.end())

List.erase(it);

else

cout<<"Number does not exist in the list."<<endl;

}


void print(list<int> &List)

{

cout<<"List : ";

list<int> :: iterator it;

for(it=List.begin();it!=List.end();it++)

{

cout <<*it<<" ";

}

cout<<endl;

}


int main()

{

list<int> DL;


cout << "Enter choice, 0 to quit, 1 to add , 2 to delete : ";

int ch;

cin >> ch;

while(ch!=0)

{

if(ch==1)

{

add(DL);

print(DL);

}

else if(ch==2)

{

pop(DL);

print(DL);

}

else

{

cout<<"Invalid Choice"<<endl;

}

cout << "Enter choice, 0 to quit, 1 to add , 2 to delete : ";

cin >> ch;

}


}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote