Double linked list using ‘C’
A doubly connected list is an information structure made out of a succession of components, where every component contains a reference to the past and next components in the rundown. This considers proficient crossing in the two headings.
Here are the normal tasks performed on a doubly connected list:
1. Inclusion toward the start: To embed another component toward the start of the rundown, you make another hub, update the references, and change the head pointer to the new hub.
2. Inclusion toward the end: To embed another component toward the finish of the rundown, you make another hub, update the references, and change the tail pointer to the new hub.
3. Inclusion at a particular position: To embed another component at a particular position, you cross the rundown to track down the ideal position, make another hub, update the references of the neighboring hubs, and change the references as needs be.
4. Cancellation all along: To erase the primary component of the rundown, you update the references of the head and the following hub, and eliminate the reference to the past hub from the new head.
5. Erasure from the end: To erase the last component of the rundown, you update the references of the tail and the past hub, and eliminate the reference to the following hub from the new tail.
6. Erasure of a particular hub: To erase a particular hub, you update the references of the past and next hubs to sidestep the hub to be erased, and change the references in like manner.
7. Crossing: You can navigate a doubly connected list in the two bearings by following the references of the hubs, either from the head to the tail or the other way around.
These are the fundamental tasks of a doubly connected list. Furthermore, you can likewise carry out different tasks, for example, looking for a component, counting the quantity of components, and getting to components at a particular position.
Program:-
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node*prev;
struct node*next;
}*head,*last;
int main()
{
int n,ch;
printf("Enter number of nodes you want to create");
scanf("%d",&n);
create(n);
dispaly();
printf("\n1:add at first\n 2: add at end\n 3: add at any position\n");
printf("4: delete at first\n 5:delete at end\n 6: delete at any position\n 7: reverse traverse");
printf("\nEnter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1: addfirst();
break;
case 2: addlast();
break;
case 3: middle();
break;
case 4:delfirst();
break;
case 5:delend();
break;
case 6:delmiddle();
break;
case 7:reverse();
break;
}
dispaly();
return 0;
}
void create(int n)
{
int i,data;
struct node*c;
head=(struct node*)malloc(sizeof(struct node));
if(head!=NULL)
{
printf("enter data node 1");
scanf("%d",&data);
head->data=data;
head->prev=NULL;
head->next=NULL;
last=head;
for(i=2;i<=n;i++)
{
c=(struct node*)malloc(sizeof(struct node));
if(c!=NULL)
printf("enter data%d",i);
scanf("%d",&data);
c->data=data;
c->prev=last;
c->next=NULL;
last->next=c;
last=c;
}
}
last=last->next;
}
void dispaly()
{
struct node*temp;
//int n=1;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d-",temp->data);
//n++;
temp=temp->next;
}
//temp=temp->next;
// printf("%d",temp->data);
}
}
void addfirst(){
struct node *temp;
struct node*c;
int data;
c=(struct node*)malloc(sizeof(struct node));
printf("enter data for new node");
scanf("%d",&data);
c->data=data;
c->next = head;
head =c;
c->prev=NULL;
}
void addlast()
{
struct node *temp;
struct node*c;
int data;
c=(struct node*)malloc(sizeof(struct node));
printf("enter data for new node");
scanf("%d",&data);
c->data=data;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next= c;
c->next=NULL;
c->prev=temp;s
}
int middle()
{
struct node*temp,*temp1,*c;
int data,i,x;
c=(struct node*)malloc(sizeof(struct node));
printf("enter data for new node");
scanf("%d",&data);
printf("enter position");
scanf("%d",&x);
c->data=data;
c->next=NULL;
temp=head;
for(i=1;i<(x-1);i++)
{
//temp1=temp;
temp=temp->next;
}
c->next=temp->next;
temp->next=c;
c->prev=temp;
}
int delfirst()
{
struct node*temp;
temp=head;
head=head->next;
head->prev=NULL;
free(temp);
return 0;
}
int delend()
{
struct node*temp,*temp1;
temp=head;
temp1=head;
while(temp!=NULL&&temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp1->next=NULL;
temp->prev =NULL;
free(temp);
return 0;
}
int delmiddle()
{
struct node*temp,*temp1;
int x,i;
printf("enter position");
scanf("%d",&x);
temp=head;
temp1=head;
for(i=1;i<x;i++)
{
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
temp->next->prev=temp1;
temp->next=NULL;
temp->prev=NULL;
free(temp);
return 0;
}
void reverse()
{
struct node*temp;
//int n=1;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
while(temp!=NULL)
{
printf("%d-",temp->data);
//n++;
temp=temp->prev;
}
printf("\n")
}
}
Output:-
0 Comments