Skip to main content

Doubly Linked List in C Programming

/*Operations on Doubly linked list */
#include < stdio.h>
#include < conio.h>

typedef struct  node
 {
       int  data;
       struct node *next,*prev;

  }node;
node *start=NULL;
node *create(node *);
node *display(node *);
node *insert_beg(node *);
node *insert_end(node *);
node *insert_before(node *);
node *delete_beg(node *);
node *delete_end(node *);
node *delete_node(node *);
node *delete_after(node *);
node *delete_list(node *);

main()
 {
 int choice;
 clrscr();
 do
   {
  printf("\n1)Create a List.\n2)display\n3)Insert at beg\n4)Insert at end \n5)Insert before given node\n6)Delete from beg\n7)Delete from end\n8)Delete given node\n9)Delete node after given node\n10)Delete list");
  printf("\n11)Quit");
  printf("\nEnter your Choice : ");
  scanf("%d",&choice);

  switch(choice)
    {
   case 1: start=create(start);
    printf("\n Linked List created.");
    break;
   case 2: start = display(start);
    break;
   case 3: start = insert_beg(start);
    break;
   case 4: start = insert_end(start);
    break;
   case 5: start = insert_before(start);
    break;
   case 6: start = delete_beg(start);
    break;
   case 7: start = delete_end(start);
    break;
   case 8: start = delete_node(start);
    break;
   case 9: start = delete_after(start);
    break;
   case 10: start = delete_list(start);
    break;
  }
 }while(choice!=11);
 getch();
 return 0;
   }

node *create(node *start)
  {
 int val,n,i;
 node *new_node;
 printf("\n Enter no of data");
 scanf("%d",&n);
 for(i=0;i< n;i++)
    {
  printf("\n Next Data : ");
  scanf("%d",&val);
  if(start==NULL) // inserting the first node
       {
         start=(node *) malloc(sizeof(node));
         start->prev = NULL;
         start->data = val;
         start->next=NULL;
       }
  else
       {
   new_node=(node *) malloc(sizeof(node));
   new_node->prev = NULL;
   new_node->data=val;
   new_node->next=start;
   start->prev = new_node;
   start = new_node;
      }
    }

  return(start);
  }


node *display(node *start)
 {
 node *ptr;
 printf("\n Data stored in the Doubly linked list : ");
 ptr=start;
 printf("\n");




 if(start==NULL)
   printf("List is empty.\n");
 else
 {
   while(ptr != NULL)
   {
    printf("\t %d", ptr->data);
    ptr = ptr->next;
   }
 }
 return(start);
 }

node *insert_beg(node *start)
 {
 node *new_node;
 int no;
 printf("\n Enter data: ");
 scanf("%d",&no);
 new_node=(node*)malloc(sizeof(node));
 start->prev=new_node;
 new_node->next = start;
 new_node->prev=NULL;
 new_node->data = no;
 start = new_node;

 return(start);
 }

 node *insert_end(node *start)
 {
 node *new_node,*ptr;
 int no;
 printf("\n Enter data: ");
 scanf("%d",&no);
 new_node=(node*)malloc(sizeof(node));
 new_node->data = no;
 new_node->next = NULL;
 if(start==NULL)
 {
    new_node->prev=NULL;
    start=new_node;
 }
 else
 {  ptr=start;
    while(ptr->next != NULL)
        ptr=ptr->next;

 ptr->next=new_node;
 new_node->prev=ptr;
 }
 return(start);
 }
  node *insert_before(node *start)
 {
 node *new_node,*ptr;
 int val,no;
 if(start==NULL)
    start =insert_beg(start);
 else
 {
 printf("\n Enter data to insert: ");
 scanf("%d",&val);
 printf("\n Enter value before which data has to be inserted: ");
 scanf("%d",&no);
 new_node=(node*)malloc(sizeof(node));
 new_node->data = val;

 ptr=start;
 while(ptr->data != no)
     ptr = ptr->next;

 new_node->next = ptr;
 ptr->prev->next=new_node;
 ptr->prev=new_node;
       }
 return(start);
 }

 node *delete_beg(node *start)
 {
 node *p;

 p=start;
 if(p==NULL)
   printf("\n List is empty.");
 else
 {
   start=start->next;
   start->prev=NULL;
   free(p);
 }

 return(start);
 }
node *delete_end(node *start)
 {
 node *p;

 p=start;
 if(p==NULL)
   printf("\n list is empty.");
 else
 {
    while(p->next != NULL)
        p=p->next;

    p->prev->next=NULL;
    free(p);
 }
 return(start);
 }

node *delete_node(node *start)
 {
 node *ptr;
 int val;
 if(start==NULL)
    printf("\n List is empty.");
 else
 {
     printf("\n Enter value of node to be deleted; ");
     scanf("%d",&val);

     ptr=start;
     if(ptr->data == val)
     {
        start=delete_beg(start);
        return(start);
     }
     else
     {
        while(ptr->data != val)
        ptr=ptr->next;

        ptr->prev->next = ptr->next;
        ptr->next->prev = ptr->prev;
        free(ptr);
        return(start);
      }
 }
 }

 node *delete_after(node *start)
 {
 node *ptr,*p;
 int val;
 if(start==NULL)
    printf("\n List is empty.");
 else
 {
    printf("\n Enter value after which node has to be deleted: ");
    scanf("%d",&val);
    ptr=start;

    while(ptr->data != val)
       ptr=ptr->next;

    p = ptr->next;
    ptr->next = p->next;
    p->next->prev = ptr;
    free(p);
    return(start);
  }
 }
node *delete_list(node *start)
 {
  if(start==NULL)
      printf("\n List is empty.");
  else
  {
    while(start != NULL)
      start = delete_beg(start);
  }
  return(start);
 }


OUTPUT
1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 1

 Enter no of data 2

 Next Data : 11

 Next Data : 22

 Linked List created.
1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 4

 Enter data: 33



1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 5

 Enter data to insert: 11

 Enter value before which data has to be inserted: 22

1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 2

 Data stored in the Doubly linked list :
         0       11      22      11      33
1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 6

1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 7

1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 8

 Enter value of node to be deleted; 22

1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 9

 Enter value after which node has to be deleted: 11

1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 2

 Data stored in the Doubly linked list :
         11
1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 10

1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice : 2

 Data stored in the Doubly linked list :
List is empty.

1)Create a List.
2)display
3)Insert at beg
4)Insert at end
5)Insert before given node
6)Delete from beg
7)Delete from end
8)Delete given node
9)Delete node after given node
10)Delete list
11)Quit
Enter your Choice :


Comments

Popular posts from this blog

MVT (Multiprogramming Variable Task) in C Programming

#include #include void main() { int i,os_m,nPage,total,pg[25]; clrscr(); printf("\nEnter total memory size:"); scanf("%d",&total); printf("\nEnter memory for OS:"); scanf("%d",&os_m); printf("\nEnter no. of pages:"); scanf("%d",&nPage); for(i=0;i =pg[i]) { printf("\n Allocate page %d",i+1); total=total-pg[i]; } else printf("\n page %d is not allocated due to insufficient memory.",i+1); } printf("\n External Fragmentation is:%d",total); getch(); } OUTPUT Enter total memory size:1024 Enter memory for OS:256 Enter no. of pages:4 Enter size of page[1]:128 Enter size of page[2]:512 Enter size of page[3]:64 Enter size of page[4]:512 Allocate page 1 Allocate page 2 Allocate page 3 page 4 is not allocated due to insufficient memory. External Fragmentation is:64

First Come First Serve (FCFS) Page replacement algorithm in C Programming

#include #include int fsize; int frm[15]; void display(); void main() { int pg[100],nPage,i,j,pf=0,top=-1,temp,flag=0; clrscr(); printf("\n Enter frame size:"); scanf("%d",&fsize); printf("\n Enter number of pages:"); scanf("%d",&nPage); for(i=0;i OUTPUT Enter frame size:3 Enter number of pages:12 Enter page[1]:1 Enter page[2]:2 Enter page[3]:3 Enter page[4]:4 Enter page[5]:1 Enter page[6]:2 Enter page[7]:5 Enter page[8]:1 Enter page[9]:2 Enter page[10]:3 Enter page[11]:4 Enter page[12]:5 page | Frame content -------------------------------------- 1 | 1 -1 -1 2 | 1 2 -1 3 | 1 2 3 4 | 4 2 3 1 | 4 1 3 2 | 4 1 2 5 | 5 1 2 1 | 5 1 2 2 | 5 1 2 3 | 5 3 2 4 | 5 3 4 5 | 5 3 4 ---------------------------...

Deadlock Prevention using Banker’s Algorithm in C Programming

#include #include void main() { int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15]; int pno,rno,i,j,prc,count,t,total; count=0; clrscr(); printf("\n Enter number of process:"); scanf("%d",&pno); printf("\n Enter number of resources:"); scanf("%d",&rno); for(i=1;i OUTPUT Enter number of process:5 Enter number of resources:3 Enter total numbers of each resources:10 5 7 Enter Max resources for each process: for process 1:7 5 3 for process 2:3 2 2 for process 3:9 0 2 for process 4:2 2 2 for process 5:4 3 3 Enter allocated resources for each process: for process 1:0 1 0 for process 2:3 0 2 for process 3:3 0 2 for process 4:2 1 1 for process 5:0 0 2 available resources: 2 3 0 Allocated matrix Max need 0 1 0| 7 5 3| 7 4 3 3 0 2| 3 2 2| 0 2 0 3 0 2| 9 0 2| 6 0 0 2 1 1| ...