Stack and Queue in DSA

Stack and Queue

stack and queue


Stack

Stack is a data structure in which elements are inserted and removed from one end known as TOP. The last element inserted into the stack is the first element to be removed, it is Last In First Out (LIFO). Inserting element is known as PUSH operation and removing element from the stack is known as POP operation.

Push and Pop operations in stack

          class stack_array{

          int[] data;

          int top;

          final int size = 10;

          public stack_array(){

                    data = new int[size];

                    top = -1;

          }

          public void push(int x){

                    if(top<size){

                              ++top;

                              data[top] = x;

                    }else System.out.println("Stack is full.");

          }

          public void pop(){

                    if(top==-1){

                              System.out.println("Stack is empty!!!");

                    }else{

                              System.out.println("\nPopped Element is : "+data[top]);

                              --top;

                    }

          }

          public void show(){

                    for(int i=0;i<=top;i++)

                              System.out.print("  "+data[i]);

                    System.out.println("\n");

          }

}

                   

Queue

Queue is linear data structure like stack in which elements are inserted from one end known as rear and removed from another end known as front.

Insert and Remove in Queue

class queue_array{

          int[] data;

          int front,rear;

          public queue_array(){

                    data = new int[10];

                    front = 0;

                    rear = -1;

          }

          public void insert(int item){

                    if(rear < 10){

                              rear = rear+1;

                              data[rear] = item;

                    }else System.out.println("queue is full");

          }

          public void remove(){

                    if(rear != -1 && rear < 10){

                              System.out.println("Removed class: "+ data[front]);

                              ++front;

                    }

          }

          public void show(){

                    for(int i=front;i<=rear;i++)

                              System.out.println(" "+data[i]);

          }

}

Circular Queue

Queue with last element attached near the first element of the queue is circular Queue.

class cir_queue_array{

          int[] data;

          int front,rear,count;

          public cir_queue_array(){

                    data = new int[10];

                    front = 0;

                    rear = -1;

                    count = 0;

          }

          public void insert(int item){

                    if(count <10){

                              rear = (rear+1)%10;

                              ++count;

                              data[rear] = item;

                    }else System.out.println("queue is full");

          }

          public void remove(){

                    if(count != -1){

                              System.out.println("Removed class: "+ data[front]);

                              front = (front+1)%10;

                              --count;

                    }

          }

          public void show(){

                    int i=0;

                    for(i=front;i!=rear;i=(i+1)%10)

                              System.out.println(i+" : "+data[i]);

                    System.out.println(i+" : "+data[i]);

                    System.out.println("Count: "+count+"\n");

          }

}

 

Stack of User Defined Data type

import java.util.*;

class student{

    int roll;

    String name;

    String address;

    public void set(int roll,String name,String add){

        this.roll = roll;

        this.name = name;

        this.address = add;

    }

    public void show(){

        System.out.println("roll: "+roll+" Name: "+name+" Address: "+address);

    }

    public int getID()

    {

        return roll;

    }

}

class stack_student{

    public static void main(String[] owl){

        int i=0;

        student[] s = new student[10];//array  stack

        student ss = new student();

        for(i = 0;i<10;i++){

            s[i] = new student();

        }

        int top = -1;

        int option,id=0;

        Scanner sc = new Scanner(System.in);

        while(true){

            System.out.println("1.Push 2.Pop 3.Display 4.Exit 66.Search");

            System.out.println("Enter choice: ");

            option = sc.nextInt();

            switch(option){

                case 1: ss = new student();                       

                        System.out.println("Name: ");String name = sc.next();

                        System.out.println("Address: ");String address = sc.next();

                        ss.set(++id,name,address);

                        if(top < 10){

                            s[++top] = ss;

                        }

                        System.out.println("TOP: "+top);

                break;

                case 2: System.out.println("Popping element");

                            s[top].show();

                        --top;

                        System.out.println("TOP: "+top);

                break;

                case 3:

                        for(i =0;i<=top;i++){

                            s[i].show();

                        }

                break;

                case 66: System.out.println("Enter ID to Search: ");

                         int idd = sc.nextInt();

                          for(i=0;i<=top;i++){

                              if(s[i].getID() == idd)

                                {

                                    s[i].show();

                                    break;

                                }

                          }     

                break;

                case 4: System.exit(0);

            }

        }

    }

}

Post a Comment

Previous Post Next Post