sujonshekh

stack linklist

Apr 10th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6. int data;
  7. struct node *link;
  8. };
  9. struct node *top=NULL,*temp;
  10. void Display();
  11. void push(int vv);
  12. void pop();
  13. void count();
  14. int main()
  15. {
  16. int c,v;
  17. while(1)
  18. {
  19. printf("\n -----Menu-----\n");
  20. printf("\n press 1 for Push\n");
  21. printf("\n press 2 for pop\n");
  22. printf("\n press 3 for Display\n");
  23. printf("\n press 4 for count\n");
  24. printf ("\n press 5 for Exit\n");
  25. printf ("\n Enter Your Choice\n");
  26. scanf("%d",&c);
  27. switch(c)
  28. {
  29. case 1:printf ("\n choice=push\n");
  30. printf("\n Enter New Vlue\n");
  31. scanf("%d",&v);
  32. push (v);
  33. break;
  34.  
  35. case 2: printf("\n choice=pop\n");
  36. if (top!=NULL)
  37. pop();
  38. else
  39. printf("\n No Data To Delete\n");
  40. break;
  41.  
  42.  
  43. case 3: printf("\n choice=Display\n");
  44. if (top!=NULL)
  45. Display();
  46. else
  47. printf("\n No Data to Display\n");
  48. break;
  49. default: printf("\n Wrong choice\n\n");
  50. break;
  51. case 4:printf ("\n choice=Count\n");
  52. count();
  53. break;
  54.  
  55. case 5: exit(0);
  56. break;
  57. }
  58. }
  59. }
  60. void push(int vv)
  61. {
  62. temp=(struct node*)malloc(1*sizeof(struct node));
  63. temp->data=vv;
  64. temp->link=top;
  65. top= temp;
  66. }
  67. void Display()
  68. {
  69. temp=top;
  70. while(temp!=NULL)
  71. {
  72. printf("\n %d", temp->data);
  73. temp=temp->link;
  74. }
  75. }
  76. void count()
  77. {
  78. int i=0;
  79. temp=top;
  80. while(temp!=NULL)
  81. {
  82. i=i+1;
  83. temp=temp->link;
  84. }
  85. printf("\n Total node=%d\n",i);
  86. }
  87. void pop()
  88. {
  89. temp=top;
  90. top=temp->link;
  91. printf("\n The Deleted value=%d\n",temp->data);
  92. free(temp);
  93. }
Add Comment
Please, Sign In to add comment