-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathstack.c
77 lines (75 loc) · 1.57 KB
/
stack.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<stdlib.h>
struct stack{
int size;
int top;
int * arr;
};
int isEmpty(struct stack* ptr){
if(ptr->top == -1){
return 1;
}
else{
return 0;
}
}
int isFull(struct stack* ptr){
if(ptr->top == ptr->size - 1){
return 1;
}
else{
return 0;
}
}
void push(struct stack* ptr, int item){
if(isFull(ptr)){
printf("stck is full");
}
else{
ptr->top++;
ptr->arr[ptr->top] = item;
}
}
int pop(struct stack* ptr){
if(isEmpty(ptr)){
printf("stack underflow! can't pop elements");
}
else{
int item = ptr->arr[ptr->top];
ptr->top--;
return item;
}
}
int peek(struct stack* sp,int i){
int arrayInd = sp->top-i+1;
if(arrayInd<0){
printf("not a valid position");
return -1;
}
else{
return sp->arr[arrayInd];
}
}
int stackTop(struct stack* sp){
return sp->arr[sp->top];
}
int stackBottom(struct stack* sp)
{
return sp->arr[0];
}
int main(){
struct stack *sp = (struct stack *) malloc(sizeof(struct stack));
sp->size =50;
sp->top = -1;
sp->arr = (int *) malloc(sp->size * sizeof(int));
printf("before pushing, Full:%d\n", isFull(sp));
printf("before pushing, Full:%d\n", isEmpty(sp));
push(sp, 1);
push(sp, 41);
push(sp, 15);
push(sp, 81);
push(sp, 91);
push(sp, 18);
printf("popped %d from the stack",pop(sp));
return 0;
}