Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *address;
  7. };
  8. struct node *createnode(int item)
  9. {
  10. struct node *p;
  11. p=(struct node *)malloc(sizeof(struct node));
  12. p->data=item;
  13. p->address=NULL;
  14. return p;
  15. };
  16. struct node *insertbeg(struct node *n,struct node *head)
  17. {
  18. if(head==NULL)
  19. {
  20. head=n;
  21. }
  22. else
  23. {
  24. n->address=head;
  25. head=n;
  26. return head;
  27. }
  28. };
  29. struct node *insertmid(struct node *n,struct node *head,int pos)
  30. {
  31. int i;
  32. struct node *p;
  33. i=1;
  34. p=head;
  35. while(p->address!=NULL&&i<pos-1)
  36. {
  37. p=p->address;
  38. i++;
  39. }
  40. n->address=p->address;
  41. p->address=n;
  42. };
  43. struct node *insertend(struct node *n,struct node *head)
  44. {
  45. struct node *p;
  46. if(head==NULL)
  47. {
  48. head=n;
  49. }
  50. else
  51. {
  52. p=head;
  53. while(p->address!=NULL)
  54. {
  55. p=p->address;
  56. }
  57. p->address=n;
  58. }
  59. return head;
  60. };
  61. struct node *delbeg(struct node *head)
  62. {
  63. if(head==NULL)
  64. {
  65. printf("empty");
  66. }
  67. else
  68. {
  69. head=head->address;
  70. }
  71. return head;
  72. };
  73. struct node *delmid(struct node *head,int data)
  74. {
  75. if(head==NULL)
  76. {
  77. printf("empty");
  78. }
  79. else
  80. {
  81. struct node *c,*p;
  82. c=p=head;
  83. while(p->data!=data&&p!=NULL)
  84. {
  85. c=p;
  86. p=p->address;
  87. }
  88. if(p!=NULL)
  89. {
  90. c->address=p->address;
  91. }
  92. else
  93. {
  94. printf("no data found");
  95. }
  96. }
  97. };
  98. struct node *delend(struct node *head)
  99. {
  100. struct node *p,*c;
  101. if(head==NULL)
  102. {
  103. printf("empty");
  104. }
  105. else
  106. {
  107. c=p=head;
  108. while(p->address!=NULL)
  109. {
  110. c=p;
  111. p=p->address;
  112. }
  113. c->address=NULL;
  114. }
  115. return head;
  116. };
  117. void traversal(struct node *head);
  118. void main()
  119. {
  120. int choice,a,po;
  121. struct node *head=NULL,*p;
  122. while(1)
  123. {
  124. printf("\nenter the choice :\n 1.insert at the begining \n2.insert at a position\n3.insert at the end\n4.delete at the begining \n5.delete at a position \n6.delete at the end \n7.trversal\n");
  125. scanf("%d",&choice);
  126. switch(choice)
  127. {
  128. case 1:
  129. {
  130. printf("you are at option 1.insert at the begining\n");
  131. printf("enter the data\n");
  132. scanf("%d",&a);
  133. p=createnode(a);
  134. head=insertbeg(p,head);
  135. break;
  136. }
  137. case 2:
  138. {
  139. printf("you are at choice 2.insert at a position\n");
  140. printf("enter the data\n");
  141. scanf("%d",&a);
  142. printf("enter the position to be inserted");
  143. scanf("%d",&po);
  144. p=createnode(a);
  145. head=insertmid(p,head,po);
  146. break;
  147. }
  148. case 3:
  149. {
  150. printf("you are at choice 3.insert at the end\n");
  151. printf("enter the data\n");
  152. scanf("%d",&a);
  153. p=createnode(a);
  154. head=insertend(p,head);
  155. break;
  156. }
  157. case 4:
  158. {
  159. printf("you are at choice 4.delete at the begining");
  160. head=delbeg(head);
  161. break;
  162. }
  163. case 5:
  164. {
  165. printf("you are at choice 5.delete at a position");
  166. printf("enter the data");
  167. scanf("%d",&a);
  168. head=delmid(head,a);
  169. break;
  170. }
  171. case 6:
  172. {
  173. printf("you are at choice 6.delete at the end");
  174. head=delend(head);
  175. break;
  176. }
  177. case 7:
  178. {
  179. printf("you are at choice 7.trversal\n");
  180. struct node *p;
  181. if(head==NULL)
  182. {
  183. printf("empty");
  184. }
  185. else
  186. {
  187. p=head;
  188. while(p!=NULL)
  189. {
  190. printf("%d\n",p->data);
  191. p=p->address;
  192. }
  193. }
  194. break;
  195. }
  196. default:
  197. {
  198. printf("you have entered the wrong option ");
  199. break;
  200. }
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement