Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- typedef struct intStack {
- int * stackArr;
- int count;
- int top;
- int stackMax;
- } intStack;
- intStack* creatStack(int max){
- intStack* stack;
- stack = new intStack;
- if (stack == NULL)
- return 0;
- stack->stackArr = new int[max];
- stack->count = 0;
- stack->top = -1;
- stack->stackMax = max;
- return stack;
- }
- void destroyStack(intStack* stack){
- delete [] stack->stackArr;
- delete stack;
- }
- int pushStack(intStack* stack, int val){
- if (stack->count == stack->stackMax)
- return 0;
- (stack->count)++;
- (stack->top)++;
- stack->stackArr[stack->top] = val;
- return 1;
- }
- int popStack(intStack* stack, int* val){
- if (stack->count == 0)
- return 0;
- *val = stack->stackArr[stack->top];
- (stack->count)--;
- (stack->top)--;
- return 1;
- }
- int topStack(intStack* stack, int* val){
- if (stack->count == 0)
- return 0;
- *val = stack->stackArr[stack->top];
- return 1;
- }
- int isEmpty(intStack* stack){
- return !(stack->count);
- }
- int isFull(intStack* stack){
- return (stack->count == stack->stackMax);
- }
- void printStack(intStack* stack){
- for(int i = 0; i < stack->count; i++)
- cout << stack->stackArr[i] << " ";
- cout << endl;
- }
- int main(){
- intStack* stack = creatStack(5);
- int temp;
- pushStack(stack, 10);
- pushStack(stack, 5);
- pushStack(stack, 7);
- pushStack(stack, 8);
- pushStack(stack, -4);
- printStack(stack);
- popStack(stack, &temp);
- printStack(stack);
- popStack(stack, &temp);
- printStack(stack);
- topStack(stack, &temp);
- cout << temp << endl;
- destroyStack(stack);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment