Linked List Manipulations Develop a program that will maintain an ordered linked
ID: 3547167 • Letter: L
Question
Linked List Manipulations 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<iostream>
#include<stdio.h>
using namespace std;
struct node
{
int data;
node *next;
};
node *front = NULL;
node *rear = NULL;
void insert_begin(int ele)
{
node *temp = new node;
temp->data = ele;
if(front == NULL)
{
temp->next = NULL;
front = temp;
rear = temp;
}
else
{
temp->next = front;
front = temp;
}
}
void insert_end(int ele)
{
node *temp = new node;
temp->data = ele;
temp->next = NULL;
if(rear == NULL)
{
rear = temp;
front = temp;
}
else
{
rear->next=temp;
rear = temp;
}
}
void del_begin()
{
node *temp = front;
if(temp == NULL)
cout<<" List is empty ";
else
{
temp = temp->next;
delete front;
front = temp;
}
}
void del_end()
{
node *temp = front;
node *prev = new node;
if(temp == NULL)
cout<<" List is empty ";
else
{
while(temp != rear)
{
prev = temp;
temp = temp->next;
}
delete temp;
rear = prev;
rear->next = NULL;
}
}
void insert_after_index(int ele,int pos)
{
node *temp = front;
node *prev = new node;
node *ins = new node;
int index,no,i=0;
ins->data = no;
while(i != pos)
{
prev = temp;
temp = temp->next;
if(prev == NULL)
{
cout<<" Index out of bounds ";
return;
}
i++;
}
if(temp != NULL)
{
prev->next = ins;
ins->next = temp;
}
else
{
prev->next = ins;
ins->next = NULL;
}
}
void insert_before_index(int ele,int pos)
{
node *temp = front;
node *prev = new node;
node *ins = new node;
int no,index;
ins->data = ele;
if(pos == 1)
{
ins->next = front;
front = ins;
}
else
{
int i=0;
while(i != pos-1)
{
prev = temp;
temp = temp->next;
i++;
if(temp == NULL)
{
cout<<" Index out of bounds ";
return;
}
}
prev->next = ins;
ins->next = temp;
}
}
void delete_index(int pos)
{
node *temp = front;
node *prev = new node;
node *after = new node;
int i=1;
if(pos == 1)
{
temp = temp->next;
delete front;
front = temp;
}
else
{
while(i != pos)
{
prev = temp;
temp = temp->next;
i++;
if(temp == NULL)
{
cout<<" Index out of bounds ";
return;
}
}
after = temp;
after = after->next;
delete temp;
prev->next = after;
}
}
void number_nodes()
{
node *temp = front;
if( temp == NULL)
cout<<" Number of nodes is: 0 ";
else
{
int i=0;
while(temp != NULL)
{
i++;
temp = temp->next;
}
cout<<" Number of nodes is: "<<i<<" ";
}
}
void display(node *q)
{
node *temp = q;
if(temp == NULL)
{
cout<<" The list is empty ";
}
else
{
cout<<" The current list is: ";
while(temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
}
void split(int pos)
{
node *b=NULL,*y,*c=NULL,*t=front;int z=0,x;
while(z!=pos)
{
x=t->data;
y->data=x;
y->next=NULL;
b=y;
t=t->next;
z++;
}
while(t!=NULL)
{
x=t->data;
insert_end(x);
t=t->next;
}
display(b);
display(c);
}
int main()
{
int ch,a,b;
ch =1;
while(ch != 11)
{
cout<<" MAIN MENU ";
cout<<"1.INSERT IN BEGINNING ";
cout<<"2.INSERT IN END ";
cout<<"3.INSERT AFTER POSITION ";
cout<<"4.INSERT BEFORE POSITION ";
cout<<"5.DELETE FROM BEGINNING ";
cout<<"6.DELETE FROM END ";
cout<<"7.DELETE PARTICULAR INDEX ";
cout<<"8.NUMBER OF NODES ";
cout<<"9.DISPLAY ";
//cout<<"10.split ";
cout<<"10.EXIT ";
cout<<"Enter your choice ";
cin>>ch;
switch(ch)
{
case 1:cin>>a;
insert_begin(a);
break;
case 2:cin>>a;
insert_end(a);
break;
case 3:cin>>a>>b;
insert_after_index(a,b);
break;
case 4:cin>>a>>b;
insert_before_index(a,b);
break;
case 5:
del_begin();
break;
case 6:
del_end();
break;
case 7:cin>>a;
delete_index(a);
break;
case 8:
number_nodes();
break;
case 9:
display(front);
break;
// case 10:cin>>a;split(a);
// break;
case 10:break;
default: cout<<" Enter a proper choice ";
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.