Full width home advertisement

HTML

Tech News

Ad

  1. /* creation of Doubly linked list & Operations On Doubly Linked List
  2. ->insertion
  3. ->at begning
  4. ->at end
  5. ->at specific position
  6. ->after specific position
  7. -> Deletion
  8. ->at begning
  9. ->at end
  10. ->at specific position
  11. ->after specific position
  12. ->find the length of list
  13. ->reverce the list
  14. // Created By SMIT
  15. */
  16.  
  17. #include<stdio.h>
  18. #include<conio.h>
  19. #include<stdlib.h>
  20.  
  21.  
  22. struct node{
  23. int data;
  24. struct node *next;
  25. struct node *prev;
  26. };
  27. struct node *head=0,*newnode,*temp,*tail;
  28. void CreateDoubly(); //creates Doubly Link List
  29. void DisplayDoubly(); //Display Doubly Link List
  30. void InsAtBeg(); //Insert At the begning Of list
  31. void InsAtPos(); //Insert At Given position in List
  32. void InsAfterPos(); //Insert After Given Position In List
  33. void InsAtEnd(); //Insert At the End Of the list
  34. void DelAtBeg(); //Delete At the Begning Of the List
  35. void DelAtPos(); //Delete At given Position in List
  36. void DelAfterPos(); //Delete After Given Positon In list
  37. void DelAtEnd(); //Delete At the End Of List
  38. void DoublyLength(); //find the length of Doubly link list
  39. void ReverseDoubly(); //Reverse The Link List
  40.  
  41. //Main
  42. int main(){
  43. int choice;
  44. //While Loop For Selecting Choice It will exit while user type 13
  45. while(choice!=13){
  46. printf("\n\n*****Main Menu*****\n");
  47. printf("\n1. Create");
  48. printf("\n2. Display");
  49. printf("\n3. insert at Begning");
  50. printf("\n4. insert at position");
  51. printf("\n5. insert after position");
  52. printf("\n6. insert at End");
  53. printf("\n7. Delete At Begning");
  54. printf("\n8. Delete At Position");
  55. printf("\n9. Delete After Position");
  56. printf("\n10. Delete At End");
  57. printf("\n11. Find the length of list");
  58. printf("\n12. Reverse Doubly Link List");
  59. printf("\n13. exit\n");
  60. printf("\nEnter Choice ");
  61. scanf("%d",&choice);
  62.  
  63. //Switch case according To the Selection Of user
  64. switch(choice){
  65. case 1:
  66. CreateDoubly();
  67. break;
  68. case 2:
  69. DisplayDoubly();
  70. break;
  71. case 3:
  72. InsAtBeg();
  73. break;
  74. case 4:
  75. InsAtPos();
  76. break;
  77. case 5:
  78. InsAfterPos();
  79. break;
  80. case 6:
  81. InsAtEnd();
  82. break;
  83. case 7:
  84. DelAtBeg();
  85. break;
  86. case 8:
  87. DelAtPos();
  88. break;
  89. case 9:
  90. DelAfterPos();
  91. break;
  92. case 10:
  93. DelAtEnd();
  94. break;
  95. case 11:
  96. DoublyLength();
  97. break;
  98. case 12:
  99. ReverseDoubly();
  100. break;
  101. case 13:
  102. exit(0);
  103. break;
  104. default:
  105. printf("Error Please Try Again");
  106. break;
  107. }
  108.  
  109. }
  110. return 0;
  111. }
  112.  
  113. //This Function will Create Doubly Link List
  114. void CreateDoubly(){
  115. int choice=1;
  116. while(choice){
  117. newnode=(struct node *)malloc(sizeof(struct node));
  118. printf("Enter Data:");
  119. scanf("%d",&newnode->data);
  120. newnode->next=0;
  121. newnode->prev=0;
  122. if(head==0){
  123. head=temp=newnode;
  124. }
  125. else{
  126. temp->next=newnode;
  127. newnode->prev=temp;
  128. temp=newnode;
  129. tail=newnode;
  130. }
  131. printf("Press 0 to exit Press 1 To Add ");
  132. scanf("%d",&choice);
  133. }
  134. }
  135. //This Function Will Display Doubly Link List
  136. void DisplayDoubly(){
  137. temp=head;
  138. while(temp!=0){
  139. printf("\n%d",temp->data);
  140. temp=temp->next;
  141. }
  142. }
  143.  
  144. //This Function will Insert in doubly link list at begingng
  145. void InsAtBeg(){
  146. temp=head;
  147. newnode=(struct node *)malloc(sizeof(struct node));
  148. printf("Enter Data: ");
  149. scanf("%d",&newnode->data);
  150. temp->prev=newnode;
  151. newnode->next=temp;
  152. newnode->prev=0;
  153. head=newnode;
  154. printf("\nNode Created Successfully");
  155. }
  156. //This Function will Insert in doubly link list at Given position
  157. void InsAtPos(){
  158. temp=head;
  159. int pos,i;
  160. printf("Enter position ");
  161. scanf("%d",&pos);
  162. for(i=1;i<pos-1;i++){
  163. temp=temp->next;
  164. }
  165. newnode=(struct node *)malloc(sizeof(struct node));
  166. printf("Enter Data ");
  167. scanf("%d",&newnode->data);
  168. newnode->next=temp->next;
  169. newnode->prev=temp;
  170. temp->next->prev=newnode;
  171. temp->next=newnode;
  172. printf("\nNode Inserted successfully");
  173. }
  174. //This Function will Insert in doubly link list after Given position
  175. void InsAfterPos(){
  176. temp=head;
  177. int i,pos;
  178. printf("Insert Position ");
  179. scanf("%d",&pos);
  180. for(i=1;i<pos;i++){
  181. temp=temp->next;
  182. }
  183. newnode=(struct node *)malloc(sizeof(struct node));
  184. printf("Enter Data");
  185. scanf("%d",&newnode->data);
  186. newnode->next=temp->next;
  187. newnode->prev=temp;
  188. temp->next->prev=newnode;
  189. temp->next=newnode;
  190. printf("\nNode created Successfully");
  191. }
  192. //This Function will Insert in doubly link list at End
  193. void InsAtEnd(){
  194. newnode=(struct node *)malloc(sizeof(struct node));
  195. printf("Enter data ");
  196. scanf("%d",&newnode->data);
  197. newnode->next=0;
  198. newnode->prev=tail;
  199. tail->next=newnode;
  200. tail=newnode;
  201. printf("\nNode Inserted Successfully");
  202. }
  203.  
  204. //This function will delete the node at begning
  205. void DelAtBeg(){
  206. temp=head;
  207. head=temp->next;
  208. head->prev=0;
  209. free(temp);
  210. printf("\nNode Deleted Successfully");
  211. }
  212.  
  213. //This function will delete the node at Given Position
  214. void DelAtPos(){
  215. temp=head;
  216. int i,pos;
  217. printf("Enter Position ");
  218. scanf("%d",&pos);
  219. for(i=1;i<pos;i++){
  220. temp=temp->next;
  221. }
  222. temp->prev->next=temp->next;
  223. temp->next->prev=temp->prev;
  224. free(temp);
  225. printf("\nNode Deleted Successfully");
  226. }
  227.  
  228. //This function will delete the node after given position
  229. void DelAfterPos(){
  230. temp=head;
  231. int i,pos;
  232. printf("Enter Position ");
  233. scanf("%d",&pos);
  234. for(i=1;i<pos+1;i++){
  235. temp=temp->next;
  236. }
  237. temp->prev->next=temp->next;
  238. temp->next->prev=temp->prev;
  239. free(temp);
  240. printf("\nNode Deleted Successfully");
  241. }
  242.  
  243. //This function will delete the node at End
  244. void DelAtEnd(){
  245. tail=tail->prev;
  246. free(tail->next);
  247. tail->next=0;
  248. printf("\nNode Deleted Successfully");
  249. }
  250.  
  251. //This Function will find the length of List
  252. void DoublyLength(){
  253. temp=head;
  254. int count=1;
  255. while(temp->next!=0){
  256. temp=temp->next;
  257. count++;
  258.  
  259. }
  260. printf("The length Of Doubly Link List is %d",count);
  261. }
  262.  
  263.  
  264. //This Function will reverse the List
  265. void ReverseDoubly(){
  266. struct node *nextnode;
  267. temp=head;
  268. while(temp!=0){
  269. nextnode=temp->next;
  270. temp->next=temp->prev;
  271. temp->prev=nextnode;
  272. temp=nextnode;
  273. }
  274. temp=head;
  275. head=tail;
  276. tail=temp;
  277. printf("Link Reversed Successfully");
  278. }
  279.  
  280. //that's it This is all about Doubly link list
  281. //your logic may be differ

No comments:

Post a Comment

Bottom Ad [Post Page]