fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct stackNode
  5. {
  6. int data;
  7. struct stackNode * next;
  8. };
  9.  
  10. // defining the generic function to PUSH an element into the stack
  11. struct stackNode * push(struct stackNode * top, int element)
  12. {
  13. //creating a temporary node and assigning the element to be PUSHED
  14. struct stackNode * temp = (struct stackNode *)malloc(sizeof(struct stackNode));
  15. if(!temp)
  16. {
  17. printf("STACK OVERFLOW");
  18. return top;
  19. }
  20.  
  21. temp -> data = element;
  22.  
  23. // we need to point the NEXT of this node to the previous TOP
  24. temp -> next = top;
  25. //Code obtained from http://w...content-available-to-author-only...w.studyalgorithms,com
  26. //Feel free to copy but please acknowledge the site wherever possible
  27.  
  28. // we need to update the TOP node and then return it
  29. // we can simply return the 'temp' node as it points to the new TOP node.
  30. return temp;
  31. }
  32.  
  33. // defining a function to check if a stack is empty
  34. int isStackEmpty(struct stackNode * top)
  35. {
  36. if(top == NULL)
  37. return 1;
  38. else
  39. return 0;
  40. }
  41.  
  42. //Code obtained from http://w...content-available-to-author-only...w.studyalgorithms,com
  43. //Feel free to copy but please acknowledge the site wherever possible
  44.  
  45. // defining a function to pop an element from a stack
  46. struct stackNode * pop(struct stackNode * top)
  47. {
  48. // check if the stack is empty
  49. if(isStackEmpty(top))
  50. {
  51. printf("STACK IS EMPTY...Stack underflow");
  52. return NULL;
  53. }
  54. else
  55. {
  56. printf("POP element = %d\n",top -> data);
  57.  
  58. // we need to delete the top and move to the next element.
  59.  
  60. struct stackNode * temp = top;
  61.  
  62. // move to the next node
  63. top = top -> next;
  64.  
  65. //Code obtained from http://w...content-available-to-author-only...w.studyalgorithms,com
  66. //Feel free to copy but please acknowledge the site wherever possible
  67.  
  68. // delete the original top
  69. free(temp);
  70.  
  71. // return the new top
  72. return top;
  73. }
  74. }
  75.  
  76. // function to get the size of the stack
  77. int getStackSize(struct stackNode * top)
  78. {
  79. int count = 0;
  80. while(top)
  81. {
  82. count++;
  83. top = top -> next;
  84. //Code obtained from http://w...content-available-to-author-only...w.studyalgorithms,com
  85. //Feel free to copy but please acknowledge the site wherever possible
  86. }
  87. return count;
  88. }
  89.  
  90. int main(void)
  91. {
  92. struct stackNode * top = NULL;
  93.  
  94. top = push(top,1);
  95. top = push(top,2);
  96. top = push(top,3);
  97. top = push(top,4);
  98. top = push(top,5);
  99.  
  100. top = pop(top);
  101. top = pop(top);
  102.  
  103. return 0;
  104. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
POP element = 5
POP element = 4