0% found this document useful (0 votes)
7 views4 pages

Exp 17.1 DSA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Exp 17.1 DSA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// Experiment 17 : WAP to perform Creation, Insertion, Deletion & Traversal on a

Double Linked List.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ int num; //Data.
struct node *back; //Pointer To Previous Node.
struct node *forw; //Pointer To Next Node.
};
void main()
{
int ele,ch=0,ch1,temp;
struct node *First,*Last,*This,*Temp,*Temp2;
First=Last=This=Temp=Temp2=NULL;
printf("Experiment 17 : This Is A Program To Perform Insertion, Deletion &
Traversal In A Double Linked List.");
while (ch!=4) //Menu.
{ printf("\nPress 1->Insert, 2->Delete, 3->Display or 4->Exit.");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{ case 1: //Inserting Element.
{ if(First!=NULL)
{ printf("\nEnter Your Choice,Where Do You Wish To Insert The
Element :");
printf("\n1. At The Beginning.");
printf("\n2. After A Particular Element.");
printf("\n3. At The End.\n");
scanf("%d",&ch1);
}
else
ch1=1;
switch(ch1)
{ case 1: //At The Beginning.
{ Temp=(struct node *)malloc(sizeof(struct node *));
printf("\nEnter The Element To Be Inserted : ");
scanf("%d",&ele);
Temp->num=ele;
Temp->back=NULL;
Temp->forw=NULL;
if(First==NULL)
First=Last=Temp;
else
{ Temp->forw=First;
First->back=Temp;
First=First->back;
}
printf("\n\t\t! Element Inserted Successfully!");
break;
}
case 2: //After A Particular Element.
{ printf("\nEnter The Element After Which The Element
Has To Be Inserted : ");
scanf("%d",&temp);
This=First;
while (This->num!=temp && This!=NULL)
This=This->forw;
if( This==NULL ) //Checking If Element Exists.
printf("\n\t\tThe Element Does Not Exists In The
Existing Linked List.");
else //Inserting Node At Required Location.
{ printf("\nEnter The Element To Be Inserted :
");
scanf("%d",&ele);
Temp=(struct node *)malloc(sizeof(struct node
*));
Temp->num=ele;
Temp->back=Temp->forw=NULL;
Temp2=This->forw;
Temp->forw=Temp2;
Temp->back=This;
This->forw=Temp;
if(Last==This)
Last=Temp;
else
Temp2->back=Temp;
printf("\n\t\t! Element Inserted
Successfully !");
}
break;
}
case 3: //At The End.
{ printf("\nEnter The Element To Be Inserted : ");
scanf("%d",&ele);
Temp=(struct node *)malloc(sizeof(struct node*));
Temp->num=ele;
Temp->back=NULL;
Temp->forw=NULL;
Temp->back=Last;
Last->forw=Temp;
Last=Temp;
printf("\n\t\t! Element Inserted Successfully !");
break;
}
}
break;
}

case 2: //Deleting Element.


{ if(First==NULL) //Check For Underflow.
printf("\n\t\t!!! Linked List Contains No Elements !!!");
else //Delete From Available Nodes.
{ if(First==Last)
ch1=1;
else
{ printf("\nEnter Your Choice for Deletion of Element :");
printf("\n1. Beginning Element.");
printf("\n2. Any Particular Element.");
printf("\n3. Last Element.\n");
scanf("%d",&ch1);
}
switch(ch1)
{ case 1: //First Element.
{ Temp=First;
ele=Temp->num;
if(First==Last)
First=Last=NULL;
else
{ First=First->forw;
First->back=NULL;
}
free(Temp);
printf("\n\t\tThe Element %d Is Deleted
Successfully.",ele);
break;
}
case 2: //A Particular Element.
{ printf("\nEnter The Element To Be Deleted : ");
scanf("%d",&ele);
This=First;
while(This->num!=ele && This!=NULL)
This=This->forw;
if(This==NULL)
printf("\n\t\t!!! The Element Does Not
Exist !!!");
else
{ Temp=This->back;
Temp2=This->forw;
ele=This->num;
if(Temp!=NULL)
Temp->forw=Temp2;
if(Temp2!=NULL)
Temp2->back=Temp;
This->back=NULL;
This->forw=NULL;
free(This);
printf("\n\t\tThe Element %d Is Deleted
Successfully.",ele);
}
break;
}
case 3: //Last Element.
{ This=Last;
Temp=This->back;
ele=This->num;
Temp->forw=NULL;
Last=Temp;
free(This);
printf("\n\t\tThe Element %d Is Deleted
Successfully.",ele);
break;
}
}
}
break;
}

case 3: //Displaying Elements.


{ if(First==NULL)
printf("\n\t\t! The Linked List Is Empty !");
else
{ printf("\n1. Forward Display.");
printf("\n2. Backward Display.");
printf("\nEnter Your Choice :");
scanf("%d",&ch1);
printf("\nThe Linked List Is :");
if(ch1==1)
{
This=First;
printf("\nFIRST -> ");
while(This!=NULL)
{ printf("%d -> ",This->num);
This=This->forw;
}
printf("LAST");
}
else if(ch1==2)
{ This=Last;
printf("\nLAST -> ");
while(This!=NULL)
{ printf("%d -> ",This->num);
This=This->back;
}
printf("FIRST");
}
}
break;
}

case 4: //Exit.
break;

default: //Wrong Choice.


{ printf("\n\t\t!!! You Entered Wrong Choice !!!");
break;
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy