-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
54 lines (44 loc) · 778 Bytes
/
Copy pathstack.c
File metadata and controls
54 lines (44 loc) · 778 Bytes
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
#include<stdio.h>
#include<stdlib.h>
#include "stack.h"
struct stack {
int n; //The quantity of elements of the stack
int vector[5]; //The stacks as a vector.
};
//Initialization of the stack
Stack* init(){
Stack *s;
s = (Stack*)malloc(sizeof(Stack));
if (s == NULL){
printf("FATAL ERROR!\n");
exit(1);
}
s->n = 0;
return s;
}
//Push Operation
void push(int q,Stack*s){
//Checks stack overflow
if (s->n == N){
printf("Stack Overflow!\n");
exit(1);
}
s->vector[s->n] = q;
s->n++;
}
int pop(Stack*s){
int v;
if (s->n == 0){
printf("There's no element in the stack!\n");
exit(2);
}
s->n--;
v = s->vector[s->n];
return v;
}
void print_stack(Stack*s){
int i;
for(i = 0; i<s->n; i++){
printf("vector[%d] = %d \n",i, s->vector[i]);
}
}