-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathStack.c
79 lines (79 loc) · 1.47 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
78
79
#include<stdio.h>
#include<stdlib.h>
struct Stack{
int size;
int top;
int *S;
};
void create(struct Stack *st){
printf("Enter the size of the Stack\n");
scanf("%d",&st->size);
st->top=-1;
st->S=(int *)malloc(st->size*sizeof(int));
}
void display(struct Stack st){
for(int i=st.top;i>=0;i--)
printf("%d ",st.S[i]);
printf("\n");
}
void push(struct Stack *st,int x){
if(st->top==st->size-1)
printf("Overflow\n");
else{st->top++;
st->S[st->top]=x;
}
}
int pop(struct Stack *st){
int x=-1;
if(st->top==-1)
printf("Stack Underflow\n");
else{
x=st->S[st->top--];
// free(x);
} return x;
}
int peek(struct Stack st,int index){
int x=-1;
if(st.top-index+1<0)
printf("Invlaid index\n");
x=st.S[st.top-index+1];
return x;
}
void isFull(struct Stack st){
if(st.top==st.size-1)
printf("full\n");
}
void isEmpty(struct Stack st){
if(!st.top==-1)
printf("\nStack is not empty\n");
printf("\nEmpty\n");
}
void stackTop(struct Stack st){
if(st.top==-1)
printf("Stack is empty\n");
else{
printf("%d",st.S[st.top]);
}
}
int main(){
struct Stack st;
create(&st);
push(&st,10);
push(&st,20);
push(&st,30);
push(&st,40);
display(st);
printf("\n");
// pop(&st);
// display(st);
// printf("%d",peek(st,3));
// pop(&st);
// pop(&st);
// pop(&st);
// display(st);
// isEmpty(st);
push(&st,100);
isFull(st);
stackTop(st);
return 0;
}