sandeshMC

Linked_List

Aug 8th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.72 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. struct node {
  4. int data;
  5. struct node *next;
  6. }*start;
  7. void create(int d) {
  8. struct node *q,*temp;
  9. temp= (struct node* )malloc(sizeof(struct node));
  10. temp->data=d;
  11. temp->next=NULL;
  12. if(start==NULL) /*If list is empty */
  13. start=temp;
  14. else { /*Element inserted at the end */
  15. q=start;
  16. while(q->next!=NULL)
  17. q=q->next;
  18. q->next=temp;
  19. }
  20. } //End of create()
  21. void add_beg(int d) {
  22. struct node *temp;
  23. temp=(struct node* )malloc(sizeof(struct node));
  24. temp->data=d;
  25. temp->next=start;
  26. start=temp;
  27. }/*End of add_beg()*/
  28. void add_after(int d,int pos) {
  29. struct node *temp,*q;
  30. int i;
  31. q=start;
  32. for(i=0;i<pos-1;i++) {
  33. q=q->next;
  34. if(q==NULL) {
  35. printf("There are less than %d elements",pos);
  36. return;
  37. }
  38. }
  39. temp=(struct node* )malloc(sizeof(struct node) );
  40. temp->next=q->next;
  41. temp->data=d;
  42. q->next=temp;
  43. }//End of add_after()
  44. void del(int d) {
  45. struct node *temp,*q;
  46. if(start->data == d) {
  47. temp=start;
  48. start=start->next; /*First element deleted*/
  49. return;
  50. }
  51. q=start;
  52. while(q->next->next != NULL) {
  53. if(q->next->data==d) /*Element deleted in between*/
  54. {
  55. temp=q->next;
  56. q->next=temp->next;
  57. return;
  58. }
  59. q=q->next;
  60. }/*End of while */
  61. if(q->next->data==d) /*Last element deleted*/
  62. {
  63. temp=q->next;
  64. q->next=NULL;
  65. return;
  66. }
  67. printf("Element %d not found\n",d);
  68. }//End of del()
  69. void display() {
  70. struct node *q;
  71. if(start == NULL) {
  72. printf("List is empty\n");
  73. return;
  74. }
  75. q=start;
  76. printf("List is :\n");
  77. while(q!=NULL) {
  78. printf("%d ", q->data);
  79. q=q->next;
  80. }
  81. printf("\n");
  82. }//End of display()
  83.  
  84. void count() {
  85. struct node *q=start;
  86. int cnt=0;
  87. while(q!=NULL) {
  88. q=q->next;
  89. cnt++;
  90. }
  91. printf("Number of elements are %d\n",cnt);
  92. }//End of count()
  93.  
  94. void main() {  
  95. int choice,n,e,position,i;
  96. start=NULL;
  97. while(1) {
  98. printf("1.Create a linked List\n");
  99. printf("2.Add at begining\n");
  100. printf("3.Add after \n");
  101. printf("4.Delete\n");
  102. printf("5.Display\n");
  103. printf("6.Count\n");
  104. printf("7.Quit\n");
  105. printf("Enter your choice : ");
  106. scanf("%d",&choice);
  107. switch(choice) {
  108. case 1:printf("How many nodes you want in the list: ");
  109. scanf("%d",&n);
  110. for(i=0;i<n;i++) {
  111. printf("Enter the element : ");
  112. scanf("%d",&e);
  113. create(e);
  114. }
  115. break;
  116. case 2:
  117. printf("Enter the element : ");
  118. scanf("%d",&e);
  119. add_beg(e);
  120. break;
  121. case 3:
  122. printf("Enter the element : ");
  123. scanf("%d",&e);
  124. printf("Enter the position after which this element is inserted : ");
  125. scanf("%d",&position);
  126. add_after(e,position);
  127. break;
  128. case 4:if(start==NULL)
  129. {
  130. printf("List is empty\n");
  131. continue;
  132. }
  133. printf("Enter the element for deletion : ");
  134. scanf("%d",&e);
  135. del(e);
  136. break;
  137. case 5:display();
  138. break;
  139. case 6:count();
  140. break;
  141. case 7: exit(0);
  142. default:
  143. printf("Wrong choice\n");
  144. } //End of switch
  145. }//End of while
  146. }//End of main()
Advertisement
Add Comment
Please, Sign In to add comment