-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_array.c
More file actions
101 lines (95 loc) · 1.48 KB
/
stack_array.c
File metadata and controls
101 lines (95 loc) · 1.48 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<stdio.h>
#include<stdlib.h>
typedef struct stack
{
int n;
int top;
int *s;
}stack;
void push(stack *st,int x)
{
if(st->top==st->n-1)
printf("stack overflow\n");
else
{
st->top++;
st->s[st->top]=x;
}
}
void display(stack st)
{
int i;
for(i=st.top;i>=0;i--)
{
printf("%d ",st.s[i]);
}
}
void create(stack *st)
{
printf("Enter the size of the stack\n");
scanf("%d",&st->n);
st->s=(int *)malloc(sizeof(int)*st->n);
st->top=-1;
}
int pop(stack *st)
{
int x=-1;
if(st->top==-1)
{
printf("Stack Underflow\n");
return x;
}
else
{
x=st->s[st->top];
st->top--;
return x;
}
}
int peek(stack st,int index)//index from above the stack//
{
int x=-1;
if(st.top-index+1<0)
return x;
else
{
x=st.s[st.top-index+1];
return x;
}
}
int stacktop(stack st)
{
if(st.top==-1)
{
return -1;
}
else
return st.s[st.top];
}
int isFull(stack st)
{
if(st.top==st.n-1)
return 1;
else
return 0;
}
int isEmpty(stack st)
{
if(st.top==-1)
return 1;
else
return 0;
}
void main()
{
stack st;
create(&st);
push(&st,1);
push(&st,2);
push(&st,4);
push(&st,6);
display(st);
printf("%d \n",pop(&st));
printf("%d\n",peek(st,3));
printf("%d",stacktop(st));
}