forked from tirthasheshpatel/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_as_static_arrays.c
126 lines (105 loc) · 2.86 KB
/
stack_as_static_arrays.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
#include <stdio.h> // Handles standard input/output
#include <errno.h> // Error Handling library
#include <stdlib.h>
// The maximum number of elements that can be stored in a stack.
#define MAX 65535
// Structure of stack
struct stack
{
// A integer pointing at the top of the stack.
int top;
// A array to hold the values of stack.
int arr[MAX+1];
}s;
// Deletes and returns the top element in a stack.
int POP(struct stack* s)
{
// IMPLEMENT POP
// Handle error when stack is empty.
if(s->top == -1) perror("Stack Underflow!");
// Get a copy of value at the top of the stack
// in a variable `element` that we are going to return.
int element = s->arr[s->top];
// Initialize the value at the top of the stack again to 0.
s->arr[s->top] = 0;
// Decrease the value of `top` by 1.
s->top--;
// Finally, return `element`.
return element;
}
// Adds a element onto the top of stack
void PUSH(struct stack* s, int value)
{
// IMPLEMENT PUSH
// Handle error in case the stack has reached its maximum limit `MAX`.
if(s->top == MAX) perror("Stack Overflow!");
// Increase the value of `top` by 1.
s->top++;
// Insert element at the index `top`.
s->arr[s->top] = value;
}
// Returns the i'th element from the top of the stack
int PEEP(struct stack* s, int index)
{
// IMPLEMENT PEEP
// Handle error if index is not valid
if(index <= 0 || index > MAX) perror("index out of range!");
return s->arr[s->top - index]; // return the i'th element from the top.
}
void CHANGE(struct stack* s, int index, int new_value)
{
// IMPLEMENT CHANGE
// Handle error in case of out of range access.
if(index <= 0 || index > MAX) perror("index out of range!");
// Insert the new value at index `top-index`.
s->arr[s->top - index] = new_value;
}
void PRINT_STACK(struct stack* s)
{
if(s->top == -1) perror("Nothing present in stack!");
for(int i=0;i<=s->top;i++){
printf("%d ", PEEP(s, s->top-i));
}
printf("\n");
}
int main()
{
struct stack s = {-1, 0};
char command;
while(1)
{
printf("Enter command: ");
scanf("%c", &command);
fflush(stdout);
fflush(stdin);
if(command == 'i'){
printf("Enter the value to insert: ");
int value;
scanf("%d", &value);
PUSH(&s, value);
}
else if(command == 'd'){
int deleted_element = POP(&s);
printf("The element popped is: %d", deleted_element);
}
else if(command == 'p'){
printf("Enter the index at which you wnat to peep: ");
int index;
scanf("%d", &index);
int peeped_element = PEEP(&s, index);
printf("Peeped Element: ", peeped_element);
}
else if(command == 'c'){
printf("Enter the index you wnat to edit: ");
int index;
scanf("%d", &index);
printf("Enter the new value: ");
int new_value;
scanf("%d", &new_value);
CHANGE(&s, index, new_value);
}
else if(command == 's') PRINT_STACK(&s);
else if(command == 'q') break;
else perror("Command not found!");
}
}