13043 - Dynamic Stacks using Dynamic Arrays
|
Time |
Memory |
| Case 1 |
1 sec |
32 MB |
Description
Define dynamic stacks as follows.
struct stack {
int *stack_buffer;
int top;
int max_size;
};
Implement the following four functions as shown in 13043.c.
- void stack_init(struct stack *s, int size);
- Use dynamic memory allocation to allocate appropriate memory according to size.
- void stack_destroy(struct stack *s);
- void dyn_push(int elem, struct stack *s);
- Push elem to the stack. If it is full, double its size and push elem.
- int dyn_pop(struct stack *s);
- Pop an element from the stack as return value.
- If top < max_size/4, shrink the stack’s size by half (as max_size/2).
- If the stack is empty before the pop operation, use printf("Stack is empty!\n"); and return the value -100000.
- int show_stack(struct stack *s);
- If stack is empty, print "Empty", Else; print "Stack contains "
- Loop through the stack, printing all of the elements in the buffer.
- Finally, print "top=%d, max_size=%d\n"
UPDATE:
If you have presentation error issues please consider formatting your printf statements as follows:
The following must be included when submitting:
#include <stdio.h>
#include <stdlib.h>
struct stack {
int *stack_buffer;
int top;
int max_size;
};
/* Your Code Here. */
void function1(...)
void function2(...)
Input
Output
Partial Judge Code
13043.c
Partial Judge Header
13043.h
Tags