23 Aug 2019

  • August 23, 2019
  • Amitraj

 Doubly Linked List Data Structure

-> Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. 

-> In a doubly linked list, each node contains a data part and two addresses, one for the previous node and one for the next node.





Basic Operations

Following are the basic operations supported by a list.

Insertion − Adds an element at the beginning of the list.

Deletion − Deletes an element at the beginning of the list.

Insert Last − Adds an element at the end of the list.

Delete Last − Deletes an element from the end of the list.

Insert After − Adds an element after an item of the list.

Delete − Deletes an element from the list using the key.

Display forward − Displays the complete list in a forward manner.

Display backward − Displays the complete list in a backward manner.


/*c program to demonstrate DLL(Doubly linked list) and perform create,insert,Delete,show,length and search operations*/                  


#include<stdlib.h>

#include<stdio.h>
struct node
{
int info;
    struct node *left;
struct node *right;
}*head=NULL,*tail=NULL,*t,*temp;

void create()

{
t =(struct node *)malloc(sizeof(struct node));
printf("Enter a node info:");
scanf("%d",&t->info);
if(head==NULL)
{
t->left=NULL;
t->right=NULL;
head=tail=t;
}
else
{
t->left=tail;
t->right=NULL;
tail->right=t;
tail=t;
}
}

void traverseltor()

{
for(t=head;t!=NULL;t=t->right)
printf("%d ",t->info);
}

void traversertol()

{
for(t=tail;t!=NULL;t=t->left)
printf("%d ",t->info);
}

int length()

{
int i=0;
for(t=tail;t!=NULL;t=t->left)
     i++;
    return i;
}

void insert()

{
int p,l;
printf("Enter a position:");
scanf("%d",&p);
l=length();
t=(struct node *)malloc(sizeof(struct node));
printf("Enter a node to insert:");
scanf("%d",&t->info);
if(p>l+1||p<=0)
{
printf("Invalid position..");
return;
}
if(p==1)
{
t->left=NULL;
t->right=head;
head->left=t;
head=t;
}
else
if(p==l+1)
{
t->left=tail;
t->right=NULL;
tail->right=t;
tail=t;
}
else
{
    temp=head;
int i;
for(i=1;i<p-1;i++)
temp=temp->right;
t->right=temp->right;
t->left=temp;
temp->right->left=t;
temp->right=t;
}
printf("Node inserted..\n");
}

void del()

{
int p,l;
printf("Enter a position:");
scanf("%d",&p);
l=length();
if(p>l||p<=0)
{
printf("Invalid position..");
return ;
}
else
if(p==1)
{
temp=head;
temp->right->left=NULL;
head=head->right;
free(temp);
}
else
if(p==l)
{
temp=tail;
temp->left->right=NULL;
tail=tail->left;
free(temp);
}

else

{
temp=head;
int i;
for(i=1;i<p;i++)
temp=temp->right;
temp->left->right=temp->right;
temp->right->left=temp->left;
free(temp);
}
printf("Node deleted..\n");
}

void search()

{
int s;
printf("Enter a no. to search:");
scanf("%d",&s);
for(t=head;t!=NULL;t=t->right)
{
if(t->info==s)
{
printf("Node found..\n");
return;
}
}
    printf("Node not found..\n");
}

int main()

{
int n;
do
{
printf("::::DLL Operation::::\n");
printf("1.create\n 2.insert\n 3.Delete\n 4.Traversel L to R\n 5.Traversel R to L\n 6.Length\n 7.Search\n 8.exit\n");
printf("Enter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: create(); break;
case 2: insert(); break;
case 3: del(); break;
case 4: traverseltor(); break;
case 5: traversertol(); break;
case 6: printf("size is= %d\n",length()); break;
case 7: search(); break;
case 8: return;

}
}while(n!=8);
return 0;
}


//INPUT/OUTPUT:-



::::DLL Operation::::

1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:1

Enter a node info:11
::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:1

Enter a node info:22
::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:1

Enter a node info:33
::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:5

33 22 11 ::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:4

11 22 33 ::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:2

Enter a position:2
Enter a node to insert:44
Node inserted..
::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:4

11 44 22 33 ::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:3

Enter a position:2
Node deleted..
::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:4

11 22 33 ::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:6

size is= 3
::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:7

Enter a no. to search:33
Node found..
::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:7

Enter a no. to search:88
Node not found..
::::DLL Operation::::
1.create
 2.insert
 3.Delete
 4.Traversel L to R
 5.Traversel R to L
 6.Length
 7.Search
 8.exit

Enter your choice:8

exit...

Translate

Popular Posts