#include <stdio.h>
#include <stdlib.h>
// Node for linked list stack
typedef struct Node {
int data;
struct Node* next;
} Node;
// Linked list stack push
void pushLinkedList(Node** top, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = *top; // link new node to current top
*top = newNode; // update top to new node
}
// Linked list stack pop
int popLinkedList(Node** top) {
if (*top == NULL) return -1; // empty stack
Node* temp = *top;
int val = temp->data;
*top = temp->next; // move top down
free(temp);
return val;
}
// Array stack structure
typedef struct {
int* arr;
int capacity;
int top;
} ArrayStack;
// Initialize array stack
ArrayStack* createArrayStack(int capacity) {
ArrayStack* stack = (ArrayStack*)malloc(sizeof(ArrayStack));
stack->capacity = capacity;
stack->top = -1;
stack->arr = (int*)malloc(capacity * sizeof(int));
return stack;
}
// Array stack push
int pushArray(ArrayStack* stack, int value) {
if (stack->top == stack->capacity - 1) return -1; // full
stack->arr[++stack->top] = value; // increment top and add
return 0;
}
// Array stack pop
int popArray(ArrayStack* stack) {
if (stack->top == -1) return -1; // empty
return stack->arr[stack->top--]; // return top and decrement
}
// Print array stack
void printArrayStack(ArrayStack* stack) {
for (int i = 0; i <= stack->top; i++) {
printf("[%d] %d\n", i, stack->arr[i]);
}
printf(" ↑ top\n");
}
// Print linked list stack
void printLinkedListStack(Node* top) {
Node* curr = top;
while (curr) {
printf("%d -> ", curr->data);
curr = curr->next;
}
printf("null\n ↑ top\n");
}
int main() {
// Create array stack with capacity 5
ArrayStack* arrayStack = createArrayStack(5);
Node* linkedListStack = NULL;
// Push 10, 20, 30
pushArray(arrayStack, 10);
pushArray(arrayStack, 20);
pushArray(arrayStack, 30);
pushLinkedList(&linkedListStack, 10);
pushLinkedList(&linkedListStack, 20);
pushLinkedList(&linkedListStack, 30);
// Print both stacks
printf("Array Stack:\n");
printArrayStack(arrayStack);
printf("Linked List Stack:\n");
printLinkedListStack(linkedListStack);
// Pop one item from each
popArray(arrayStack);
popLinkedList(&linkedListStack);
// Print after pop
printf("\nAfter one pop:\n");
printf("Array Stack:\n");
printArrayStack(arrayStack);
printf("Linked List Stack:\n");
printLinkedListStack(linkedListStack);
// Free linked list
while (linkedListStack) {
Node* temp = linkedListStack;
linkedListStack = linkedListStack->next;
free(temp);
}
free(arrayStack->arr);
free(arrayStack);
return 0;
}
newNode->next = *top; // link new node to current top
*top = newNode; // update top to new node
add new node at front to push onto linked list stack
*top = temp->next; // move top down
free(temp);
remove front node to pop from linked list stack
stack->arr[++stack->top] = value; // increment top and add
increment top index and add value to array stack
return stack->arr[stack->top--]; // return top and decrement
return top value and decrement top index to pop from array stack
Array Stack:
[0] 10
[1] 20
[2] 30
↑ top
Linked List Stack:
30 -> 20 -> 10 -> null
↑ top
After one pop:
Array Stack:
[0] 10
[1] 20
↑ top
Linked List Stack:
20 -> 10 -> null
↑ top