Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1 2

#include<iostream> 3
switch(choice) case 8:
#include<stdlib.h> display();
{
using namespace std; break;
case 1:
case 9:
beginsert(); exit(0);
//Creating node
struct node break; break;
{ default:
case 2:
int data; cout<<"Please enter valid choice..";
lastinsert(); }
struct node *next;
}; break; }
struct node *head; }
case 3:
randominsert();
//Function declaration //Displaying the list of nodes 11
void beginsert (); break; void display()
void lastinsert (); case 4: {
begin_delete(); struct node *ptr;
void randominsert(); ptr = head;
void begin_delete(); break; if(ptr == NULL)
void last_delete(); case 5: {
last_delete(); cout<<"Nothing to print";
void random_delete();
}
void display(); break;
else
void search(); case 6: {
random_delete(); cout<<"\nprinting values . . . . .\n";
while (ptr!=NULL)
//Main Function Definition break;
{
int main () case 7: cout<<ptr->data<<" ";
{
search(); ptr = ptr -> next;
int choice =0;
}
while(choice != 9) break;
}
{
}
cout<<"\n\n*********Main Menu*********\n";
cout<<"\nChoose one option from the following list: \n";
cout<<"\n===============================================\n";
cout<<"\n1.Insert at beginning\n2.Insert at last\n3.Insert at any random location\n";
cout<<"4.Delete from Beginning\n5.Delete from last\n6.Delete node after specified location\n"
cout<<"7.Search for an element\n8.Display elements\n9.Exit\n";
cout<<"\nEnter your choice?\n";
cin>>choice;
4 5
//Inserting at the beginning of the node //Inserting at the end of the node
void beginsert() void lastinsert()
{
struct node *ptr; {
int item; struct node *ptr,*temp;
ptr = (struct node *) malloc(sizeof(struct node *)); int item;
if(ptr == NULL)
ptr = (struct node*)malloc(sizeof(struct node));
{
cout<<"\nOVERFLOW"; if(ptr == NULL)
} {
else
cout<<"\nOVERFLOW";
{
cout<<"\nEnter value \n"; }
cin>>item; else
ptr->data = item; {
ptr->next = head;
head = ptr; cout<<"\nEnter value?\n";
cout<<"\nNode inserted"; cin>>item;
} ptr->data = item;
}
if(head == NULL)

6 {
//Deleting at the beginning of the node
ptr -> next = NULL;
void begin_delete()
head = ptr;
{
cout<<"\nNode inserted";
struct node *ptr;
}
if(head == NULL)
else
{
{
cout<<"\nList is empty\n";
temp = head;
}
while (temp -> next != NULL)
else
{
{
temp = temp -> next;
ptr = head;
}
head = ptr->next;
temp->next = ptr;
delete ptr;
ptr->next = NULL;
cout<<"\nNode deleted from the begining ...\n";
cout<<"\nNode inserted";
}
}
}
}
}
7 8
//Inserting after a specified location //Deleting after a specified location
void randominsert() void random_delete()
{
{
int i,loc,item;
struct node *ptr, *temp; struct node *ptr,*ptr1;
ptr = (struct node *) malloc (sizeof(struct int loc,i;
node));
cout<<"\n Enter the location of the node after
if(ptr == NULL)
{ which you want to perform deletion: ";
cout<<"\nOVERFLOW"; cin>>loc;
}
ptr=head;
else
{ for(i=0;i<loc;i++)
cout<<"\nEnter element value: "; {
cin>>item; ptr1 = ptr;
ptr->data = item;
cout<<"\nEnter the location after which you ptr = ptr->next;
want to insert: ";
cin>>loc; if(ptr == NULL)
temp=head;
{
for(i=0;i<loc;i++)
{ cout<<"\nCan't delete";
temp = temp->next; return;
if(temp == NULL)
}
{
cout<<"\ncan't insert\n"; }
return; ptr1 ->next = ptr ->next;
}
delete ptr;

} cout<<"\nDeleted node at "<<loc+1;


ptr ->next = temp ->next; }
temp ->next = ptr;
cout<<"\nNode inserted";
}
}
9 10
//Deleting at the end of the node //Searching a node
void last_delete() void search()
{
{
struct node *ptr;
struct node *ptr,*ptr1; int item,i=0,status=-1;
if(head == NULL) ptr = head;
if(ptr == NULL)
{
{
cout<<"\nlist is empty"; cout<<"\nEmpty List\n";
} }
else
else if(head -> next == NULL)
{
{ cout<<"\nEnter item which you want to search?: ";
head = NULL; cin>>item;
delete head; while (ptr!=NULL)
{
cout<<"\nOnly node of the list deleted ...\n"; if(ptr->data == item)
} {
else cout<<"item found at location: "<<i;
status++;
{
}
ptr = head;
while(ptr->next != NULL) i++;
ptr = ptr -> next;
{
}
ptr1 = ptr;
ptr = ptr ->next; if(status==-1)
{
}
ptr1->next = NULL; cout<<"Item not found\n";
delete ptr; }
cout<<"\nDeleted Node from the last ...\n";
}
}
}
}

You might also like