Repository navigation
Expand file tree
/
Copy pathstack.h
More file actions
93 lines (81 loc) · 2.17 KB
/
Copy pathstack.h
File metadata and controls
93 lines (81 loc) · 2.17 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
#ifndef STACK_JL
#define STACK_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define STACK_MAX_SIZE 100 // The maximum number of elements that we can store in the stack.
// Define how our (linear and dynamic) stack is structured.
typedef struct{
int data[STACK_MAX_SIZE]; // The internal array that stores our stack elements.
int top; // The index of the top element. When it's empty is equal to -1.
} Stack;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Stack. We start with top equal to -1 to show it's empty.
void initStack(Stack *stack){
stack->top = -1;
}
// Check if the Stack is Empty. Returns 1 if it's empty or 0 if it has elements.
int isEmptyStack(Stack *stack){
if(stack->top == -1){
return 1;
}
else{
return 0;
}
}
// Check if the Stack is Full. Returns 1 if it's full or 0 if it's not full.
int isFullStack(Stack *stack){
if(stack->top >= STACK_MAX_SIZE-1){
return 1;
}
else{
return 0;
}
}
// Push an Element onto the Stack. Returns 1 if successful or 0 if the stack is full.
int pushStack(Stack *stack, int value){
if(isFullStack(stack)){
return 0;
}
else{
stack->top++;
stack->data[stack->top] = value;
return 1;
}
}
// Pop an Element from the Stack. Returns 1 if successful or 0 if the stack is empty.
int popStack(Stack *stack, int *value){
if(isEmptyStack(stack)){
return 0;
}
else{
*value = stack->data[stack->top];
stack->top--;
return 1;
}
}
// Peeks at the Top Element without Removing It. Returns 1 if successful or 0 if the stack is empty.
int peekStack(Stack *stack, int *value){
if(isEmptyStack(stack)){
return 0;
}
else{
*value = stack->data[stack->top];
return 1;
}
}
// Print all Elements of the Stack. Displays elements from bottom to top.
void printStack(Stack *stack){
printf("[");
for(int i = 0; i <= stack->top; i++){
printf("%d", stack->data[i]);
if(i < stack->top){
printf(", ");
}
}
printf("]\n");
}
#endif //STACK_JL