Advertisement
fqrmix

Stack(Push+Pop+Print)

Dec 25th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define STACK_OVERFLOW -100
  3. #define STACK_UNDERFLOW -101
  4. #define STACKSIZE 5
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. struct Stack {
  10.     float Array[STACKSIZE];
  11.     int size;
  12. };
  13.  
  14. void push(Stack *stack, float value);
  15.  
  16. void printStackValue(float value);
  17.  
  18. void printStack(const Stack *stack);
  19.  
  20. int pop(Stack *stack);
  21.  
  22.  
  23. void main()
  24. {
  25.     Stack Stack;
  26.     Stack.size = 0;
  27.     push(&Stack, 3);
  28.     printStack(&Stack);
  29.     push(&Stack, 5);
  30.     printStack(&Stack);
  31.     push(&Stack, 7);
  32.     printStack(&Stack);
  33.     printf("%d\n", pop(&Stack));
  34.     printStack(&Stack);
  35.     printf("%d\n", pop(&Stack));
  36.     printStack(&Stack);
  37.     printf("%d\n", pop(&Stack));
  38.     printStack(&Stack);
  39.     system("pause");
  40. }
  41.  
  42. void printStackValue(int value) {
  43.     printf("%d", value);
  44. }
  45.  
  46. void printStack(const Stack *stack) {
  47.     int i;
  48.     int len = stack->size - 1;
  49.     printf("stack size = %d > \n", stack->size);
  50.     for (i = 0; i < len; i++) {
  51.         printStackValue(stack->Array[i]);
  52.         printf(" | ");
  53.     }
  54.     if (stack->size != 0) {
  55.         printStackValue(stack->Array[i]);
  56.     }
  57.     printf("\n");
  58. }
  59.  
  60. int pop(Stack *stack) {
  61.     if (stack->size == 0) {
  62.         exit(STACK_UNDERFLOW);
  63.     }
  64.     stack->size--;
  65.     return stack->Array[stack->size];
  66. }
  67.  
  68. void push(Stack *stack, float value)
  69. {
  70.     if (stack->size >= STACKSIZE)
  71.         exit(STACK_OVERFLOW);
  72.     stack->Array[stack->size] = value;
  73.     stack->size++;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement