-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack1.c
55 lines (44 loc) · 993 Bytes
/
stack1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct Stack{
int top;
unsigned capacity;
int* array;
};
struct Stack* createStack(unsigned capacity){
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top=-1;
stack->array=(int*)malloc(stack->capacity * sizeof(int));
return stack;
}
int isFull(struct Stack* stack){
return stack->top == stack->capacity -1 ;
}
int isEmpty(struct Stack* stack){
return stack->top == -1;
}
void push(struct Stack* stack, int item){
if(isFull(stack))
return;
stack->array[++stack->top] = item;
}
int pop(struct Stack* stack){
if(isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
}
int peek(struct Stack* stack){
if(isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}
int main(){
struct Stack* stack = createStack(100);
//struct StackNode* root = NULL;
push(stack,100);
push(stack,20);
printf(pop(stack));
printf(peek(stack));
}