Guest User

Untitled

a guest
Sep 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. typedef struct NODE {
  6. int data;
  7. struct NODE* next;
  8. }node;
  9.  
  10. node* top = NULL;
  11. // Push Function
  12. void push(int x)
  13. {
  14. node *temp;
  15. temp = (node *) malloc(sizeof(node));
  16. temp -> data = x;
  17. temp -> next = top;
  18. top = temp;
  19.  
  20. }
  21. // Pop function
  22. int pop()
  23. {
  24. node* temp;
  25. int num=0;
  26. temp = top;
  27. num = temp -> data;
  28. top = top -> next;
  29. free(temp);
  30. return num;
  31. }
  32.  
  33. int Top()
  34. {
  35. return top -> data;
  36. }
  37.  
  38. void display(node *head)
  39. {
  40. if(head == NULL) printf("NULL!n");
  41. else
  42. {
  43. printf("%dn", head -> data);
  44. display(head->next);
  45. }
  46. }
  47. int main() {
  48. int element,choice,val,tp;
  49. do
  50. {
  51. printf("---Stack Operations---n");
  52. printf("1.PUSHn");
  53. printf("2.POPn");
  54. printf("3.DISPLAYn");
  55. printf("4.Top elementn");
  56. printf("5.EXITn");
  57. printf("Enter an optionn");
  58. scanf("%d",&choice);
  59.  
  60. switch(choice)
  61. {
  62. case 1:
  63. printf("Enter the element to pushn");
  64. scanf("%d",&val);
  65. push(val);
  66. break;
  67.  
  68. case 2:
  69. element = pop();
  70. printf("Popped element is: %dn",element);
  71. break;
  72.  
  73. case 3:
  74. display(top);
  75. break;
  76.  
  77. case 4:
  78. tp = Top();
  79. printf("Top element is:%dn",tp);
  80. break;
  81.  
  82. case 5:
  83. exit(0);
  84. default:
  85. printf("Enter a valid optionn");
  86. }
  87. }while(choice != 5);
  88.  
  89. }
Add Comment
Please, Sign In to add comment