Advertisement
erfanul007

doubly link list

Oct 31st, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node
  5. {
  6. int data;
  7. struct node* prev;
  8. struct node* next;
  9. };
  10.  
  11. struct node* head = NULL, *end= NULL;
  12.  
  13.  
  14.  
  15. void insert(int x){
  16. struct node* newnode;
  17. newnode = (struct node*) malloc(sizeof(struct node));
  18. newnode -> prev = NULL;
  19. newnode -> data = x;
  20. newnode -> next = NULL;
  21. if(head != NULL)
  22. {
  23. newnode -> next = head;
  24. head -> prev = newnode;
  25. }
  26. else
  27. {
  28. end=newnode;
  29. }
  30. head = newnode;
  31.  
  32. }
  33.  
  34.  
  35. void insert_middle(int x, int pos)
  36. {
  37.  
  38. struct node* newnode,*cur;
  39. newnode = (struct node*) malloc(sizeof(struct node));
  40. newnode -> prev = NULL;
  41. newnode -> data = x;
  42. newnode -> next = NULL;
  43. int index=1;
  44. cur = head;
  45. while(cur->next != NULL && index<pos)
  46. {
  47. cur=cur->next;
  48. index++;
  49. }
  50. if(cur->next != NULL)
  51. {
  52. newnode->next=cur->next;
  53. newnode->prev=cur;
  54. (cur->next)->prev = newnode;
  55. cur->next=newnode;
  56. }
  57. else
  58. {
  59. newnode->prev = head;
  60. cur->next = newnode;
  61. }
  62. }
  63.  
  64.  
  65. void l_insert(int x)
  66. {
  67. struct node* newnode;
  68. newnode = (struct node*) malloc(sizeof(struct node));
  69. newnode -> prev = NULL;
  70. newnode -> data = x;
  71. newnode -> next = NULL;
  72. if(head != NULL)
  73. {
  74. newnode -> prev = end;
  75. end -> next = newnode;
  76. }
  77. else
  78. {
  79. head=newnode;
  80. }
  81. end = newnode;
  82.  
  83. }
  84.  
  85. void delete_1st()
  86. {
  87. struct node* cur = head;
  88. cur = head;
  89. head=head->next;
  90. head->prev = NULL;
  91. free(cur);
  92.  
  93.  
  94. }
  95.  
  96. void Delete_mid()
  97. {
  98. int pos,i;
  99. printf("Enter your position for Delete :- ");
  100. scanf("%d",&pos);
  101. struct node *cur = head;
  102. for(i = 1; i < pos ; i++)
  103. {
  104. cur = cur -> next;
  105. }
  106. if(cur==head){
  107. head=head->next;
  108. head->prev = NULL;
  109. free(cur);
  110. }
  111. else if(cur==end){
  112. end=end->prev;
  113. end->next = NULL;
  114. free(cur);
  115. }
  116. else{
  117. (cur->prev)->next = cur->next;
  118. (cur->next)->prev = cur->prev;
  119. free(cur);
  120. }
  121. }
  122.  
  123.  
  124. void delete_last()
  125. {
  126. struct node* temp=end;
  127. while (temp -> next !=NULL)
  128. {
  129. temp = temp -> next;
  130. }
  131. end = end->prev;
  132. end->next=NULL;
  133. free(temp);
  134. }
  135.  
  136.  
  137.  
  138. void printdata()
  139. {
  140.  
  141. struct node* temp = head;
  142. while(temp != NULL){
  143. printf("%d >--< ", temp -> data);
  144. temp = temp -> next;
  145. }
  146. printf("\n");
  147.  
  148. }
  149.  
  150. int main()
  151. {
  152. int x, t;
  153.  
  154. while(1)
  155. { printf("\t\t\t\tPress(1) for First Insert\n\t\t\t\tPress(2) for last Middle\n\t\t\t\tPress(3) for insert Last\n");
  156. printf("\t\t\t\tPress(4) for delete First\n\t\t\t\tPress(5) for delete Middle\n\t\t\t\tPress(6) for delete last\n");
  157. printf("\nchoose a option :-");
  158. scanf("%d",&t);
  159.  
  160.  
  161.  
  162.  
  163. if(t==1)
  164. {
  165. printf("Input a Data :- ");
  166. scanf("%d", &x);
  167. insert(x);
  168. printdata();
  169. }
  170.  
  171. else if( t == 2)
  172. {
  173. int pos;
  174. printf("Input a Data :- ");
  175. scanf("%d", &x);
  176. printf("Enter position for insert middle :- ");
  177. scanf("%d",&pos);
  178. insert_middle(x,pos);
  179. printdata();
  180. }
  181.  
  182. else if(t==3)
  183. {
  184. printf("Input a Data :- ");
  185. scanf("%d", &x);
  186. l_insert(x);
  187. printdata();
  188. }
  189.  
  190.  
  191. else if(t==4)
  192. {
  193. delete_1st();
  194. printdata();
  195. }
  196.  
  197. else if(t==5)
  198. {
  199. Delete_mid();
  200. printdata();
  201. }
  202.  
  203. else if(t==6)
  204. {
  205. delete_last();
  206. printdata();
  207. }
  208.  
  209. }
  210. return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement