Stack ADT

 

Stack ADT

Handwritten Notes- Click Here

A Stack is a linear data structure in which insertion and deletion of elements are performed only at one end. This end is called the TOP.

Stack follows the LIFO (Last In, First Out) principle. It means the element inserted last will be removed first.

A stack can be implemented using:

  • Array
  • Linked List

Basic Stack Operations:

1. Push-used to insert a new element into the stack.

2. Pop-used to remove an element from the stack.

3. Peek-used to view the element at the TOP without removing it.

4. isEmpty-checks whether the stack contains any elements.

5. isFull-checks whether the stack has reached its maximum capacity


Push() Operation

The process of inserting a new element at the TOP of the stack is called Push.

Algorithm

  1. Check whether the stack is full.
  2. If the stack is full, report Stack Overflow.
  3. Otherwise, increment TOP.
  4. Insert the new element at the TOP position. 

Before Push

 D

 C

 B

 A  

->Top                                                                             





Push E into the stack.
After Push   
                     

 E

 D

 C

 B

 A

->Top                                                  
            





C Program

void push(int data)
{
    if (top == MAXSIZE - 1)
    {
        printf("Stack Overflow");
    }
    else
    {
        top++;
        stack[top] = data;
    }
}


Pop() Operation

The process of removing the top element from the stack is called Pop.

The element at the TOP is always removed first.

Algorithm

  1. Check whether the stack is empty.
  2. If the stack is empty, report Stack Underflow.
  3. Store the element present at TOP.
  4. Decrement TOP.
  5. Return the deleted element. 

Before Pop                   

 E

 D

 C

 B

 A

->Top                                                  
            




Perform Pop.The element E is removed.

After Pop 

 D

 C

 B

 A  

->Top                                                                             





C Program

int pop()
{
    int data;
    if (top == -1)
    {
        printf("Stack Underflow");
        return -1;
    }

    data = stack[top];
    top--;
    return data;
}


Video Explanation



Comments

Popular posts from this blog

Queue ADT

Entity-Relationship(ER) Model

Normalization in DBMS,(1NF,2NF,3NF,BCNF,4NF,5NF)