Stack

Discover the Power of Stack: Unleash Your Inner Programmer!

What is Stack

A stack is a fundamental linear data structure that follows the LIFO principle, which stands for Last-In-First-Out. The LIFO principle means that the last item added to the stack is the first to be removed.

In simpler terms, imagine a stack of plates in a restaurant. The last plate added to the stack is the first to be taken off when a customer orders food. This is the same principle that a stack follows in computer science.

A stack is a data structure with two fundamental operations: push and pop. The push operation involves adding an element to the top of the stack. In contrast, the pop operation removes the element at the top of the stack. Other functions, like peek and isEmpty, help you interact with the stack in different ways. With a peek, you can look at the top book without removing it. This can be helpful if you just want to see what’s there without making any changes. And with isEmpty, you can check whether the stack is empty or not.

Application of Stack

Here are some common applications of stacks:

  1. Function Call/Return: When a function is called, the return address and the function parameters are pushed onto the stack. The stack is then used to retrieve the return address and the function parameters when the function returns.
  2.  Expression Evaluation: Infix, postfix and prefix expressions can be evaluated using stacks. For example, to evaluate a postfix expression, we can use a stack to store the operands and then pop them to perform the necessary operations.
  3.  Memory Management: Stacks are used to keep track of allocated memory blocks. When a memory block is allocated, its address is pushed onto the stack. When the memory block is freed, its address is popped from the stack.
  4.  Parsing: Stacks are used in parsing algorithms to keep track of the program’s structure. For example, a stack can be used to check if the brackets in an expression are balanced.
  5.  Undo/Redo: Stacks are used in undo/redo operations in software applications. When a user performs an action, the present state of the application is pushed into the stack. When the user wants to undo the action, the state is popped from the stack, and the application reverts to the previous state.
  6.  Web Browsers: Web browsers use stacks to implement the back and forward buttons. When a user visits a web page, the URL of the page is pushed onto the back stack. When the user clicks the back button, the URL is popped from the back stack and pushed onto the forward stack.

How will you create a stack and perform operations on it using an array illustrate with the programming code or algorithm?

Here is an example implementation of a stack data structure using an array in C, along with code to perform operations on the stack:

#include <stdio.h>
#include <stdlib.h> /* for using exit() */
#define MAXSIZE 100
int stack[MAXSIZE];
int top = -1; /* index pointing to the top of stack */
void push(int);
int pop();
int main()
{
    int ch = 1, i, num;
    while (ch == 1)
    {
        printf("\nMAIN MENU:\n");
        printf("1. Add element to stack (push)\n");
        printf("2. Delete element from the stack (pop)\n");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("\nEnter the data: ");
            scanf("%d", &num);
            push(num);
            break;
        case 2:
            i = pop();
            if (i != -1)
                printf("\nValue returned from pop function is %d\n", i);
            break;
        default:
            printf("Invalid Choice.\n");
        }
        printf("Do you want to do more operations on Stack? (1 for yes, any other key to exit) ");
        scanf("%d", &ch);
    } /* end of outer while */
    return 0;
}
void push(int y)
{
    if (top == MAXSIZE - 1)
    {
        printf("STACK FULL.\n");
        return;
    }
    else
    {
        top++;
        stack[top] = y;
    }
}
int pop()
{
    int a;
    if (top == -1)
    {
        printf("STACK EMPTY.\n");
        return -1; /* to indicate that stack is empty */
    }
    else
    {
        a = stack[top];
        top--;
        return (a);
    }
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *