forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
147 lines (128 loc) · 2.82 KB
/
stack.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Kyler Smith, 2017
* Stack data structure implementation.
*/
////////////////////////////////////////////////////////////////////////////////
//INCLUDES
#include <stdio.h>
#include <stdlib.h>
////////////////////////////////////////////////////////////////////////////////
//MACROS: CONSTANTS
////////////////////////////////////////////////////////////////////////////////
//DATA STRUCTURES
struct node {
int data;
struct node* next;
struct node* pre;
} *head, *tmp;
////////////////////////////////////////////////////////////////////////////////
//GLOBAL VARIABLES
int count = 0;
////////////////////////////////////////////////////////////////////////////////
//FUNCTION PROTOTYPES
void create();
void push(int x);
int pop();
int peek();
int size();
int isEmpty();
////////////////////////////////////////////////////////////////////////////////
//MAIN ENTRY POINT
int main(int argc, char const *argv[]) {
int x, y, z;
create();
push(4);
x = pop();
// 4. Count: 0. Empty: 1.
printf("%d.\t\tCount: %d.\tEmpty: %d.\n", x, size(), isEmpty());
push(1);
push(2);
push(3);
x = pop();
y = pop();
// 3, 2. Count: 1. Empty: 0;
printf("%d, %d.\t\tCount: %d.\tEmpty: %d.\n", x, y, size(), isEmpty());
pop(); // Empty the stack.
push(5);
push(6);
x = peek();
push(7);
y = pop();
push(8);
z = pop();
// 1, 6, 7, 8. Count: 2. Empty: 0.
printf("%d, %d, %d.\tCount: %d.\tEmpty: %d.\n", x, y, z, size(), isEmpty());
return 0;
}
/**
* Initialize the stack to NULL.
*/
void create() {
head = NULL;
}
/**
* Push data onto the stack.
*/
void push(int x) {
if(head == NULL) {
head = (struct node *)malloc(1 * sizeof(struct node));
head->next = NULL;
head->pre = NULL;
head->data = x;
} else {
tmp = (struct node *)malloc(1 * sizeof(struct node));
tmp->data = x;
tmp->next = NULL;
tmp->pre = head;
head->next = tmp;
head = tmp;
}
++count;
}
/**
* Pop data from the stack
*/
int pop() {
int returnData;
if(head == NULL) {
printf("ERROR: Pop from empty stack.\n");
exit(1);
} else {
returnData = head->data;
if(head->pre == NULL){
free(head);
head = NULL;
}
else {
head = head->pre;
free(head->next);
}
}
--count;
return returnData;
}
/**
* Returns the next value to be popped.
*/
int peek() {
if(head != NULL)
return head->data;
else {
printf("ERROR: Peeking from empty stack.");
exit(1);
}
}
/**
* Returns the size of the stack.
*/
int size() {
return count;
}
/**
* Returns 1 if stack is empty, returns 0 if not empty.
*/
int isEmpty() {
if(count == 0)
return 1;
return 0;
}