Advertisement
Guest User

muel

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