bishalbiswas

linked list

Oct 21st, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<malloc.h>
  4. #include<string.h>
  5. using namespace std;
  6. void display(void);
  7. void insert_first(void);
  8. void insert_last(void);
  9. void insert_loc(void);
  10. void delete_specific(void);
  11. void delete_first(void);
  12. void delete_last(void);
  13.  
  14. char item[10];
  15. struct node{
  16. char info[10];
  17. struct node* link;
  18. }*start=NULL;
  19. int main()
  20. {
  21. int choice;
  22. do{
  23. printf("\n\n 1)insert first\n 2)insert last\n 3)insert loc\n 4)display\n 5)delete item\n 6)delete first\n 7)delete last\n 8)exit\n");
  24. printf("enter choice:");
  25. scanf("%d",&choice);
  26. switch(choice)
  27. {
  28. case 1:
  29. insert_first();
  30. break;
  31. case 2:
  32. insert_last();
  33. break;
  34. case 3:
  35. insert_loc();
  36. break;
  37. case 4:
  38. display();
  39. break;
  40. case 5:
  41. delete_specific();
  42. break;
  43. case 6:
  44. delete_first();
  45. break;
  46. case 7:
  47. delete_last();
  48. break;
  49. case 8:
  50. printf("\nBYE BYE!!!!");
  51. return 0;
  52. default :
  53. printf("invalid key...please try again!!!");
  54.  
  55. }
  56. }while(1);
  57.  
  58. }
  59. void insert_first(void)
  60. {
  61. int n=1;
  62. char item1[10];
  63. struct node *ptr;
  64. printf("item to be inserted:");
  65. cin>>item1;
  66. if(start==NULL)
  67. {
  68. start=(struct node *)malloc(sizeof(struct node));
  69. strcpy(start->info,item1);
  70. start->link=NULL;
  71.  
  72. }
  73. else
  74. {
  75. ptr=start;
  76. start=(struct node *)malloc(sizeof(struct node));
  77. strcpy(start->info,item1);
  78. start->link=ptr;
  79. }
  80. cout<<"item inserted:"<<item1;
  81. }
  82. void insert_last(void)
  83. {
  84.  
  85. struct node *ptr;
  86. printf("item to be inserted:");
  87. if(start==NULL)
  88. {
  89. start=(struct node *)malloc(sizeof(struct node));
  90. cin>>start->info;
  91. start->link=NULL;
  92.  
  93. }
  94. else
  95. {
  96. ptr=start;
  97. while(ptr->link!=NULL)
  98. ptr=ptr->link;
  99. ptr->link=(struct node *)malloc(sizeof(struct node));
  100. ptr=ptr->link;
  101. cin>>ptr->info;
  102. ptr->link=NULL;
  103. }
  104. printf("item inserted:%s",ptr->info);
  105. }
  106. void insert_loc(void)
  107. {
  108.  
  109. struct node *ptr,*newn;
  110. ptr=start;
  111. newn=start;
  112. if(start==NULL)
  113. cout<<"\nthe list is empty!!!,insert atleast one node...\n";
  114. else
  115. {
  116. cout<<"enter an item after which a node to be inserted:";
  117. cin>>item;
  118. cout<<"\nitem to be inserted:";
  119. while(ptr!=NULL)
  120. {
  121. if(strcmp(ptr->info,item)==0)
  122. {
  123. newn=(struct node *)malloc(sizeof(struct node));
  124. cin>>newn->info;
  125. newn->link=ptr->link;
  126. ptr->link=newn;
  127. break;
  128. }
  129. else
  130. ptr=ptr->link;
  131.  
  132. }
  133. }
  134. cout<<"item inserted:"<<newn->info;
  135. return;
  136. }
  137. void display(void)
  138. {
  139.  
  140. struct node *ptr=start;
  141. int n=1;
  142. if(ptr!=NULL)
  143. {
  144. while(ptr!=NULL)
  145. {
  146. printf("no %d:%s\n",n++,ptr->info);
  147. ptr=ptr->link;
  148. }
  149. }
  150. else
  151. {
  152. printf("List is empty!!!\n");
  153. }
  154.  
  155.  
  156. }
  157.  
  158. void delete_specific(void)
  159. {
  160. struct node *ptr, *prev;
  161. printf("\n\nEnter ITEM which is to be deleted: ");
  162. scanf("%s", &item);
  163. if (start == NULL)
  164. printf("\n\nLinked list is empty!!!.\n");
  165.  
  166. else if (strcmp(start->info,item)==0)
  167. {
  168. ptr = start;
  169. start = start->link;
  170. free(ptr);
  171. }
  172. else
  173. {
  174. ptr = start;
  175. prev = start;
  176. while (ptr != NULL)
  177. {
  178. if (strcmp(ptr->info,item)==0)
  179. {
  180. prev->link = ptr->link;
  181. free(ptr);
  182. printf("\n\nItem deleted: %s", item);
  183. break;
  184. }
  185. else
  186. {
  187. prev = ptr;
  188. ptr = ptr->link;
  189. }
  190. }
  191. printf("\n\nItem deleted: %s", item);
  192. }
  193. }
  194. void delete_first(void)
  195. {
  196.  
  197. struct node *ptr;
  198. ptr=start;
  199. if(start==NULL)
  200. printf("the list is empty.\n");
  201. else
  202. {
  203. strcpy(item,start->info);
  204. start=start->link;
  205. }
  206. printf("item deleted:%s",item);
  207. free(ptr);
  208. }
  209.  
  210. void delete_last()
  211. {
  212. struct node *ptr, *prev;
  213. if (start == NULL)
  214. printf("\n\nLinked list is empty.\n");
  215. else
  216. {
  217. ptr = start;
  218. prev = start;
  219.  
  220. while (ptr->link != NULL)
  221. {
  222. prev = ptr;
  223. ptr = ptr->link;
  224. }
  225. strcpy(item,ptr->info);
  226. if (start->link == NULL)
  227. start = NULL;
  228. else
  229. prev->link = NULL;
  230. free(ptr);
  231. printf("\n\nItem deleted: %s", item);
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment