STACK through C

 

Stack in Data Structures 

1. Introduction to Stack

A stack is one of the most important linear data structures used in computer science. It follows a specific order for storing and accessing data known as the LIFO principle (Last In, First Out).

This means that the element inserted last into the stack will be the first one to be removed.

A simple real-life example of a stack is a stack of plates in a kitchen. When plates are placed one on top of another, the last plate placed on top is the first one removed.

Stacks are widely used in:

  • Programming languages
  • Operating systems
  • Expression evaluation
  • Memory management
  • Undo/redo operations



2. Basic Concept of Stack

A stack works like a vertical structure where elements are inserted and removed from the same end, called the top.

Two important operations are performed on a stack:

  1. Push – Insert an element into the stack
  2. Pop – Remove an element from the stack

The position from which insertion and deletion occur is called the TOP.

Example:

Top
 |
30
20
10

If we push 40, the stack becomes:

Top
 |
40
30
20
10

If we pop an element, 40 will be removed first.


3. Characteristics of Stack

The stack data structure has several important characteristics:

  1. It follows the LIFO principle (Last In First Out).
  2. Insertion and deletion happen only from one end (Top).
  3. It is a linear data structure.
  4. It can be implemented using arrays or linked lists.
  5. The stack size may be fixed or dynamic.

4. Basic Operations of Stack

The stack supports several basic operations.

4.1 Push Operation

The push operation is used to insert a new element into the stack.

Steps:

  1. Check if the stack is full.
  2. Increase the value of top.
  3. Insert the element at the top position.

Example:

Before push:

Top
 |
30
20
10

Push 40:

Top
 |
40
30
20
10

4.2 Pop Operation

The pop operation removes the top element from the stack.

Steps:

  1. Check if the stack is empty.
  2. Store the top element.
  3. Decrease the top value.

Example:

Before pop:

Top
 |
40
30
20
10

After pop:

Top
 |
30
20
10

4.3 Peek Operation

The peek operation returns the top element of the stack without removing it.

Example:

Stack:

Top
 |
30
20
10

Peek → returns 30


4.4 isEmpty Operation

This operation checks whether the stack is empty.

Condition:

top == -1

4.5 isFull Operation

This operation checks whether the stack is full.

Condition:

top == MAX - 1

5. Stack Implementation Using Array in C

A stack can be implemented using arrays in the C programming language.

Stack Structure

We define:

  • Array to store elements
  • Variable top to track the top position

Example:

#include <stdio.h>
#define MAX 5

int stack[MAX];
int top = -1;

Push Operation in C

void push(int value)
{
    if(top == MAX-1)
    {
        printf("Stack Overflow\n");
    }
    else
    {
        top++;
        stack[top] = value;
    }
}

Pop Operation in C

void pop()
{
    if(top == -1)
    {
        printf("Stack Underflow\n");
    }
    else
    {
        printf("Deleted element: %d\n", stack[top]);
        top--;
    }
}

Display Stack

void display()
{
    int i;
    if(top == -1)
    {
        printf("Stack is empty\n");
    }
    else
    {
        for(i = top; i >= 0; i--)
        {
            printf("%d\n", stack[i]);
        }
    }
}

6. Stack Implementation Using Linked List

Another way to implement a stack is using a linked list.

In this method:

  • Each node contains data
  • A pointer to the next node

Structure of node:

struct node
{
    int data;
    struct node *next;
};

Advantages:

  • Dynamic memory allocation
  • No fixed size

7. Stack Overflow and Stack Underflow

Stack Overflow

Stack overflow occurs when we try to insert an element into a full stack.

Example condition:

top == MAX - 1

Example message:

Stack Overflow

Stack Underflow

Stack underflow occurs when we try to remove an element from an empty stack.

Example condition:

top == -1

Example message:

Stack Underflow

8. Applications of Stack

Stacks are widely used in computer science.

8.1 Function Calls

When a function is called in a program, the system stores the function details in a call stack.

Example:

  • Local variables
  • Return address
  • Parameters

8.2 Expression Evaluation

Stacks are used to evaluate expressions such as:

  • Infix
  • Prefix
  • Postfix expressions

Example:

A + B

Can be converted to:

AB+

8.3 Parenthesis Checking

Stacks help check balanced parentheses.

Example:

( A + B ) * ( C + D )

8.4 Undo and Redo Operations

Many applications use stacks for undo operations.

Examples:

  • Text editors
  • Photoshop
  • Word processors

8.5 Backtracking Algorithms

Stacks are used in algorithms like:

  • Maze solving
  • Depth First Search (DFS)
  • Path finding

8.6 Browser History

Web browsers use stacks to store visited pages.

Example:

  • Back button
  • Forward button

9. Advantages of Stack

Stacks provide several benefits:

  1. Simple implementation
  2. Efficient insertion and deletion
  3. Useful in recursion
  4. Helps manage function calls
  5. Important for expression evaluation

10. Disadvantages of Stack

Despite its advantages, stacks also have limitations:

  1. Limited access (only top element)
  2. Fixed size in array implementation
  3. Overflow and underflow problems
  4. Not suitable for searching elements

11. Real Life Examples of Stack

Stacks can be observed in many real-world situations.

Examples:

  1. Stack of books
  2. Stack of plates
  3. Browser history
  4. Undo operations in software
  5. Function call stack in programming

12. Conclusion

A stack is a fundamental data structure used in programming and computer science. It follows the Last In First Out (LIFO) principle, meaning the last element inserted is the first one removed.

The main operations of a stack include push, pop, peek, isEmpty, and isFull. Stacks can be implemented using arrays or linked lists in the C programming language.

Stacks are extremely useful in many areas such as function calls, expression evaluation, recursion, browser history, and undo operations. Due to their simplicity and efficiency, stacks remain one of the most widely used data structures in software development.

Understanding stacks is essential for students learning data structures and algorithms, as they form the foundation for solving many complex programming problems.

Comments

Popular Posts