Naimul_X

SINGLY_LINKED_LIST

Feb 25th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. struct list{
  4. int data;
  5. struct list *next;
  6.  
  7. };
  8.  
  9.  
  10. typedef struct list node;
  11. node *head,*tail,*prev,*last,*temp;
  12.  
  13. void showMenu()
  14. {
  15. printf("\n1.Insert\n2.Delete\n3.Exit\n");
  16. }
  17.  
  18. node *insertion(node *head,node *tail)
  19. {
  20. node *temp;
  21. int item;
  22. printf("ISERT ITEM: ");
  23. scanf("%d",&item);
  24.  
  25. temp=new node();
  26. temp->data=item;
  27.  
  28. if(head->data>temp->data)
  29. {
  30. temp->next=head;
  31. head=temp;
  32. return head;
  33. }
  34. else if(temp->data>tail->data)
  35. {
  36. node *x;
  37. x=head;
  38. while(x->next!=NULL)
  39. {
  40. x=x->next;
  41. }
  42. x->next=temp;
  43. x=temp;
  44. temp->next=NULL;
  45. return head;
  46. }
  47. else if(head->data<item)
  48. {
  49. node *x;
  50. x=head;
  51. while(x->next->data < item)
  52. {
  53. x=x->next;
  54. }
  55. temp->next=x->next;
  56. x->next=temp;
  57. return head;
  58. }
  59. return head;
  60. }
  61.  
  62. node *deletation(node *head,node *tail)
  63. {
  64. node *temp,*prev;
  65. int item;
  66. printf("DELETE ITEM: ");
  67. scanf("%d",&item);
  68.  
  69. if(head->data==item)
  70. {
  71. temp=head;
  72. head=head->next;
  73. delete(temp);
  74. return head;
  75.  
  76. }
  77.  
  78. else
  79. {
  80.  
  81. temp=head;
  82. while(temp->data != item)
  83. {
  84. prev=temp;
  85. temp=temp->next;
  86. }
  87.  
  88. prev->next=temp->next;
  89.  
  90. delete(temp);
  91. return head;
  92. }
  93.  
  94. return head;
  95.  
  96. }
  97.  
  98. void display(node *head)
  99. {
  100. node *temp;
  101. int len=0;
  102.  
  103. temp=head;
  104.  
  105. while(temp!=NULL)
  106. {
  107. printf("%d ",temp->data);
  108. temp=temp->next;
  109. len++;
  110. }
  111. printf("LENGTH IS: %d\n",len);
  112. printf("\n");
  113.  
  114. }
  115.  
  116.  
  117. int main()
  118. {
  119. int ch,i=1;
  120. head=NULL;
  121.  
  122. printf("DO YOU WANT TO ADD ITEM: ");
  123. scanf("%d",&ch);
  124.  
  125.  
  126.  
  127. while(ch==1)
  128. {
  129.  
  130. if(head==NULL)
  131. {
  132. head=new node();
  133. printf("ENTER DATA: ");
  134. scanf("%d",&head->data);
  135. head->next=NULL;
  136. tail=head;
  137.  
  138. }
  139. else{
  140. temp=new node();
  141. printf("ENTER DATA: ");
  142. scanf("%d",&temp->data);
  143. temp->next=NULL;
  144. tail->next=temp;
  145. tail=temp;
  146. }
  147. printf("DO YOU WANT TO ADD ITEM: ");
  148. scanf("%d",&ch);
  149.  
  150.  
  151. }
  152.  
  153.  
  154. display(head);
  155.  
  156.  
  157. do{
  158. showMenu();
  159. int x;
  160. scanf("%d",&x);
  161.  
  162.  
  163. if(x==1)
  164. {
  165.  
  166. head=insertion(head,tail);
  167. display(head);
  168. }
  169. else if(x==2)
  170. {
  171. head=deletation(head,tail);
  172. display(head);
  173. }
  174. else{
  175. i=0;
  176. }
  177.  
  178.  
  179.  
  180.  
  181. }while(i=1);
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment