Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Double Linked List in C

#include<stdio.h>
#include<conio.h>
void insertbeg();
void insertend();
void insertmid();
void del();
void disp();
void size();
struct list
{
int data;
struct list *lptr;
struct list *rptr;
};
struct list *head,*temp,*last;
void main()
{
int ch;
struct list *newnode;
clrscr();
while(1)
{
printf("\n 1.Insert at begining");
printf("\n 2.Insert at Middle");
printf("\n 3.Insert at end");
printf("\n 4.Delete");
printf("\n 5.Display");
printf("\n 6.Size");
printf("\n 7.Exit");
printf("\n enter ur choice");
scanf("%d",&ch);
clrscr();
switch(ch)
{
case 1:
      insertbeg();
      break;
case 2:
      insertmid();
      break;
case 3:
      insertend();
      break;
case 4:
      del();
      break;
case 5:
      disp();
      break;
case 6:
       size();
       break;
case 7:
       exit(0);
       default:
       printf("wrongly selected");
}
       }
         }
       void insertbeg()
       { int val;
       struct list *newnode;
       newnode=(struct list *)malloc(sizeof(struct list));
       printf("Enter the value");
       fflush(stdin);
       scanf("%d",&val);
       newnode->data=val;
       newnode->lptr=NULL;
       newnode->rptr=NULL;
       if(head==NULL)
       { head=newnode;
       last=newnode;
       }else
       {  newnode->rptr=head;
       head->lptr=newnode;
      head=newnode;
       }

       }
       void insertend()
       {
          int value;
       struct list *newnode;
       printf("enter the value");
       fflush(stdin);
       scanf("%d",&value);
       newnode=(struct list *)malloc(sizeof(struct list));
       newnode->data=value;
       newnode->rptr=NULL;
       newnode->lptr=NULL;
       if(head==NULL)
       {head=newnode;
       last=newnode;
       }
       else
       {last->rptr=newnode;
       newnode->lptr=last;
       last=newnode;

       }
       }
       void insertmid()
       {int value,value1;


       struct list *newnode;
       printf("enter the value:");
       fflush(stdin);
       scanf("%d",&value);
       newnode=(struct list *)malloc(sizeof(struct list));
       newnode->data=value;
       newnode->prev=NULL;
       newnode->rptr=NULL;
       printf("enter insert after the value\n");
       scanf("%d",&value1);
       temp=head;
       while(temp->data!=value1)
        temp=temp->next;
       newnode->next=temp->next;
       temp->next->prev=newnode;
       temp->next=newnode;
       newnode->prev=temp;

       }
       void del()
       {int value;
       temp=head;
       if(head==NULL)
       printf("list is empty");
       else
       {printf("enter the value to be delete\n");
       scanf("%d",&value);
       if(head->data==value)
       head=head->next;
       else
       {
       while(head->next->data!=value)
       head=head->next;
       if(head->next==NULL)
       printf("the element not present\n");
       else
       if(head->next->next==NULL)
       last=head;
       head->next=head->next->next;
       printf("the element is deleted\n");
       head=temp;
       }
       }
       }


       void size()
       {int count=0;
      temp=head;
      while(temp!=NULL)
      {count++;
      temp=temp->next;
      }
      printf("size of list is %d",count);
     // return 0;
       }
       void disp()
       {  temp=head;
       if(temp==NULL)
       printf("List is empty");
       else
       {
       printf("\n The list is");
       while(temp->next!=NULL)
       {printf("%d<->",temp->data);
       temp=temp->next;
        }
        printf("%d",temp->data);
        }
       }