-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
133 lines (115 loc) · 2.98 KB
/
Copy pathstack.c
File metadata and controls
133 lines (115 loc) · 2.98 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Includes definitions:
- typedef struct {...} Stack;
- int isEmptyStack(Stack *s);
- int isFullStack(Stack *s);
- int pushStack(Stack *s,int value);
- int popStack(Stack *s);
- void displayStack(Stack *s);
- Stack *initializeStack();
- int peekStack(Stack *s);
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX 100
// Definition of stack
typedef struct {
int stack[MAX];
int top;
} Stack;
/*
Initializes and allocates memory for a new stack.
Returns the pointer to the new stack
*/
Stack *initializeStack() {
Stack *s = (Stack *)malloc(sizeof(Stack));
s->top=-1;
return s;
}
/*
returns 1 if a stack is empty, else returns 0
*/
int isEmptyStack(Stack *s) {
if (s->top==-1) return 1; //true
else return 0; //false
}
/*
returns 1 if a stack is full, else returns 0
*/
int isFullStack(Stack *s) {
if (s->top==MAX-1) return 1;
else return 0;
}
/*
Inserts new value in the stack structure
It takes the following parameters:
- stack (`Stack *`) : pointer of the stack struct, in order to modify it
- value (`int`) : value to insert (push)
If successful, returns 0, else returns -1
*/
int pushStack(Stack *s,int value) {
if (isFullStack(s)) return -1;
s->stack[++s->top] = value;
return 0;
}
/*
Deletes and returns the element from the top of the stack structure
It takes the following parameters:
- stack (`Stack *`) : pointer of the stack struct, in order to modify it
If no element is left to delete then returns `INT_MIN` from `limits.h`
*/
int popStack(Stack *s) {
if (!isEmptyStack(s)) {
int value = s->stack[s->top--];
return value;
}
else return INT_MIN;
}
/*
Returns the element from the top of the stack structure
It takes the following parameters:
- stack (`Stack *`) : pointer of the stack struct, in order to modify it
If the stack is empty, then returns `INT_MIN` from `limits.h`
*/
int peekStack(Stack *s) {
if (!isEmptyStack(s))
return s->stack[s->top];
else
return INT_MIN;
}
/*
Prints the elements of the stack in an array-like fashion,
- [a1,a2,a3,...]
*/
void displayStack(Stack *s) {
printf("[");
for (int i = 0; i <= s->top; i++)
{
printf("%d, ",s->stack[i]);
}
printf("]\n");
}
/*
Searches for a given element in the stack and returns
the index of the found value nearest to the top.
- stack (`Stack *`) : pointer of the stack struct, in order to modify it
- int (`int`) : value to search for in the stack
Returns `-1` if value is not found.
*/
int searchStack(Stack *s, int value) {
for (int i=s->top;i>=0;i--)
if (value==s->stack[i]) return i;
return -1;
}
// Main for Debugging Purpose only
// int main() {
// Stack *s = initializeStack();
// pushStack(s,12);
// pushStack(s,12);
// pushStack(s,23);
// pushStack(s,34);
// pushStack(s,45);
// pushStack(s,56);
// printf("%d",searchStack(s,12));
// }