Data Structures
Array Stack
A stack stores new values at the top and removes the newest value first.
Array Stack
array_stack.c
#include <stdio.h>
int main(void) {
int limit = ;
int stack[4] = {0, 0, 0, 0};
int top = 0;
for (int i = 0; i < limit; i++) {
stack[top] = i + 1;
top = top + 1;
}
top = top - 1;
int popped = stack[top];
printf("top=%d popped=%d\n", top, popped);
return 0;
}
#include <stdio.h>
int main(void) {
int limit = ;
int stack[4] = {0, 0, 0, 0};
int top = 0;
for (int i = 0; i < limit; i++) {
stack[top] = i + 1;
top = top + 1;
}
top = top - 1;
int popped = stack[top];
printf("top=%d popped=%d\n", top, popped);
return 0;
}
#include <stdio.h>
int main(void) {
int limit = ;
int stack[4] = {0, 0, 0, 0};
int top = 0;
for (int i = 0; i < limit; i++) {
stack[top] = i + 1;
top = top + 1;
}
top = top - 1;
int popped = stack[top];
printf("top=%d popped=%d\n", top, popped);
return 0;
}
top index
The `top` index points one past the newest value in the stack.
last in first out
Decrementing `top` before reading removes the most recently pushed value.