Repository navigation
Expand file tree
/
Copy pathlinkedstack.h
More file actions
111 lines (98 loc) · 2.88 KB
/
Copy pathlinkedstack.h
File metadata and controls
111 lines (98 loc) · 2.88 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
#ifndef LINKEDSTACK_JL
#define LINKEDSTACK_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// Define how our stack node is structured.
typedef struct StackNode{
int data; // The value of the node that we need to store.
struct StackNode* next; // The pointer to the node below it.
} StackNode;
// Define how our (linear and dynamic) stack built on a linked list is structured. It has no size limit.
typedef struct{
StackNode* top; // The pointer to the top node (null if it's empty).
int size; // The number of elements in our stack.
} LinkedStack;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Linked Stack. We start setting the top pointer to null and size to 0.
void initLinkedStack(LinkedStack *stack){
stack->top = NULL;
stack->size = 0;
}
// Check if the Stack is Empty. Returns 1 if it's empty or 0 if it has elements.
int isEmptyLinkedStack(LinkedStack *stack){
if(stack->top == NULL){
return 1;
}
else{
return 0;
}
}
// Push an Element onto the Stack. Returns 1 if successful or 0 if there's no memory.
int pushLinkedStack(LinkedStack *stack, int value){
StackNode* newNode = (StackNode*)malloc(sizeof(StackNode));
if(!newNode){
return 0;
}
else{
newNode->data = value;
newNode->next = stack->top;
stack->top = newNode;
stack->size++;
return 1;
}
}
// Pop an Element from the Stack. Returns 1 if successful or 0 if the stack is empty.
int popLinkedStack(LinkedStack *stack, int *value){
if(isEmptyLinkedStack(stack)){
return 0;
}
else{
StackNode* temp = stack->top;
*value = temp->data;
stack->top = temp->next;
free(temp);
stack->size--;
return 1;
}
}
// Peek at the Top Element without Removing It. Returns 1 if successful or 0 if the stack is empty.
int peekLinkedStack(LinkedStack *stack, int *value){
if(isEmptyLinkedStack(stack)){
return 0;
}
else{
*value = stack->top->data;
return 1;
}
}
// Print a Node and all the Nodes Below It, from the bottom one to this one.
void printStackNodes(StackNode *node){
if(node != NULL){
printStackNodes(node->next);
if(node->next != NULL){
printf(", ");
}
printf("%d", node->data);
}
}
// Print all Elements of the Stack. Displays elements from bottom to top, like the Stack.
void printLinkedStack(LinkedStack *stack){
printf("[");
printStackNodes(stack->top);
printf("]\n");
}
// Free all the Nodes and Reset the Stack.
void freeLinkedStack(LinkedStack *stack){
StackNode* temp = stack->top;
while(temp != NULL){
StackNode* nextNode = temp->next;
free(temp);
temp = nextNode;
}
initLinkedStack(stack);
}
#endif //LINKEDSTACK_JL