Stack in C
0 317
Introduction to Stacks in C
In C programming, a stack is a fundamental data structure that follows the Last In, First Out (LIFO) principle.
It's comparable to a stack of books where the last book added is the first one to be taken out.
Stacks find extensive usage in programming due to their simplicity and versatile applications.
Syntax:
A stack is a collection of elements where two primary operations, push and pop, are performed.
The push operation inserts an element onto the top of the stack, while the pop operation extracts the element from the top of the stack.
Additionally, there's a peek operation to examine the top element without removing it.
Example:
//Definition of a Stack Structure in C: #define MAX_SIZE 100 typedef struct { int arr[MAX_SIZE]; int top; } Stack;
Here, we define a structure named Stack containing an array of integers named arr and an integer top representing the current top element index.
MAX_SIZE denotes the maximum capacity of the stack.
Operations on Stack:
Initialization:
void initialize(Stack *stack) { stack->top = -1; }
This function initializes the stack by setting the top index to -1, indicating an empty stack.
Push Operation:
void push(Stack *stack, int element) { if (stack->top == MAX_SIZE - 1) { printf("Stack Overflow! Cannot push element %d\n", element); return; } stack->arr[++stack->top] = element; }
The push function adds an element to the top of the stack.
It first checks if the stack is already full to prevent overflow.
Pop Operation:
int pop(Stack *stack) { if (stack->top == -1) { printf("Stack Underflow! Cannot pop element.\n"); return -1; // Return an invalid value to indicate underflow } return stack->arr[stack->top--]; }
The pop function dequeues and retrieves the topmost element from the stack.
It checks for an empty stack to avoid underflow.
Peek Operation:
int peek(Stack *stack) { if (stack->top == -1) { printf("Stack is empty!\n"); return -1; // Return an invalid value to indicate an empty stack } return stack->arr[stack->top]; }
The peek function returns the top element of the stack without removing it.
It also checks for an empty stack .
Example:
//program fot stack in C #include<stdio.h>#include<stdlib.h> #define MAX_SIZE 100 // Structure to represent the stack typedef struct { int arr[MAX_SIZE]; int top; } Stack; // Function to initialize the stack void initialize(Stack *stack) { stack->top = -1; } // Function to check if the stack is empty int isEmpty(Stack *stack) { return (stack->top == -1); } // Function to check if the stack is full int isFull(Stack *stack) { return (stack->top == MAX_SIZE - 1); } // Function to push an element onto the stack void push(Stack *stack, int element) { if (isFull(stack)) { printf("Stack Overflow! Cannot push element %d\n", element); return; } stack->arr[++stack->top] = element; } // Function to pop an element from the stack int pop(Stack *stack) { if (isEmpty(stack)) { printf("Stack Underflow! Cannot pop element.\n"); exit(1); } return stack->arr[stack->top--]; } // Function to peek the top element of the stack without removing it int peek(Stack *stack) { if (isEmpty(stack)) { printf("Stack is empty!\n"); exit(1); } return stack->arr[stack->top]; } int main() { Stack stack; initialize(&stack); push(&stack, 10); push(&stack, 20); push(&stack, 30); printf("Top element: %d\n", peek(&stack)); printf("Popped element: %d\n", pop(&stack)); printf("Top element after popping: %d\n", peek(&stack)); return 0; }
Output:
Top element: 30 Popped element: 30 Top element after popping: 20
This program initializes a stack, pushes elements onto it, peeks at the top element, and then pops an element from it, demonstrating stack operations
Share:
Comments
Waiting for your comments