fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. typedef struct node
  6. {
  7. void* dataPtr;
  8. struct node* link;
  9. } STACK_NODE;
  10.  
  11. typedef struct
  12. {
  13. int count;
  14. STACK_NODE* top;
  15. } STACK;
  16.  
  17. STACK* createStack()
  18. {
  19. STACK* stack;
  20.  
  21. stack = (STACK*)malloc(sizeof(STACK));
  22. if (stack)
  23. {
  24. stack->count = 0;
  25. stack->top = NULL;
  26. }
  27. return stack;
  28. }
  29.  
  30. bool pushStack(STACK* stack, void* dataInPtr)
  31. {
  32. STACK_NODE* newPtr;
  33.  
  34. newPtr = (STACK_NODE*)malloc(sizeof(STACK_NODE));
  35. if (!newPtr)
  36. return false;
  37.  
  38. newPtr->dataPtr = dataInPtr;
  39.  
  40. newPtr->link = stack->top;
  41. stack->top = newPtr;
  42.  
  43. (stack->count)++;
  44. return true;
  45. }
  46.  
  47. void* popStack(STACK* stack)
  48. {
  49. void* dataOutPtr;
  50. STACK_NODE* temp;
  51.  
  52. if (stack->count == 0)
  53. dataOutPtr = NULL;
  54. else
  55. {
  56. temp = stack->top;
  57. dataOutPtr = stack->top->dataPtr;
  58. stack->top = stack->top->link;
  59. free(temp);
  60. (stack->count)--;
  61. }
  62. return dataOutPtr;
  63. }
  64.  
  65. void* stackTop(STACK* stack)
  66. {
  67. if (stack->count == 0)
  68. return NULL;
  69. else
  70. return stack->top->dataPtr;
  71. }
  72.  
  73. bool emptyStack(STACK* stack)
  74. {
  75. return (stack->count == 0);
  76. }
  77.  
  78. bool fullStack(STACK* stack)
  79. {
  80. STACK_NODE* temp;
  81.  
  82. if (temp = (STACK_NODE*)malloc(sizeof(stack->top)))
  83. {
  84. free(temp);
  85. return false;
  86. }
  87.  
  88. return true;
  89. }
  90.  
  91. int stackCount(STACK* stack)
  92. {
  93. return stack->count;
  94. }
  95.  
  96. STACK* destroyStack(STACK* stack)
  97. {
  98. STACK_NODE* temp;
  99.  
  100. if (stack)
  101. {
  102. while (stack->top != NULL)
  103. {
  104. free(stack->top->dataPtr);
  105.  
  106. temp = stack->top;
  107. stack->top = stack->top->link;
  108. free(temp);
  109. }
  110.  
  111. free(stack);
  112. }
  113. return NULL;
  114. }
  115.  
  116. int main()
  117. {
  118. STACK* stack;
  119. int* dataPtr=NULL;
  120. int i = 0,
  121. data[2] = { 1, 2};
  122. bool done = false,
  123. firstdata = true;
  124.  
  125. stack = createStack();
  126.  
  127. printf("Enter numbers, <EOF> to stop\n");
  128.  
  129. while (!done)
  130. {
  131. if (firstdata)
  132. {
  133. dataPtr = (int*)malloc(sizeof(int));
  134. *dataPtr = data[i];
  135. printf("%d\n", *dataPtr);
  136. pushStack(stack, dataPtr);
  137. i++;
  138. if (i == 2)
  139. {
  140. firstdata = false;
  141. }
  142. }
  143. else
  144. {
  145. if ((scanf("%d", dataPtr) == EOF) || fullStack(stack))
  146. done = true;
  147. else
  148. {
  149.  
  150. while (!emptyStack(stack))
  151. {
  152. dataPtr = (int*)popStack(stack);
  153. printf("%d\n", *dataPtr);
  154. }
  155.  
  156. }
  157. }
  158. }
  159. destroyStack(stack);
  160.  
  161. return 0;
  162. }
  163.  
Success #stdin #stdout 0s 2428KB
stdin
Standard input is empty
stdout
Enter numbers, <EOF> to stop
1
2