Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. struct node *prev;
  6. int info;
  7. struct node *link;
  8. };
  9. struct node * create(struct node *,struct node *);
  10. void display(struct node *,struct node *);
  11. int count(struct node *,struct node *);
  12. struct node * InsBeg(struct node *,struct node *);
  13. struct node * InsEnd(struct node *,struct node *);
  14. struct node * Insbetween(struct node *,struct node *);
  15. struct node * DelBeg(struct node *,struct node *);
  16. struct node * DelEnd(struct node *,struct node *);
  17. struct node * Delbetween(struct node *,struct node *);
  18. void search(struct node *);
  19. int main()
  20. {
  21. system("clear");
  22. struct node *start,*end,*ptr;
  23. int choice,c;
  24. start=(struct node *)malloc(sizeof(struct node));
  25. end=start;
  26. while(1)
  27. {
  28. printf("\n0: Quit");
  29. printf("\n1: Create");
  30. printf("\n2: Display");
  31. printf("\n3: Count");
  32. printf("\n4: Insert at begening");
  33. printf("\n5: Insert at End");
  34. printf("\n6: Insert inbetween a node");
  35. printf("\n7: Delete first node");
  36. printf("\n8: Delete last node");
  37. printf("\n9: Delete inbetween node");
  38. printf("\n10: Search an element");
  39. printf("\n\nEnter your option: ");
  40. scanf("%d",&choice);
  41.  
  42. switch(choice)
  43. {
  44. case 0: return 0;
  45.  
  46. case 1: end=create(start,end);
  47. break;
  48. case 2: display(start,end);
  49. break;
  50. case 3: c=count(start,end);
  51. printf("\nNumber of nodes in linklist= %d",c);
  52. break;
  53. case 4: start = InsBeg(start,end);
  54. ptr=start;
  55. while(ptr->link!=NULL)
  56. ptr=ptr->link;
  57. end=ptr;
  58. break;
  59. case 5: end=InsEnd(start,end);
  60. ptr=end;
  61. while(ptr->prev!=NULL)
  62. ptr=ptr->prev;
  63. start=ptr;
  64. break;
  65. case 6: start=Insbetween(start,end);
  66. ptr=start;
  67. while(ptr->link!=NULL)
  68. ptr=ptr->link;
  69. end=ptr;
  70. break;
  71. case 7: start = DelBeg(start,end);
  72. if(start==NULL)
  73. end=NULL;
  74. break;
  75. case 8: end=DelEnd(start,end);
  76. if(end==NULL)
  77. start=NULL;
  78. break;
  79. case 9: start=Delbetween(start,end);
  80. ptr=start;
  81. while(ptr->link!=NULL)
  82. ptr=ptr->link;
  83. end=ptr;
  84. break;
  85. case 10: search(start);
  86. break;
  87. default: printf("Invalid choice:::");
  88. }
  89. }
  90. }
  91.  
  92. struct node * create(struct node *start,struct node *end)
  93. {
  94. struct node *ptr,*tmp;
  95. int op;
  96. printf("\nEnter node value:");
  97. scanf("%d",&start->info);
  98. start->link=NULL;
  99. start->prev=NULL;
  100. ptr=start;
  101. printf("\nDo you want to add more nodes (1/0) :");
  102. scanf("%d",&op);
  103. while(op!=0)
  104. {
  105. tmp=ptr;
  106. ptr->link=(struct node *)malloc(sizeof(struct node));
  107. ptr=ptr->link;
  108. printf("\nEnter node value:");
  109. scanf("%d",&ptr->info);
  110. ptr->link=NULL;
  111. ptr->prev=tmp;
  112. end=ptr;
  113. printf("\nDo you want to add more nodes (1/0) :");
  114. scanf("%d",&op);
  115. }
  116. return end;
  117. }
  118.  
  119. void display(struct node *start,struct node *end)
  120. {
  121. struct node *ptr,*ptr1;
  122. printf("\nLinklist in forward traversing:\n ");
  123. ptr=start;
  124. while(ptr!=NULL)
  125. {
  126. printf("%d\t",ptr->info);
  127. ptr=ptr->link;
  128. }
  129. printf("\nLinklist in backward traversing:\n");
  130. ptr=end;
  131. while(ptr!=NULL)
  132. {
  133. printf("%d\t",ptr->info);
  134. ptr=ptr->prev;
  135. }
  136. }
  137.  
  138. int count(struct node *start,struct node * end)
  139. {
  140. struct node *ptr;
  141. int c=0;
  142. ptr=start;
  143. while(ptr!=NULL)
  144. {
  145. c++;
  146. ptr=ptr->link;
  147. }
  148. return c;
  149. }
  150.  
  151. struct node * InsBeg(struct node *start,struct node * end)
  152. {
  153. struct node *new1;
  154. new1=(struct node *)malloc(sizeof(struct node));
  155. if(new1==NULL)
  156. {
  157. printf("\nMemory Overflow");
  158. return 0;
  159. }
  160. printf("\nEnter new node value: ");
  161. scanf("%d",&new1->info);
  162.  
  163. if(start==NULL)
  164. {
  165. start=end=new1;
  166. new1->link=NULL;
  167. new1->prev=NULL;
  168. }
  169. else
  170. {
  171. new1->link=start;
  172. start->prev=new1;
  173. new1->prev=NULL;
  174. start=new1;
  175. }
  176.  
  177. return start;
  178. }
  179.  
  180. struct node * InsEnd(struct node *start,struct node * end)
  181. {
  182. struct node *new1,*ptr;
  183. new1=(struct node *)malloc(sizeof(struct node));
  184. if(new1==NULL)
  185. {
  186. printf("\nMemory Overflow");
  187. return 0;
  188. }
  189. printf("\nEnter new node value: ");
  190. scanf("%d",&new1->info);
  191.  
  192. if(end==NULL)
  193. {
  194. start=end=new1;
  195. new1->link=NULL;
  196. new1->prev=NULL;
  197. }
  198. else
  199. {
  200. new1->link=NULL;
  201. end->link=new1;
  202. new1->prev=end;
  203. end=new1;
  204. }
  205.  
  206. return end;
  207. }
  208.  
  209. struct node * Insbetween(struct node *start,struct node * end)
  210. {
  211. struct node *new1,*ptr;
  212. int loc,c=1;
  213. printf("\nEnter node number to insert: ");
  214. scanf("%d",&loc);
  215.  
  216. ptr=start;
  217. while(c<loc-1)
  218. {
  219. ptr=ptr->link;
  220. c++;
  221. }
  222. if(c==1)
  223. start=InsBeg(start,end);
  224. else if(ptr->link==NULL)
  225. end=InsEnd(start,end);
  226. else
  227. {
  228. new1=(struct node *)malloc(sizeof(struct node));
  229. if(new1==NULL)
  230. {
  231. printf("\nMemory Overflow");
  232. return 0;
  233. }
  234. printf("\nEnter new node value: ");
  235. scanf("%d",&new1->info);
  236.  
  237. new1->link=ptr->link;
  238. ptr->link->prev=new1;
  239. ptr->link=new1;
  240. new1->prev=ptr;
  241. }
  242. return start;
  243. }
  244.  
  245. struct node * DelBeg(struct node *start,struct node * end)
  246. {
  247. struct node *ptr;
  248. if(start==NULL)
  249. {
  250. printf("\nUnderflow");
  251. return 0;
  252. }
  253.  
  254. ptr=start;
  255.  
  256. if(start->link==NULL)
  257. {
  258. start=NULL;
  259. end=NULL;
  260. }
  261. else
  262. {
  263. start=start->link;
  264. start->prev=NULL;
  265. }
  266. free(ptr);
  267.  
  268. return start;
  269. }
  270.  
  271. struct node * DelEnd(struct node *start,struct node * end)
  272. {
  273. struct node *prev,*ptr;
  274. if(start==NULL)
  275. {
  276. printf("\nUnderflow");
  277. return 0;
  278. }
  279. ptr=end;
  280. if(start->link==NULL)
  281. {
  282. start=NULL;
  283. end=NULL;
  284. }
  285. else
  286. {
  287. end=end->prev;
  288. end->link=NULL;
  289. }
  290. free(ptr);
  291.  
  292. return end;
  293. }
  294.  
  295. struct node * Delbetween(struct node *start,struct node * end)
  296. {
  297. struct node *prev,*ptr;
  298. int loc,c=1;
  299. if(start==NULL)
  300. {
  301. printf("\nUnderflow");
  302. return 0;
  303. }
  304. printf("\nEnter node number to delete: ");
  305. scanf("%d",&loc);
  306.  
  307. ptr=start;
  308. while(c<loc)
  309. {
  310. ptr=ptr->link;
  311. c++;
  312. }
  313.  
  314. if(c==1)
  315. start=DelBeg(start,end);
  316. else if(ptr->link==NULL)
  317. end=DelEnd(start,end);
  318. else
  319. {
  320. ptr->prev->link = ptr->link;
  321. ptr->link->prev = ptr->prev;
  322. }
  323. free(ptr);
  324. return start;
  325. }
  326.  
  327. void search(struct node *start)
  328. {
  329. struct node *ptr;
  330. int item,c=0;
  331. printf("\nEnter node value you want to search: ");
  332. scanf("%d",&item);
  333.  
  334. ptr=start;
  335. while(ptr != NULL)
  336. {
  337. c++;
  338. if(ptr->info == item)
  339. break;
  340. ptr=ptr->link;
  341. }
  342. if(ptr==NULL)
  343. printf("\nItem not found");
  344. else
  345. printf("\nItem found at node number %d",c);
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement