fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define SIZE 10
  4. int count=0;
  5. typedef struct node{
  6. int value;
  7. struct stack *next;
  8. struct stack *prev;
  9. }stack;
  10. stack *bottom=NULL;
  11. stack *top=NULL;
  12. stack *createnode(int a)
  13. {
  14. stack *newstack;
  15. newstack=(stack*)calloc(1,sizeof(stack));
  16. newstack->value=a;
  17. newstack->prev=NULL;
  18. newstack->next=NULL;
  19. return newstack;
  20. }
  21. int isempty()
  22. {
  23. if(count==0)
  24. {return 1;}
  25. else
  26. {return 0;}
  27. }
  28. int isfull()
  29. {
  30. if(count==SIZE)
  31. {return 1;}
  32. else
  33. {return 0;}
  34. }
  35. void push(int a)
  36. { if(isfull()==1)
  37. {
  38. printf("overflow condition: stack full");
  39. }
  40. else
  41. {count+=1;
  42. stack *newstack = createnode(a);
  43. if(bottom==NULL)
  44. {
  45. bottom=newstack;
  46. top=bottom;
  47. }
  48. else
  49. {
  50. top->next=newstack;
  51. newstack->prev=top;
  52. top=newstack;
  53. }
  54. }
  55. }
  56. int peek()
  57. {
  58. printf("%d\n",top->value);
  59. }
  60. int pop()
  61. { if(isempty()==1)
  62. {
  63. printf("underflow condition:empty stack");
  64. }
  65. else
  66. {if(count==1)
  67. { int c;
  68. c=top->value;
  69. top=NULL;
  70. printf("popped value is:%d\n",c);
  71. count=count-1;
  72. }
  73. else
  74. {int c;
  75. c=top->value;
  76. top=top->prev;
  77. top->next=NULL;
  78. printf("popped value is:%d\n",c);
  79. count=count-1;
  80. }
  81. }
  82. }
  83.  
  84. int main(void) {
  85. stack ruby; int i;
  86. for(i=0;i<12;i++)
  87. {push(i);
  88. printf("%d\n",count);
  89. }
  90. isfull();
  91. pop();
  92. pop();
  93. pop();
  94. pop();
  95. pop();
  96. pop();
  97. pop();
  98. pop();
  99. pop();
  100. pop();
  101. pop();
  102. int c=isempty();
  103. printf("%d",c);
  104. printf("%d",count);
  105. return 0;
  106. }
  107.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
1
2
3
4
5
6
7
8
9
10
overflow condition: stack full10
overflow condition: stack full10
popped value is:9
popped value is:8
popped value is:7
popped value is:6
popped value is:5
popped value is:4
popped value is:3
popped value is:2
popped value is:1
popped value is:0
underflow condition:empty stack10