-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.c
More file actions
109 lines (92 loc) · 1.64 KB
/
Stack.c
File metadata and controls
109 lines (92 loc) · 1.64 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
102
103
104
105
106
107
108
109
/*
Author: CWN221
Date: 5/03/2025
RegNo: BSE-01-0032/2024
Description: C program to describe Stack in DSA
*/
#include <stdio.h>
#define Max 10
int count = 0;
//Create a stack
typedef struct stack {
int top;
int size[Max];
}st;
//Create an empty stack
void createEmpty(st *s) {
s-> top = -1;
}
//Check if stack is empty
int checkEmpty(st *s) {
if (s-> top == -1) {
return 1;
} else {
return 0;
}
}
//Check if stack is full
int checkFull(st *s) {
if (s-> top == Max - 1) {
return 1;
} else {
return 0;
}
}
//Adding elements
void add(st *s, int no) {
if (checkFull(s)) {
printf("Stack is full.");
} else {
s-> top++;
s-> size[s->top] = no;
}
count++;
}
//Removing elements
void removeStack(st *s) {
if (checkEmpty(s)) {
printf("Stack is empty.\n");
} else {
printf("Element removed: %d\n", s-> size[s-> top]);
s-> top--;
count--;
}
}
//Printing stack
void printStack(st *s) {
int i;
if (checkEmpty(s)) {
printf("Stack is empty.\n");
return;
}
for (i = 0; i < count; i++) {
printf("%d ", s-> size[i]);
}
printf("\n");
}
int main() {
st s;
//Create Elements
createEmpty(&s);
//Push
printf("New Elements: \n");
add(&s, 1);
add(&s, 2);
add(&s, 3);
add(&s, 4);
//Print stack
printStack(&s);
//Pop
removeStack(&s);
//Print new elements after pop
printf("\nNew elements after popping: \n");
printStack(&s);
//Adding new elements
add(&s, 4);
add(&s, 5);
add(&s, 6);
//Printing new elements after push
printf("New elements after pushing: \n");
printStack(&s);
return 0;
}