Hey I need some help with a project I\'m working on. My professor wants us to ma
ID: 3533773 • Letter: H
Question
Hey I need some help with a project I'm working on. My professor wants us to make an ordered doubly linked list. The one i made Isn't ordered and I'm confused as to where I would start or how to go about it. Please help!
Here's the .cpp
#include <iostream>
#include <cstdlib>
#include <string>
/* *****************************************************************************************
* 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-> next != NULL)
{
if(temp2 == NULL)
{
cout<<"Given node not found!"<<endl;
break;
}
else
{
temp3=temp2;
temp2=temp2->next;
}
}
temp1->next=temp2;
temp3->next=temp1;
temp1->prev=temp3;
temp2->prev=temp1;
break;
}
}
/* *****************************************************************************************
* Function name: display *
* Function purpose: displays the list *
* Function parameters: N/A *
* Function type: void *
***************************************************************************************** */
void display()
{
temp3=start;
if(start==NULL)
cout << "There are no records to display!" << endl;
else
{
cout << "Here is your list: " << endl << endl;
while(temp3 != NULL)
{
cout << "ID: " << temp3->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;
}
temp1=temp1->next;
}
}
/* *****************************************************************************************
* Function name: delNode *
* Function purpose: deletes nodes from the linked list *
* Function parameters: N/A *
* Function type: void *
***************************************************************************************** */
void delnode()
{
char d;
cout << "Press 's' to delete from start of list" << endl
<< "Press 'm' to delete from middle of list" << endl
<< "Press 'e' to delete from the enf of the list" << endl;
cin>>d;
switch (d)
{
case's':
if (start == NULL)
{
cout<<"There are no records to delete!"<<endl;
}
else
{
temp1=start;
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;
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
delete temp1;
temp2->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-> next != NULL)
{
if(temp2 == NULL)
{
cout<<"Given node not found!"<<endl;
break;
}
else
{
temp3=temp2;
temp2=temp2->next;
}
}
temp1->next=temp2;
temp3->next=temp1;
temp1->prev=temp3;
temp2->prev=temp1;
break;
}
}
/* *****************************************************************************************
* Function name: show *
* Function purpose: Displays the list from tail to end *
* Function parameters: N/A *
* Function type: void *
***************************************************************************************** */
void show()
{
cout << "Here is your list displayed backwards:" << endl << endl;
temp3=start;
if(start == NULL)
cout<< "There are no records to display!" << endl;
else
{
while(temp3->next!=NULL)
{
temp3=temp3->next;
}
while(temp3->prev!=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()
{
while(start != NULL)
{
if(start == NULL)
cout << "The list is empty!";
else
{
temp1=start;
delete temp1;
start = start->next;
}
}
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);
}
/* *****************************************************************************************
* 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->next != NULL)
{
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);
}
temp1=temp1->next;
}
}
Explanation / Answer
You have 2 values which it can be ordered in, what does your professor want it ordered in? by Name? by ID number?