Chain Dequeue Program ISC 2010 Theory

Chain is an entity which can hold at the most 50 integers. The chain enables the user to add and remove integers from both the ends i.e. front and rear. Define a class Chain with the following details:

Class name: Chain
Data members:
ele[]: the array to hold the integer elements.
cap: stores the maximum capacity of the array.
front: to point the index of the front.
rear: to point the index of the rear.
Member functions:
Chain(int max): constructor to initialize the data cap = max, front = rear = -1 and to create the integer array.
void pushFront(int v): to add integers from the front index if possible else display the message “full from front”.
int popFront(): to remove and return elements from front. If the array is empty then return -999.
void pushRear(int v): to add integers from the rear index if possible else display the message “full from rear”.
int popRear(): to remove and return elements from rear. If the array is empty then return -999.

Specify the class Chain giving details of the constructor and the member functions void pushFront(int), int popFront(), void pushRear(int) and int popRear().

The main() function need not be written.

import java.io.*;
class Chain{
    int ele[];
    int cap;
    int front;
    int rear;
    public Chain(int max){
        cap = max;
        ele = new int[cap];
        front = -1;
        rear = -1;
    }
    public void pushFront(int v){
        if(front == -1){
            front = 0;
            rear = 0;
            ele[front] = v;
        }
        else if(front == 0)
            System.out.println("Full from front.");
        else
            ele[--front] = v;
    }
    public int popFront(){
        int data = 0;
        if(front == -1)
            return -999;
        else{
            data = ele[front];
            if(front == rear){
                front = -1;
                rear = -1;
            }
            else
                front++;
            return data;
        }
    }
    public void pushRear(int v){
        if(rear == -1){
            front = 0;
            rear = 0;
            ele[rear] = v;
        }
        else if(rear + 1 < cap)
            ele[++rear] = v;
        else
            System.out.println("Full from rear.");
    }
    public int popRear(){
        int data = 0;
        if(rear == -1)
            return -999;
        else{
            data = ele[rear];
            if(front == rear){
                front = -1;
                rear = -1;
            }
            else
                rear--;
            return data;
        }
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Queue size: ");
        int size = Integer.parseInt(br.readLine());
        if(size > 50)
            size = 50;
        Chain obj = new Chain(size);
        while(true){
            System.out.println("1. Push from the front");
            System.out.println("2. Pop from the front");
            System.out.println("3. Push from the rear");
            System.out.println("4. Pop from the rear");
            System.out.print("Enter your choice: ");
            int choice = Integer.parseInt(br.readLine());
            switch(choice){
                case 1:
                System.out.print("Data to be pushed: ");
                int data = Integer.parseInt(br.readLine());
                obj.pushFront(data);
                break;
                case 2:
                data = obj.popFront();
                if(data == -999)
                    System.out.println("Underflow!");
                else
                    System.out.println(data + " popped from the front.");
                break;
                case 3:
                System.out.print("Data to be pushed: ");
                data = Integer.parseInt(br.readLine());
                obj.pushRear(data);
                break;
                case 4:
                data = obj.popRear();
                if(data == -999)
                    System.out.println("Underflow!");
                else
                    System.out.println(data + " popped from the rear.");
                break;
                default:
                System.out.println("Bye...");
                return;
            }
        }
    }
}

9 thoughts on “Chain Dequeue Program ISC 2010 Theory

  1. Sir,a program is given which states that:
    WAP in Java to accept 2 strings. Display the new string by taking each character of the 1st string from left to right and of the second string from right to left.The letters should be taken alternatively from each string.
    The length of both the strings must be same.
    Sample Input:
    String 1: HISTORY
    String 2: SCIENCE
    Sample Output:
    HECISNTEOIRCYS

  2. Sir,a program is given which states that:
    WAP in JAVA to accept a string.Count & display the frequency of each character present in the string.The character with multiple frequencies should be displayed only once.
    Sample Input:golden jubilee
    Sample Output:
    Alphabet Frequency
    g——-> 1
    o——-> 1
    l——–> 2
    d——–> 1
    e———> 3
    n———->1
    j———->1
    u———->1
    b———->1
    i———->1

    • public void display(){
      if(front == -1 && rear == -1)
      System.out.println(“Queue empty”);
      else{
      for(int i = front; i <= rear; i++) System.out.print(ele[i] + "\t"); System.out.println(); } }

    • Sir,a program is given which states that:
      WAP in JAVA to accept a string.Count & display the frequency of each character present in the string.The character with multiple frequencies should be displayed only once.
      Sample Input:golden jubilee
      Sample Output:
      Alphabet Frequency
      g——-> 1
      o——-> 1
      l——–> 2
      d——–> 1
      e———> 3
      n———->1
      j———->1
      u———->1
      b———->1
      i———->1
      How to display the output in the following order as mentioned in the question?

Leave a Reply