Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "int_stack.h"
- int main (void)
- {
- int curNum; /* index describing the current number */
- tstack stack;
- init (&stack); /* stack is initialised */
- for (curNum=0 ; curNum<VALUES ; curNum++) /* we push some values */
- if (push (&stack, curNum)==0)
- exit (1);
- for (curNum=0 ; curNum<VALUES ; curNum++) /* values are popped */
- printf ("Number popped: %i\n", pop (&stack));
- destroy (&stack); /* we destroy stack */
- return 0;
- }
- void init(tstack *stack)
- {
- stack->top=0;
- stack->numbers=malloc(sizeof(int));
- }
- void destroy (tstack *stack)
- {
- free (stack->numbers); /* we free memory */
- }
- int isempty (tstack *stack)
- {
- return (stack->top);
- }
- int push(tstack *stack,int element)
- {
- int *temp;
- temp = (int*)realloc(stack->numbers, sizeof(int)*((stack->top)+1));
- if(!(temp))
- {
- free(stack->numbers);
- printf("Error while reallocating memory\n");
- return 0;
- }
- stack->top++;
- stack->numbers=temp;
- (stack->numbers)[stack->top-1]=element;
- return 1;
- }
- int pop (tstack *stack)
- {
- if (!stack->top)
- {
- printf ("The stack is empty, nothing to pop!\n");
- return 0;
- }
- return (stack->numbers [--stack->top]);
- }
Add Comment
Please, Sign In to add comment