-
Notifications
You must be signed in to change notification settings - Fork 1
/
Array Stack.c
164 lines (132 loc) · 3.57 KB
/
Array 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct ArrayStackNodeType {
char data;
}ArrayStackNode;
typedef struct ArrayStackType {
int maxcount; // Maxium number of data.
int currentcount; // Currnet number of stored data.
ArrayStackNode* pData; // 1 dimensional array to store data.
}ArrayStack;
ArrayStack* createArrayStack(int size) {
ArrayStack* pReturn = NULL;
pReturn = (ArrayStack*)malloc(sizeof(ArrayStack)); // Memory allocation.
memset(pReturn, 0, sizeof(ArrayStack)); // Memory initialization.
pReturn->maxcount = size; // Set Maximum number of data.
// Creating and initializing arrays with elements as many as the max number of data.
pReturn->pData = (ArrayStackNode*)malloc(sizeof(ArrayStackNode) * size);
memset(pReturn->pData, 0, sizeof(ArrayStackNode) * size);
return pReturn;
}
// Check if the array stack is full.
int isArrayStackFull(ArrayStack* pStack) {
int ret = 0;
if (pStack != NULL) {
if (pStack->currentcount == pStack->maxcount) {
ret = 1;
}
}
return ret;
}
// push, AS: ArrayStack
int pushAS(ArrayStack* pStack, char data) {
int ret = 0;
// determine if the array stack is full.
if (isArrayStackFull(pStack) == 0) {
// push data to TOP.
pStack->pData[pStack->currentcount].data = data;
// Change the position of TOP.
pStack->currentcount++;
ret = 1;
}
else {
printf("Error, stack is full !, pushAS()\n");
}
return ret;
}
int isArrayStackEmpty(ArrayStack* pStack) {
int ret = 0;
if (pStack != NULL) {
if (pStack->currentcount == 0) {
ret = 1;
}
}
return ret;
}
ArrayStackNode* popAS(ArrayStack* pStack) {
ArrayStackNode* pReturn = NULL;
// Check if the array stack if empty.
if (0 == isArrayStackEmpty(pStack)) {
// Memory allocation and memory check for nodes to return.
pReturn = (ArrayStackNode*)malloc(sizeof(ArrayStackNode));
if (pReturn != NULL) {
// Enter the data to be returned to the node.
pReturn->data = pStack->pData[pStack->currentcount - 1].data;
// Change the position of TOP, to left.
pStack->currentcount--;
}
else {
printf("Error, memory allocation, popAS()\n");
}
}
return pReturn;
}
ArrayStackNode* peekAS(ArrayStack* pStack) {
ArrayStackNode* pReturn = NULL;
if (pStack != NULL) {
// Check if the array stack is empty.
if (isArrayStackEmpty(pStack) == 0) {
// Speicfy a return value, return a pointer that points the top node of the array stack.
pReturn = &(pStack->pData[pStack->currentcount - 1]);
}
}
return pReturn;
}
void deleteArrayStack(ArrayStack* pStack) {
if (pStack != NULL) {
if (pStack->pData != NULL) {
free(pStack->pData);
}
free(pStack);
}
}
void displayArrayStack(ArrayStack* pStack) {
int i = 0;
if (pStack != NULL) {
int size = pStack->maxcount;
int top = pStack->currentcount;
printf("Stack Size: %d, Number of nodes: %d\n", pStack->maxcount, pStack->currentcount);
for (i = size - 1; i >= top; i--) {
printf("[%d]-[Empty]\n", i);
}
for (i = top-1; i >= 0; i--) {
printf("[%d]-[%c]\n", i, pStack->pData[i].data);
}
}
}
int main(int argc, char* argv[]) {
ArrayStack* pStack = NULL;
ArrayStackNode* pNode = NULL;
pStack = createArrayStack(6);
if (pStack != NULL) {
pushAS(pStack, 'A');
pushAS(pStack, 'B');
pushAS(pStack, 'C');
pushAS(pStack, 'D');
displayArrayStack(pStack);
pNode = popAS(pStack);
if (pNode != NULL) {
printf("Pop value-[%c]\n", pNode->data);
free(pNode);
}
displayArrayStack(pStack);
pNode = peekAS(pStack);
if (pNode != NULL) {
printf("Peek value-[%c]\n", pNode->data);
}
displayArrayStack(pStack);
deleteArrayStack(pStack);
}
return 0;
}