Stack in Data Structure

stack in data structure Definition -

Stack is a dynamic data structure with verity of application in the field of computing it is known as dynamic data structure because stack is Last In First Out (LIFO) data structure because new items are added to the top of the stack and item can only be removed from top. A stack is also known as Abstract Data Type.

Real World Example of Stack -

  • A stack of neatly folded shirts.
  • A stack of coin. A stack of plates.

To implement a stack you can use array variable. 

We will also declare one variable to check the size of stack and pointer variable to indicate the top of the stack, in empty stack top variable will be zero. 

Now if you want to enter some data into stack i.e called push operation. This involve increment value of top variable.

Below diagram shows stack push operation in stack.

stack in data structurestack in data structure

Taking data from stack is called pop operation. we will copy the data from top position and decrements the value of pop.

Below diagram show pop operation in stack.

stack in data structureStack Pseudo code -

Dim StackArray(5) As integer
Const MaxSize As Integer = 5
Dim Top As Integer

Push Operation 
 IF Top = MaxSize THEN
        OUTPUT = "Stack is FULL"
 ELSE 
    Top = Top +1
	StackArray(Top) = new item
 END IF
 END Push
 
Pop Operation
IF Top =0 THEN
   OUTPUT = "Stack is empty"
   copy item = StackArray(TOP)
   Top = Top -1
 END IF
 END Pop

Array Stack Implementation in Java -

/**
 Push and POP Operation*
 */
package stack;
import java.util.Scanner;

/**
 * @author dubeyd
 *
 */
public class Array_Stack_Impl {
    static int top=-1;
    static int capacity;
    int array[] = new int[capacity];
    
    public void push(int data) throws StackException{
        if(top>=capacity-1){
            throw new StackException("StackOverFlow");
        }else{
            top++;
            array[top]=data;
            
        }
        
    }
    public int pop() throws StackException{
        int localCount=0;
        if(top<localCount){
            throw new StackException("StackUnderFlow");
        }
        int value=0;
        value=array[top];
        array[top]=0;
        top--;
        System.out.println(value);
        return value;
    }

Peek, isEmpty, isFull operation method -

public int peek(){
    if(top<0){
        System.out.println("No element present to peek");
        return 0;
    }
    int value=0;
    value=array[top];
    System.out.println(value);
    return value;
}
public boolean isEmpty(){
    boolean flag = false;
    if(top==-1){
        flag=true;
    }
    return flag;
}
public boolean isFull(){
    boolean flag=false;
    if(capacity==top+1){
        flag=true;
    }
    return flag;
}
public void dispaly(){
    int localCount=0;
    if(top<0){
        System.out.println("Empty Stack");
        return;
    }
    while(localCount<=top){
        System.out.print(array[localCount]+ " ");
        localCount++;
    }
    System.out.println();
    
}

Main method -

 public static void main(String args[]) throws StackException{
       Scanner sc = new Scanner(System.in);
       System.out.println("Please enter Array capacity");
       capacity=sc.nextInt();
       Array_Stack_Impl stack=new Array_Stack_Impl();
       stack.push(5);
       stack.push(15);
       stack.push(65);
       stack.dispaly();
       stack.pop();
       stack.dispaly();
       stack.push(65);
       stack.push(75);
       stack.dispaly();
       stack.pop();
       stack.pop();
       stack.pop();
       stack.pop();
       stack.peek();
       System.out.println( stack.isEmpty());
       System.out.println(stack.isFull());
       sc.close();
    }

}

class StackException extends Exception{
   StackException(String s){
    super(s);
    }
}

Leave a Comment

%d bloggers like this: