//Program for implementing Doubly Linear Linked List.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
//Structure definition
typedef struct dll
{
int data;
struct dll *next,*prev;
}node;
node *head,*t,*temp;
//Creating head node
node *create()
{
node *head;
head=(node *)malloc(sizeof(node));
head->data=-1;
head->next=NULL;
head->prev=NULL;
printf("\nHead node is created...\n");
return(head);
}
//Inserting nodes into doubly linked list
void insert(node *head)
{
node *temp,*t;
int v;
temp=(node *)malloc(sizeof(node));
printf("\n\nEnter data for new node:");
scanf("%d",&v);
temp->data=v;
temp->next=NULL;
temp->prev=NULL;
t=head;
while(t->next!=NULL)
{
t=t->next;
}
t->next=temp;
temp->prev=t;
}
//Displaying the doubly linked list
void display(node *head)
{
node *temp;
temp=head->next;
while(temp->next!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
printf("%d\t",temp->data);
}
//Delition of node from doubly linked list
void Delete(node *head)
{
int c;
node *temp,*t;
temp=head;
printf("\n\nEnter the data to be deleted from list:");
scanf("%d",&c);
while(temp->next!=NULL)
{
if(temp->next->data==c)
{
t=temp->next;
temp->next=t->next;
t->next->prev=temp;
free(t);
}
else
{
temp=temp->next;
}
}
}
//Reversing the doubly linked list
void reverse(node *head)
{
int a[10],i=0;
node *temp;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
while(temp->prev!=NULL)
{
printf("%d\t",temp->data);
temp=temp->prev;
}
}
//Reverting the doubly linked list
void revert(node *head)
{
node *p,*q,*r,*t;
p=head->next;
t=head;
q=NULL;
while(p!=NULL)
{
r=q;
q=p;
p=p->next;
q->next=r;
p->prev=r;
q->prev=t;
}
t->next=q;
display(head);
}
//Main function
void main()
{
int ch;
node *head;
clrscr();
printf("*******Doubly Linear Linked List*******");
do
{
printf("\n\nMenues:");
printf("\n\n1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit");
printf("\n\nEnter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: head=create();
break;
case 2: insert(head);
break;
case 3: Delete(head);
break;
case 4: reverse(head);
break;
case 5: revert(head);
break;
case 6: display(head);
break;
case 7: exit(0);
}
}while(ch<7);
getch();
}
/* Output of Program:
*******Doubly Linear Linked List*******
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:1
Head node is created...
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:2
Enter data for new node:11
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:2
Enter data for new node:22
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:2
Enter data for new node:33
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:2
Enter data for new node:44
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:2
Enter data for new node:55
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:2
Enter data for new node:66
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:6
11 22 33 44 55 66
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:3
Enter the data to be deleted from list:44
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:6
11 22 33 55 66
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:4
66 55 33 22 11
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:6
11 22 33 55 66
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:5
66 55 33 22 11
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:6
66 55 33 22 11
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:3
Enter the data to be deleted from list:66
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:6
55 33 22 11
Menues:
1.Create 2.Insert 3.Delete 4.Reverse 5.Revert 6.Display 7.Exit
Enter Your Choice:7*/
Pankaj Gaikar is a
professional blogger
from Pune, India who writes on Technology, Android,
Gadgets, social media and latest tech updates at
Punk Tech
,
Being Android
&
Shake The Tech
. Email me
HERE
0 comments :
Post a Comment