DOUBLE LINKED LIST IMPLEMENTATION
CODE :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *prev,*next;
};
struct Node *head=NULL;
void insert_beg(int d)
{
struct Node *p;
p= (struct Node*)malloc(sizeof(struct Node));
p->data=d;
p->prev=NULL;
if(head == NULL)
{
p->next = NULL;
head=p;
}
else
{
p->next=head;
head->prev=p;
head=p;
}
}
void insert_end(int d)
{
struct Node *p;
p=(struct Node*)malloc(sizeof(struct Node));
p->data=d;
p->next = NULL;
if(head == NULL)
{
p->prev=NULL;
head=p;
}
else
{
struct Node *q = head;
while(q-> next != NULL)
q = q-> next;
q-> next = p;
p-> prev = q;
}
}
void insert_pos(int d,int pos)
{
int i;
struct Node *p;
p=(struct Node*)malloc(sizeof(struct Node));
p->data=d;
p->next = NULL;
if(head == NULL)
{
p->prev=NULL;
head=p;
}
else
{
struct Node*q=head;
for(i=1;i<pos-1;i++)
q=q->next;
p->next=q->next;
q->next->prev=p;
p->prev=q;
q->next=p;
}
}
void del_beg()
{
if(head == NULL)
printf("DLL is Empty and Deletion not possible");
else
{
struct Node *q = head;
if(q->next==NULL)
{
head = NULL;
free(q);
}
else{
head = head-> next;
head-> prev = NULL;
free(q);
}
}
}
void del_end()
{
if(head == NULL)
printf("DLL is Empty and Deletion not possible");
else
{
struct Node *p,*q= head;
if(q->next==NULL)
{
head = NULL;
free(q);
}
else
{
while(q-> next != NULL)
{
p=q;
q= q-> next;
}
p->next=NULL;
free(q);
}
}
}
void del_pos(int pos)
{
if(head == NULL)
printf("DLL is Empty and Deletion not possible");
else
{
int i;
struct Node *q,*p;
q=head;
if(q->next==NULL)
{
head = NULL;
free(q);
}
else
{
for(i=1;i<pos;i++)
{
p=q;
q=q->next;
}
p->next=q->next;
q->next=p;
free(q);
}
}
}
void display()
{
struct Node*p;
p=head;
if(head==NULL)
{
printf("\nDLL is empty");
return;
}
printf("\nDLL elements are:");
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
void search(int d)
{
struct Node *p;
int c=0;
p=head;
while(p!=NULL)
{
if(p->data==d)
{
printf("\nsearch element is found at %d position",c+1);
c=0;
return;
}
else
{
c++;
p=p->next;
}
}
if(p==NULL)
printf("\nsearch element is unsuccessful");
}
int main()
{
int d,op,pos;
do
{
printf("\n1.Insertion 2.Deletion 3.Traversal 4.Search");
printf("\nEnter your option: ");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter data to be inserted: ");
scanf("%d",&d);
printf("\nIn insertion 1.beg 2.end 3.pos");
printf("\nEnter your option: ");
scanf("%d",&op);
switch(op)
{
case 1: insert_beg(d);
display();
break;
case 2: insert_end(d);
display();
break;
case 3: printf("\nEnter the position: ");
scanf("%d",&pos);
insert_pos(d,pos);
display();
break;
}
break;
case 2: printf("\nDeletion from 1.beg 2.end 3.pos");
printf("\nEnter your option: ");
scanf("%d",&op);
switch(op)
{
case 1: del_beg();
display();
break;
case 2: del_end();
display();
break;
case 3: printf("\nEnter the position: ");
scanf("%d",&pos);
del_pos(pos);
display();
break;
}
break;
case 3: display();
break;
case 4: printf("\nEnter the element to be searched: ");
scanf("%d",&d);
search(d);
break;
default: printf("\nEnter a valid option");
break;
}
printf("\nDo you want to continue or press 0 to stop ");
scanf("%d",&d);
}while(d!=0);
return 0;
}
Comments
Post a Comment