Doubly Linklist | Data Structure | C Program

Doubly Link-list:


INSERT AT BEGINNING:

struct node *temp1,*temp2;
temp1=(struct node*)malloc(sizeof(struct node));
printf("Enter value to insert:\n");
scanf("%d",&temp1->info);
temp1->next=NULL;
temp1->pre=NULL;
if(start==NULL)
 start=temp1;
else
{
        temp1->next=start;
 start->pre=temp1;
 start=temp1;
}


INSERT AT GIVEN POSITION:

int i,pos;
struct node *temp1,*temp2;
temp1=(struct node*)malloc(sizeof(struct node));
printf("Enter the postion:\n");
scanf("%d",&pos);
printf("Enter value:\n");
scanf("%d",&temp1->info);
temp1->next=NULL;
temp1->pre=NULL;
if(start==NULL)
 start=temp1;
else if(pos==1)
{
 temp1->next=start;
 start->pre=temp1;
 start=temp1;
}
else
{
 temp2=start;
 for(i=1;i<pos-1;i++)
  temp2=temp2->next;
 temp1->next=temp2->next;
 temp2->next=temp1;
 temp1->pre=temp2;
 temp1->next->pre=temp1;
}

INSERT AT END:

struct node *temp1,*temp2;
temp2=start;
temp1=(struct node*)malloc(sizeof(struct node));
printf("Enter to insert:\t");
scanf("%d",&temp1->info);
temp1->next=NULL;
temp1->pre=NULL;
if(start==NULL)
 start=temp1;
else
{
 while(temp2->next!=NULL)
  temp2=temp2->next;
 temp2->next=temp1;
 temp1->pre=temp2;
}

DELETE AT BEGIN:

struct node *temp;
if(start==NULL)
        printf("List is empty!!\n");
else
{
 if(start->next==NULL)
 {
  free(start);
  start=NULL;
 }
 else
 {
  temp=start;
  start=start->next;
  start->pre=NULL;
  free(temp);
 }
}

DELETE AT GIVEN POSITION:

int pos,i;
struct node *temp;
if(start==NULL)
 printf("List is empty!!\n");
else
{
 temp=start;
 printf("Enter the postion:\n");
 scanf("%d",&pos);
 if(start->next==NULL)
 {
  free(start);
  start=NULL;
 } 
 else if(pos==1)
 {
  start=start->next;
  free(temp);
 }
 else
 {
  for(i=1;i<=pos-1;i++)
   temp=temp->next;
  temp->pre->next=temp->next;
  temp->next->pre=temp->pre;
  free(temp);
 }
}

DELETE AT END:

struct node *temp1,*temp2;
if(start==NULL)
 printf("List is empty!!\n");
else
{
 temp1=start;
 if(start->next==NULL)
        {
 free(start);
 start=NULL;
 }
 else
 {
  while(temp1->next!=NULL)
  {
   temp2=temp1;
   temp1=temp1->next;
  }
  free(temp1);
  temp2->next=NULL;
}
}

DISPLAY:

struct node *temp;
if(start==NULL)
 printf("List is empty!!\n");
else
{
 temp=start;
 while(temp!=NULL)
{
 printf("%d\t",temp->info);
 temp=temp->next;
 }
}
-----------------------------------------------------------------

>>C-Program: Circular linklist

Comments

Popular posts from this blog

TOP 10 PROGRAMMING LANGAUAGES COMMING 2021 | ARTICLE

Write a C-program to swap two number:

Print Fibonacci Series using Recursion | C program :