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 Add(node** head,int x){
struct node* temp=*head;
while(temp!=NULL){
if(temp->data==x){
cout<<"Number already exists.";
return;
}
temp=temp->next;
}
//create a new node
node* newnode = new node;
newnode->data=x;
newnode->next=NULL;
newnode->prev=NULL;
//if the linked list is empty
if(*head==NULL){
*head=newnode;
return;
}
//else
else{
//To insert in the beginning
if((*head)->data>newnode->data){
newnode->next=*head;
newnode->next->prev=newnode;
*head=newnode;
}
//To insert in between and in end
else{
struct node* curr=*head;
while(curr->next!=NULL && curr->next->datadata){
curr=curr->next;
}
newnode->next=curr->next;
if(curr->next!=NULL)
newnode->next->prev=newnode;
curr->next=newnode;
newnode->prev=curr;
}
}
}
//To delete a number from doubly linked list
int Delete(struct node** head,int x){
if(*head == NULL){
cout<<"The linked list is empty.";
return -1;
}
//To delete from beginning
if((*head)->data==x){
*head=(*head)->next;
return 1;
}
//To delete in between or from end
struct node* curr=*head;
while(curr->data!=x && curr->next!=NULL){
curr=curr->next;
}
if(curr->data!=x){
return 0;
}
if(curr->next!=NULL){
curr->next->prev=curr->prev;
}
if(curr->prev!=NULL){
curr->prev->next=curr->next;
}
return 1;
}
//To display the doubly linked list
void displayDLL(struct node* head){
while(head!=NULL){
cout<<"Number: "<data<<"
";
head=head->next;
}
}
//To search in the doubly linked list
int Search(struct node* head, int x){
int count=0;
while(head!=NULL){
//To count the position of the number
count++;
if(head->data==x){
return count;
}
head=head->next;
}
//if reaches here, that means number was not found
return -1;
}
//To modify the doubly linked list
void Modify(struct node** head,int s,int f){
//check if the head node is the one we are searching for
struct node* curr=*head;
if(Search(curr,s)==-1){
cout<<"The number you want to modify doesn't exist.";
return;
}
while(curr!=NULL){
if(curr->data==f){
cout<<"The number you want to change to already exists.";
return;
}
curr=curr->next;
}
Delete(head,s);
Add(head,f);
cout<<"The number is modified.";
}
//To delete the entire doubly linked list
void deleteEntireList(struct node** head){
struct node* temp;
while(*head!=NULL){
temp=*head;
*head=(*head)->next;
free(temp); //to free the memory of the node
}
}
int main(){
struct node* head;
int choice;
cout<<"Pick a task from the menu below:
";
cout<<"********************************
";
cout<<"1. Create
2. Add
3. Delete
4. Display";
cout<<"
5. Modify
6. Purge entire list
7. Search for node
8. Exit the program";
cout<<"
********************************
";
do{
cout<<"User's task pick: ";
cin>>choice;
cout<<"--------------------------------
";
if(choice==1){
head=NULL;
cout<<"Linked List created.";
}
else if(choice==2){
int x;
cout<<"Enter a value to add: ";
cin>>x;
Add(&head,x);
cout<<"Number added to list.";
}
else if(choice==3){
int x;
cout<<"What would you like to delete: ";
cin>>x;
x=Delete(&head,x);
if(x==1){
cout<<"Number is deleted from linked list.";
}
else{
cout<<"Number not found.";
}
}
else if(choice==4){
if(head==NULL){
cout<<"End of linked list.";

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!