Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define STACK_OVERFLOW -100
- #define STACK_UNDERFLOW -101
- #define STACKSIZE 5
- #include <stdio.h>
- #include <stdlib.h>
- struct Stack {
- float Array[STACKSIZE];
- int size;
- };
- void push(Stack *stack, float value);
- void printStackValue(float value);
- void printStack(const Stack *stack);
- int pop(Stack *stack);
- void main()
- {
- Stack Stack;
- Stack.size = 0;
- push(&Stack, 3);
- printStack(&Stack);
- push(&Stack, 5);
- printStack(&Stack);
- push(&Stack, 7);
- printStack(&Stack);
- printf("%d\n", pop(&Stack));
- printStack(&Stack);
- printf("%d\n", pop(&Stack));
- printStack(&Stack);
- printf("%d\n", pop(&Stack));
- printStack(&Stack);
- system("pause");
- }
- void printStackValue(int value) {
- printf("%d", value);
- }
- void printStack(const Stack *stack) {
- int i;
- int len = stack->size - 1;
- printf("stack size = %d > \n", stack->size);
- for (i = 0; i < len; i++) {
- printStackValue(stack->Array[i]);
- printf(" | ");
- }
- if (stack->size != 0) {
- printStackValue(stack->Array[i]);
- }
- printf("\n");
- }
- int pop(Stack *stack) {
- if (stack->size == 0) {
- exit(STACK_UNDERFLOW);
- }
- stack->size--;
- return stack->Array[stack->size];
- }
- void push(Stack *stack, float value)
- {
- if (stack->size >= STACKSIZE)
- exit(STACK_OVERFLOW);
- stack->Array[stack->size] = value;
- stack->size++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement