-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack_using_array.c
188 lines (145 loc) · 3.23 KB
/
stack_using_array.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/****************************************************************************
File name: stack_using_array.c
Author: babajr
*****************************************************************************/
/*
Implementation of stack using array physical data structure.
Idea is to push element at the end of array and delete / pop element
from the end.
*/
#include <stdio.h>
#include <stdlib.h>
struct stack
{
int size; // size of stack.
int top; // index always pointing to the top of the stack.
int *s; // pointer to dynamic array.
};
typedef struct stack stack;
/*
API to create the stack. Initialise the stack.
*/
void create(stack *st)
{
// Ask user to enter the size.
printf("Enter the size of ARRAY/STACk \n");
scanf("%d", &st->size);
// Initially stack is empty i.e. top = -1.
st->top = -1;
// create array on the heap.
st->s = (int *)malloc(sizeof(int) * st->size);
}
/*
API to add element to the stack at the top.
*/
void push(stack *st, int value)
{
// Check if stack is full or not.
if(st->top == st->size-1) // -1 as stack index starts from 0.
{
printf("Stack Overflow\n");
}
else
{
// point the top to next element and add the value at top.
st->top++;
st->s[st->top] = value;
}
}
/*
API to delete / pop out element from the stack. It will return popped out value.
*/
int pop(stack *st)
{
int value = -1; // if stack is empty, return -1.
// Check if stack is empty.
if(st->top == -1)
printf("Stack is Empty\n");
else
{
value = st->s[st->top];
st->top--;
}
return value;
}
/*
API to display the elements of the stack.
*/
void display(stack st)
{
// Traverse the stack.
for(int i = st.top; i >= 0; i--)
{
printf("%d\t", st.s[i]);
}
printf("\n");
}
/*
API to get the element from the stack present at particular position.
pos index value in stack
1 3 20 <-- top
2 2 15
3 1 10
4 0 5
Relation between Index and Pos ==>
index = (top - pos) + 1
*/
int peek(stack st, int pos)
{
int value = -1; // If invalid index, return -1.
// Check for valid index.
if((st.top - pos + 1) < 0)
printf("Invalid Index\n");
else
value = st.s[st.top - pos + 1];
return value;
}
/*
API to check is stack is empty.
*/
int isEmpty(stack st)
{
if(st.top == -1)
return 1;
else
return 0;
}
/*
API to check is stack is full.
*/
int isFull(stack st)
{
if(st.top == (st.size - 1))
return 1;
else
return 0;
}
/*
API to get the element at the top.
*/
int stackTop(stack st)
{
if(isEmpty(st) == 0)
return st.s[st.top];
else
return -1;
}
int main(void)
{
stack st;
create(&st);
push(&st, 1);
push(&st, 3);
push(&st, 2);
push(&st, 5);
push(&st, 4);
display(st);
// pop(&st);
// pop(&st);
// display(st);
printf("peek() === %d\n", peek(st, 1));
printf("isEmpty() === %d\n", isEmpty(st));
printf("isFull() === %d\n", isFull(st));
printf("stackTop() === %d\n", stackTop(st));
return 0;
}