Question: How can I make it so in this program we have to make the linked list first before any of the other options can be
How can I make it so in this program we have to make the linked list first before any of the other options can be chosen.
#include
using namespace std;
struct node
int data;
struct node prev;
struct node next;
;
To insert a new number in the doubly linked list
void Addnode head,int x
struct node temphead;
whiletempNULL
iftempdatax
cout"Number already exists.";
return;
temptempnext;
create a new node
node newnode new node;
newnodedatax;
newnodenextNULL;
newnodeprevNULL;
if the linked list is empty
ifheadNULL
headnewnode;
return;
else
else
To insert in the beginning
ifheaddatanewnodedata
newnodenexthead;
newnodenextprevnewnode;
headnewnode;
To insert in between and in end
else
struct node currhead;
whilecurrnext!NULL && currnextdatadata
currcurrnext;
newnodenextcurrnext;
ifcurrnext!NULL
newnodenextprevnewnode;
currnextnewnode;
newnodeprevcurr;
To delete a number from doubly linked list
int Deletestruct node head,int x
ifhead NULL
cout"The linked list is empty.";
return ;
To delete from beginning
ifheaddatax
headheadnext;
return ;
To delete in between or from end
struct node currhead;
whilecurrdata!x && currnext!NULL
currcurrnext;
ifcurrdata!x
return ;
ifcurrnext!NULL
currnextprevcurrprev;
ifcurrprev!NULL
currprevnextcurrnext;
return ;
To display the doubly linked list
void displayDLLstruct node head
whileheadNULL
cout"Number: data
;
headheadnext;
To search in the doubly linked list
int Searchstruct node head, int x
int count;
whileheadNULL
To count the position of the number
count;
ifheaddatax
return count;
headheadnext;
if reaches here, that means number was not found
return ;
To modify the doubly linked list
void Modifystruct node head,int sint f
check if the head node is the one we are searching for
struct node currhead;
ifSearchcurrs
cout"The number you want to modify doesn't exist.";
return;
whilecurrNULL
ifcurrdataf
cout"The number you want to change to already exists.";
return;
currcurrnext;
Deleteheads;
Addheadf;
cout"The number is modified.";
To delete the entire doubly linked list
void deleteEntireListstruct node head
struct node temp;
whileheadNULL
temphead;
headheadnext;
freetemp; to free the memory of the node
int main
struct node head;
int choice;
cout"Pick a task from the menu below:
;
cout
;
cout Create
Add
Delete
Display";
cout
Modify
Purge entire list
Search for node
Exit the program";
cout
;
do
cout"User's task pick: ;
cinchoice;
cout
;
ifchoice
headNULL;
cout"Linked List created.";
else ifchoice
int x;
cout"Enter a value to add: ;
cinx;
Add&head,x;
cout"Number added to list.";
else ifchoice
int x;
cout"What would you like to delete: ;
cinx;
xDelete&head,x;
ifx
cout"Number is deleted from linked list.";
else
cout"Number not found.";
else ifchoice
ifheadNULL
cout"End of linked list.";
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
