Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a display method to display all the elements in the stack classes. pac

ID: 3537732 • Letter: I

Question


Implement a display method to display all the elements in the stack classes.



package code;

public class Stack {
    private char[] list;
    private int size;
    private int n;
       
    public Stack() {
        size = 100;
        list = new char[size];
        n = 0;
    }
    public Stack(int s) {
        size = s;
        list = new char[size];
        n = 0;
    }
   
    public void push (char c) {
        if (n >= size) {
            System.out.println("The stack is full!");
        } else {
            list[n] = c;
            n++;
        }
    }
   
    public char pop() {
        if (n <= 0) {
            System.out.println("The stack is empty");
            return ' ';
        } else {
            n--;
            return list[n];
        }
    }
   
    public char peek() {
        if (n <= 0) {
            System.out.println("The stack is empty");
            return ' ';
        }else {
            return list[n - 1];
        }
    }
   
    public int search(char ch) {
        for (int i = n - 1; i >= 0; i--) {
            if (list[i] == ch) {
                return i;
            }
        }
        return -1;
    }
   
    public boolean isEmpty() {
        return n <= 0;
    }
       
}


Implement a display method to display all the elements in the queue class.


package code;

public class Queue {
    private char[] list;
    private int size;
    private int count;
    private int front, back;
   
    public Queue() {
       size = 100;
       list = new char[size];
       front = 0;
       back = size - 1;
       count = 0;
    }
   
    public Queue (int s) {
        size = s;
        list = new char[size];
        front = 0;
        back = size - 1;
        count = 0;
    }


Explanation / Answer


//DISPLAY METHOD FOR STACK CLASS

public void display()

{

System.out.println("Displaying Stack Elements,Top to Bottom:");

for (int i = n - 1; i >= 0; i--)

{ System.out.println(list[i]);

}

}



//DISPLAY METHOD FOR QUEUE CLASS

public void display()

{

System.out.println("Displaying Queue Elements,front to back:");

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

{ System.out.println(list[i]);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote