C Stack – Learn Stack Implementation in C
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. Elements are added (pushed) and removed (popped) from the top of the stack.
๐น What is a Stack?
A stack is a collection of elements with two primary operations: push (adding an element to the top) and pop (removing the top element). The stack follows the LIFO order, meaning the last element added is the first one to be removed.
๐ Example 1: Stack Structure and Operations
This example demonstrates how to create a simple stack structure in C, including basic push and pop operations.
#include <stdio.h> #include <stdlib.h> #define MAX 5 // Define maximum size of stack struct Stack { int arr[MAX]; // Array to store stack elements int top; // Index of the top element }; // Function to initialize the stack void initStack(struct Stack* stack) { stack->top = -1; // Set top to -1, indicating stack is empty } // Function to check if stack is full int isFull(struct Stack* stack) { return stack->top == MAX - 1; // Return 1 if stack is full } // Function to check if stack is empty int isEmpty(struct Stack* stack) { return stack->top == -1; // Return 1 if stack is empty } // Function to push an element to the stack void push(struct Stack* stack, int value) { if (isFull(stack)) { printf("Stack Overflow!\n"); } else { stack->arr[++(stack->top)] = value; // Increment top and add value to stack printf("Pushed %d to stack\n", value); } } // Function to pop an element from the stack int pop(struct Stack* stack) { if (isEmpty(stack)) { printf("Stack Underflow!\n"); return -1; // Return -1 if stack is empty } else { return stack->arr[(stack->top)--]; // Return top element and decrement top } } int main() { struct Stack stack; initStack(&stack); push(&stack, 10); push(&stack, 20); push(&stack, 30); printf("Popped element: %d\n", pop(&stack)); printf("Popped element: %d\n", pop(&stack)); return 0; }
๐น Peek Operation
In addition to push and pop, a stack also supports the peek operation, which allows you to view the top element without removing it from the stack.
๐ Example 2: Peek Operation
This example demonstrates how to implement a peek function to view the top element of the stack without removing it.
#include <stdio.h> #include <stdlib.h> #define MAX 5 struct Stack { int arr[MAX]; int top; }; void initStack(struct Stack* stack) { stack->top = -1; } int isFull(struct Stack* stack) { return stack->top == MAX - 1; } int isEmpty(struct Stack* stack) { return stack->top == -1; } void push(struct Stack* stack, int value) { if (isFull(stack)) { printf("Stack Overflow!\n"); } else { stack->arr[++(stack->top)] = value; printf("Pushed %d to stack\n", value); } } int pop(struct Stack* stack) { if (isEmpty(stack)) { printf("Stack Underflow!\n"); return -1; } else { return stack->arr[(stack->top)--]; } } // Function to peek at the top element int peek(struct Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty!\n"); return -1; } else { return stack->arr[stack->top]; // Return the top element } } int main() { struct Stack stack; initStack(&stack); push(&stack, 10); push(&stack, 20); push(&stack, 30); printf("Top element is: %d\n", peek(&stack)); printf("Popped element: %d\n", pop(&stack)); return 0; }
๐น Stack Applications
Stacks are widely used in various applications, including:
- Expression evaluation (e.g., infix, postfix, and prefix expressions)
- Function calls (call stack in programming languages)
- Undo functionality in text editors or browsers
๐ฏ Key Takeaways
- A stack follows the LIFO principle: the last element added is the first to be removed.
- Common stack operations include push, pop, and peek.
- Stacks are essential for implementing recursive functions and managing function calls in memory.
๐ Practice Time!
Try modifying the examples to experiment with different stack sizes, adding more elements, and performing operations in various orders!