Stacks with C
Basic template and methods

Operations:

int pop: return the value of the top node and remove it void push: add a new node as top node int peek: return the value of the top node void printStack

Construct Stack by Array:

1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <stdbool.h>
4
#include <limits.h>
5
​
6
​
7
struct Stack {
8
unsigned capacity;
9
int top;
10
int* array;
11
};
12
​
13
struct Stack* createStack(unsigned capacity){
14
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
15
stack -> capacity = capacity;
16
stack -> top = -1;
17
stack -> array = (int*)malloc(stack -> capacity * (sizeof(int)));
18
19
return stack;
20
}
21
​
22
​
23
bool isFull(struct Stack* stack){
24
return stack -> top == stack -> capacity - 1;
25
}
26
​
27
bool isEmpty(struct Stack* stack){
28
return stack -> top == - 1;
29
}
30
​
31
​
32
int pop(struct Stack* stack){
33
if(isEmpty(stack)){
34
return INT_MIN;
35
}
36
return stack->array[stack->top--];
37
}
38
​
39
​
40
void push(struct Stack* stack, int data){
41
if(isFull(stack)){
42
return;
43
}
44
stack->array[++stack->top] = data;
45
}
46
​
47
int peak(struct Stack* stack){
48
if(isEmpty(stack)){
49
return INT_MIN;
50
}
51
return stack->array[stack->top];
52
}
53
​
54
void printStack(struct Stack* stack){
55
if(isEmpty(stack)){
56
return;
57
}
58
59
for(int i = 0; i < stack->capacity; i++){
60
printf("stack[%d] = %d \n", i, stack->array[i]);
61
}
62
}
63
​
Copied!

Test Code:

1
int main(){
2
​
3
struct Stack* stack = createStack(4);
4
5
push(stack, 10);
6
push(stack, 20);
7
push(stack, 30);
8
9
printf("%d popped from stack\n", pop(stack));
10
11
printStack(stack);
12
​
13
return 0;
14
}
Copied!

Construct Stack by Linked List:

1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <stdbool.h>
4
#include <limits.h>
5
​
6
struct stackNode {
7
int data;
8
struct stackNode* next;
9
};
10
​
11
struct stackNode* newNode(int data){
12
struct stackNode* node = (struct stackNode*) malloc(sizeof(struct stackNode));
13
node -> data = data;
14
node -> next = NULL;
15
​
16
return node;
17
}
18
​
19
​
20
bool isEmpty(struct stackNode* top){
21
return !top;
22
}
23
​
24
​
25
int pop(struct stackNode** top){
26
if(isEmpty(*top)){
27
return INT_MIN;
28
}
29
30
struct stackNode* tmpNode = *top;
31
*top = (*top)->next;
32
​
33
int popped = tmpNode->data;
34
free(tmpNode);
35
36
return popped;
37
}
38
​
39
​
40
void push(struct stackNode** top, int data){
41
​
42
struct stackNode* node = newNode(data);
43
44
node->next = *top;
45
*top = node;
46
}
47
​
48
int peek(struct stackNode* top){
49
if(isEmpty(top)){
50
return INT_MIN;
51
}
52
return top->data;
53
}
54
​
55
void printStack(struct stackNode** top){
56
if(isEmpty(*top)){
57
return;
58
}
59
60
int i = 0;
61
while( *top != NULL){
62
printf("top[%d] = %d \n", i, (*top)->data);
63
*top = (*top)->next;
64
}
65
}
Copied!

Test Code:

1
int main(){
2
​
3
struct stackNode* root = NULL;
4
5
push(&root, 10);
6
push(&root, 20);
7
push(&root, 30);
8
9
printf("%d popped from stack\n", pop(&root));
10
11
printf("Top element is %d\n", peek(root));
12
13
14
printStack(&root);
15
​
16
return 0;
17
}
Copied!