Advertisement
Guest User

khatri aids

a guest
Jan 20th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include <stdio.h>
  3.  
  4.  
  5. void display();
  6.  
  7.  
  8. struct node
  9. {
  10. int info;
  11. int choice;
  12. while(1){
  13.  
  14. printf("\n===================== MENU=================================\n");
  15. void insert_begin();
  16. void insert_end();
  17. void delete_end();
  18. void insert_pos();
  19. void delete_begin();
  20. void delete_pos();
  21.  
  22. printf("\n 1.Display");
  23. printf("\n 2.Insert at the beginning");
  24. printf("\n 3.Insert at the end");ition");
  25. printf("\n 4.Insert at specified pos
  26. printf("\n 5.Delet
  27. struct node *next;
  28. };
  29. struct node *start=NULL;
  30. int main()
  31. {e from beginning");
  32. printf("\n 6.Delete from the end ");
  33. printf("\n 7.Delete from specified position");
  34. printf("\n 8.Exit");
  35. printf("\n--------------------------------------\n");
  36. printf("\nEnter your choice:");
  37. scanf("%d",&choice);
  38. switch(choice)
  39. {
  40.  
  41. case 1:
  42. display();
  43. break;
  44. case 2:
  45. insert_begin();
  46. break;
  47. case 3:
  48. insert_end();
  49. break;
  50. case 4:
  51. insert_pos();
  52. break;
  53. case 5:
  54. delete_begin();
  55. break;
  56. case 6:
  57. delete_end();
  58. break;
  59. case 7:
  60. delete_pos();
  61. break;
  62.  
  63. case 8:
  64. exit(0);
  65. break;
  66.  
  67. default:
  68. printf("\nPlease enter the valid option\n");
  69. break;
  70. }
  71. }
  72. return 0;
  73. }
  74.  
  75. void display()
  76. {
  77. struct node *ptr;
  78. if(start==NULL)
  79. {
  80. printf("\nList is empty:\n");
  81. return;
  82. }
  83. else
  84. {
  85. ptr=start;
  86. printf("\nThe List elements are:\n");
  87. while(ptr!=NULL)
  88. {
  89. printf("%d\t",ptr->info );
  90. ptr=ptr->next ;
  91. }
  92. }
  93. }
  94. void insert_begin()
  95. {
  96. struct node *temp;
  97. temp=(struct node *)malloc(sizeof(struct node));
  98. if(temp==NULL)
  99. {
  100. printf("\nOut of Memory Space:\n");
  101. return;
  102. }
  103. printf("\nEnter the data value for the node:\t" );
  104. scanf("%d",&temp->info);
  105. temp->next =NULL;
  106. if(start==NULL)
  107. {
  108. start=temp;
  109. }
  110. else
  111. {
  112. temp->next=start;
  113. start=temp;
  114. }
  115. }
  116. void insert_end()
  117. {
  118. struct node *temp,*ptr;
  119. temp=(struct node *)malloc(sizeof(struct node));
  120. if(temp==NULL)
  121. {
  122. printf("\nOut of Memory Space:\n");
  123. return;
  124. }
  125. printf("nEnter the data value for the node:t" );
  126. scanf("%d",&temp->info );
  127. temp->next =NULL;
  128. if(start==NULL)
  129. {
  130. start=temp;
  131. }
  132. else
  133. {
  134. ptr=start;
  135. while(ptr->next !=NULL)
  136. {
  137. ptr=ptr->next ;
  138. }
  139. ptr->next =temp;
  140. }
  141. }
  142. void insert_pos()
  143. {
  144. struct node *ptr,*temp;
  145. int i,pos;
  146. temp=(struct node *)malloc(sizeof(struct node));
  147. if(temp==NULL)
  148. {
  149. printf("nOut of Memory Space:n");
  150. return;
  151. }
  152. printf("nEnter the position for the new node to be inserted:t");
  153. scanf("%d",&pos);
  154. printf("nEnter the data value of the node:t");
  155. sca nf("%d",&temp->info) ;
  156.  
  157. temp->next=NULL;
  158. if(pos==0)
  159. {
  160. temp->next=start;
  161. start=temp;
  162. }
  163. else
  164. {
  165. for(i=0,ptr=start;i<pos-1;i++) { ptr=ptr->next;
  166. if(ptr==NULL)
  167. {
  168. printf("nPosition not found:[Handle with care]n");
  169. return;
  170. }
  171. }
  172. temp->next =ptr->next ;
  173. ptr->next=temp;
  174. }
  175. }
  176. void delete_begin()
  177. {
  178. struct node *ptr;
  179. if(ptr==NULL)
  180. {
  181. printf("nList is Empty:n");
  182. return;
  183. }
  184. else
  185. {
  186. ptr=start;
  187. start=start->next ;
  188. printf("\nThe deleted element is :%d\t",ptr->info);
  189. free(ptr);
  190. }
  191. }
  192. void delete_end()
  193. {
  194. struct node *temp,*ptr;
  195. if(start==NULL)
  196. {
  197. printf("nList is Empty:");
  198. exit(0);
  199. }
  200. else if(start->next ==NULL)
  201. {
  202. ptr=start;
  203. start=NULL;
  204. printf("\nThe deleted element is:%d\t",ptr->info);
  205. free(ptr);
  206. }
  207. else
  208. {
  209. ptr=start;
  210. while(ptr->next!=NULL)
  211. {
  212. temp=ptr;
  213. ptr=ptr->next;
  214. }
  215. temp->next=NULL;
  216. printf("\nThe deleted element is:%d\t",ptr->info);
  217. free(ptr);
  218. }
  219. }
  220. void delete_pos()
  221. {
  222. int i,pos;
  223. struct node *temp,*ptr;
  224. if(start==NULL)
  225. {
  226. printf("\nThe List is Empty:\n");
  227. exit(0);
  228. }
  229. else
  230. {
  231. printf("\nEnter the position of the node to be deleted:\t");
  232. scanf("%d",&pos);
  233. if(pos==0)
  234. {
  235. ptr=start;
  236. start=start->next ;
  237. printf("\nThe deleted element is:%d\t",ptr->info );
  238. free(ptr);
  239. }
  240. else
  241. {
  242. ptr=start;
  243. for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;
  244. if(ptr==NULL)
  245. {
  246. printf("\nPosition not Found:\n");
  247. return;
  248. }
  249. }
  250. temp->next =ptr->next ;
  251. printf("\nThe deleted element is:%d\t",ptr->info );
  252. free(ptr);
  253. }
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement