DS | Practical - 9 | C Program to Perform Operations on The Doubly Linked List

 

Write a C Program to Perform Operations on The Doubly Linked List

GTU DS Practical-9

Write a program to implement the following operations on the doubly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Delete a last node of the linked list.
(d) Delete a node before a specified position.

Code for Doubly Linked List

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int info;
  6. struct node *lptr,*rptr;
  7. }*l,*r;
  8. void doubins(int);
  9. void doubdel(int);
  10. void display();
  11. int chk;
  12. void main()
  13. {
  14. int ch,x;
  15. l=NULL;
  16. r=NULL;
  17. do
  18. {
  19. printf("\n Press:=>\n");
  20. printf("\n 1.Insert Node");
  21. printf("\n 2.Delete Node");
  22. printf("\n 3.Display Doubly Linked List");
  23. printf("\n 4.Exit");
  24. printf("\n Enter Choice: ");
  25. scanf("%d",&ch);
  26. switch(ch)
  27. {
  28. case 1:
  29. printf("\n\t Enter Element: ");
  30. scanf("%d",&x);
  31. doubins(x);
  32. display();
  33. break;
  34. case 2:
  35. printf("\n\t Enter Element which you want to Delete: ");
  36. scanf("%d",&x);
  37. chk=0;
  38. if(l!=NULL) //if(r!=NULL)
  39. {
  40. printf("\n\n Before Deletion:=>");
  41. printf("\n----------------");
  42. display();
  43. }
  44. doubdel(x);
  45. if(chk==0) //if(r!=NULL)
  46. {
  47. printf("\n\n\n\n\n After Deletion:=>");
  48. printf("\n---------------");
  49. display();
  50. }
  51. break;
  52. case 3:
  53. display();
  54. break;
  55. case 4:
  56. exit(0);
  57. default:
  58. printf("\n\t Invalid Choice.\n\tTry Again.");
  59. }
  60. }while(ch!=4);
  61. }
  62. void doubins(int x)
  63. {
  64. struct node *New,*temp;
  65. New=(struct node *)malloc(sizeof(struct node));
  66. New->info=x;
  67. if(l==NULL) //if(r==NULL)
  68. {
  69. New->lptr=NULL;
  70. New->rptr=NULL;
  71. l=New;
  72. r=New;
  73. return;
  74. }
  75. if(x<=l->info) //If the Node is less than all the Nodes
  76. {
  77. New->lptr=NULL;
  78. New->rptr=l;
  79. l->lptr=New;
  80. l=New;
  81. return;
  82. }
  83. temp=l; //If the Node is inserted in between two nodes
  84. while(temp->info<x && temp!=NULL)
  85. temp=temp->rptr;
  86. if(temp!=NULL)
  87. {
  88. New->lptr=temp->lptr;
  89. New->rptr=temp;
  90. temp->lptr=New;
  91. New->lptr->rptr=New;
  92. return;
  93. }
  94. New->rptr=NULL; //Node is inserted at last
  95. New->lptr=r;
  96. r->rptr=New;
  97. r=New;
  98. }
  99. void doubdel(int x)
  100. {
  101. struct node *temp;
  102. if(l==NULL) //if(r==NULL)
  103. {
  104. printf("\n\n\tDoubly Linked List Underflow on Delete.");
  105. chk=1;
  106. return;
  107. }
  108. if(x==l->info) //If First Node is tobe Deleted
  109. {
  110. temp=l;
  111. l=l->rptr;
  112. l->lptr=NULL;
  113. free(temp);
  114. return;
  115. }
  116. if(x==r->info) //If Last Node is tobe Deleted
  117. {
  118. temp=r;
  119. r=r->lptr;
  120. r->rptr=NULL;
  121. free(temp);
  122. return;
  123. }
  124. temp=l;
  125. while(temp->info!=x && temp!=NULL)
  126. temp=temp->rptr;
  127. if(temp==NULL) //If Node not found in the List
  128. {
  129. /* gotoxy(1,13);
  130. delline();
  131. gotoxy(1,13);
  132. delline();
  133. gotoxy(1,14);
  134. delline();
  135. gotoxy(1,16);
  136. delline();
  137. gotoxy(1,17);
  138. delline();
  139. gotoxy(1,18);
  140. delline();
  141. gotoxy(10,13);
  142. printf("\n\tNode not found.");
  143. chk=1;
  144. return; */
  145. }
  146. temp->lptr->rptr=temp->rptr;
  147. temp->rptr->lptr=temp->lptr;
  148. free(temp);
  149. return;
  150. }
  151. void display()
  152. {
  153. struct node *temp;
  154. if(l==NULL) //if(r==NULL)
  155. printf("\n\n\tDoubly Linked List is Empty.");
  156. else
  157. {
  158. printf("\n\nDoubly Linked List:\n\n\n");
  159. printf("l = %u\n\n",l);
  160. temp=l;
  161. while(temp!=NULL)
  162. {
  163. printf("[%u|%u|%d|%u]->",temp,temp->lptr,temp->info,temp->rptr);
  164. temp=temp->rptr;
  165. }
  166. printf("NULL");
  167. printf("\n\nr = %u",r);
  168. }
  169. }

 

Go to:

Home Page :

Practical 10:

Comments