Guest User

Untitled

a guest
Jan 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. /*Linked List*/
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. int count=0;
  5. struct Node *start=NULL;
  6. struct Node
  7. {
  8. int data;
  9. struct Node *next;
  10. };
  11. void insert_at_begin(int x)
  12. {
  13. struct Node *t;
  14. t=(struct Node*)malloc(sizeof(struct Node));
  15. count++;
  16. if(start==NULL)
  17. {
  18. start=t;
  19. start->data=x;
  20. start->next=NULL;
  21. return;
  22. }
  23. t->data=x;
  24. t->next=start;
  25. start=t;
  26. }
  27. void insert_at_end(int x)
  28. {
  29. struct Node *t,*temp;
  30. t=(struct Node*)malloc(sizeof(struct Node));
  31. count++;
  32. if(start==NULL)
  33. {
  34. start=t;
  35. start->data=x;
  36. start->next=NULL;
  37. return;
  38. }
  39. temp=start;
  40. while(temp->next!=NULL)
  41. {
  42. temp=temp->next;
  43. }
  44. temp->next=t;
  45. t->data=x;
  46. t->next=NULL;
  47. }
  48. void delete_from_begin()
  49. {
  50. struct Node *t;
  51. int n;
  52. if(start==NULL)
  53. {
  54. printf("Linked List is empty!!!\n\n");
  55. return;
  56. }
  57. n=start->data;
  58. t=start->next;
  59. free(start);
  60. start=t;
  61. count--;
  62. printf("Deleted element is %d\n\n",n);
  63. return;
  64. }
  65. void delete_from_end()
  66. {
  67. struct Node *t,*u;
  68. int n;
  69. if(start==NULL)
  70. {
  71. printf("Linked List is empty!!!\n\n");
  72. return;
  73. }
  74. count--;
  75. if(start->next==NULL)
  76. {
  77. n=start->data;
  78. free(start);
  79. start=NULL;
  80. printf("Deleted element is %d\n\n",n);
  81. return;
  82. }
  83. t=start;
  84. while(t->next!=NULL)
  85. {
  86. u=t;
  87. t=t->next;
  88. }
  89. n=t->data;
  90. u->next=NULL;
  91. free(t);
  92. printf("Deleted element is %d\n\n",n);
  93. return;
  94. }
  95. void display()
  96. {
  97. struct Node *t;
  98. if(start==NULL)
  99. {
  100. printf("Linked List is empty!!!\n\n");
  101. return;
  102. }
  103. printf("No of elements: %d\n",count);
  104. printf("Elements are: ");
  105. t=start;
  106. while(t->next!=NULL)
  107. {
  108. printf("%d ",t->data);
  109. t=t->next;
  110. }
  111. printf("%d ",t->data);
  112. printf("\n\n");
  113. }
  114. int main()
  115. {
  116. int ch,data;
  117. while(1)
  118. {
  119. printf("---LINKED LIST PROGRAMS---\n");
  120. printf("1. INSERT AT BEGINING\n");
  121. printf("2. INSERT AT END\n");
  122. printf("3. DELETE FROM BEGINING\n");
  123. printf("4. DELETE FROM END\n");
  124. printf("5. DISPLAY LIST\n");
  125. printf("6. EXIT\n\n");
  126. printf("Enter your choice: ");
  127. scanf("%d",&ch);
  128. if(ch==1)
  129. {
  130. printf("Enter the insert value: ");
  131. scanf("%d",&data);
  132. printf("\n");
  133. insert_at_begin(data);
  134. }
  135. else if(ch==2)
  136. {
  137. printf("Enter the insert value: ");
  138. scanf("%d",&data);
  139. printf("\n");
  140. insert_at_end(data);
  141. }
  142. else if(ch==3)
  143. {
  144. delete_from_begin();
  145. }
  146. else if(ch==4)
  147. {
  148. delete_from_end();
  149. }
  150. else if(ch==5)
  151. {
  152. display();
  153. }
  154. else if(ch==6)
  155. {
  156. break;
  157. }
  158. else
  159. {
  160. printf("Wrong choice!!!\n");
  161. }
  162. }
  163. }
Add Comment
Please, Sign In to add comment