For the following code Add a poll method that returns the in

For the following code:

Add a poll() method that returns the integer value of the front element in the queue and removes its from the queue. Add a peek() method that returns the value of the front element but does not remove it from the queue. Create a new class called ArrayStack. Implement a stack using an array. Add all stack methods: push(), pop(), peek(), isEmpty(), and print().

public class ArrayQueue {

        private int data[];

        private int capacity=100; //maximum capacity for this queue

        private int front; //index of front of queue

        private int back; // index of back of queue

        private int length;

        //constructor

        public ArrayQueue(){

               data=new int[capacity];

               front=0;

               back=0;

               length=0;

        }

       

        //add an item to the queue

        public void add(int value){

               //add to the back

               if(back<capacity){

                       data[back]=value;

                       back++;

                       length++;

               }else{

                       System.out.println(\"reached maximum capacity\");

               }

              

        }

       

       

        public boolean isEmpty(){

               return (length==0);

        }

       

        //print the values of the queue

        public void print(){

               for (int i=front; i<back;i++){

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

               }

               System.out.println();

        }

}

Solution

package arrayqueue;

/**
*
* @author alok
*/
class ArrayQueue {

private final int data[];
private final int capacity = 100; //maximum capacity for this queue
private final int front; //index of front of queue
private int back; // index of back of queue
private static int length;

//constructor
public ArrayQueue() {
data = new int[capacity];
front = 0;
back = 0;
length = 0;
}

//add an item to the queue
public void add(int value) {
//add to the back
if (back < capacity) {
data[back] = value;
back++;
length++;
} else {
System.out.println(\"reached maximum capacity\");
}

}

public boolean isEmpty() {
return (length == 0);
}

//print the values of the queue
public void print() {
for (int i = front; i < back; i++) {
System.out.print(data[i] + \"\\t\");
}
System.out.println();
}

public int poll() {
if (!isEmpty()) {
//System.out.println(\"Length Is :\"+ length);
int i = data[length - 1];
data[length - 1] = 0;
length--;
back--;
return i;
}
return -1;
}

public int peek() {
if (!isEmpty()) {
int i = data[length - 1];
return i;
}
return -1;
}

}

class ArrayStack {

private final int data[];
private final int capacity = 100; //maximum capacity for this queue
private int top;

public ArrayStack() {
data = new int[capacity];
top = -1;
}

public void push(int value) {
if (!(isFull())) {
data[++top] = value;
}

}

public void pop() {
if (!(isEmpty())) {
System.out.println(\"The Peak value Is:\" + data[top]);//check the peak value here
data[top--] = 0;
} else {
System.out.println(\"Stack Is Empty you can\'t perform pop operation into Stack!!!\");
}
}

public int peek() {
return data[top];
}

public boolean isEmpty() {
return (top == -1);
}

public boolean isFull() {
return (top == capacity - 1);
}

}

public class Queue {

public static void main(String[] args) {
int i;
System.out.println(\"Operation Started for Queue:\");
arrayqueue.ArrayQueue aq = new arrayqueue.ArrayQueue();
System.out.println(\"\ *****************************\");
System.out.println(\"\ Check the Queue For Emptyness : Queue is Empty..?\\t\" + aq.isEmpty());
aq.add(1);
aq.add(2);
aq.add(3);
aq.add(100);
aq.add(200);
aq.add(300);
System.out.println(\"\ The Queue After Adding the Value:\");
aq.print();
i = aq.poll();
System.out.println(\"\ Peek value Which is deleted Is:\\t\" + i);
System.out.println(\"\ The Queue After The Top Element is Deleted\ \");
aq.print();
i = aq.peek();
System.out.println(\"\ Peek value of The Queue is:\\t\" + i);
System.out.println(\"\ operation Are Completed For Queue \ \");
System.out.println(\"*****************************\ \");
System.out.println(\"*****************************\ \");
System.out.println(\"Operation Started for Stack :\");
arrayqueue.ArrayStack as = new arrayqueue.ArrayStack();
System.out.println(\"\ *****************************\");
/*below function code is only for checking the pop operation is working properly or not
you can remove this line code as per your requirement*/
as.pop();
System.out.println(\"\ Check the Stack For Emptyness : Stack is Empty..?\\t\" + as.isEmpty());
as.push(1);
as.push(2);
as.push(3);
as.push(20);
as.push(50);
as.push(100);
i = as.peek();
System.out.println(\"\ Peek value of The Stack is:\\t\" + i);
as.pop();
i = as.peek();
System.out.println(\"\ Peek value of The Stack is:\\t\" + i);
System.out.println(\"\ operation Are Completed For Stack \ \");

}
}

For the following code: Add a poll() method that returns the integer value of the front element in the queue and removes its from the queue. Add a peek() method
For the following code: Add a poll() method that returns the integer value of the front element in the queue and removes its from the queue. Add a peek() method
For the following code: Add a poll() method that returns the integer value of the front element in the queue and removes its from the queue. Add a peek() method
For the following code: Add a poll() method that returns the integer value of the front element in the queue and removes its from the queue. Add a peek() method

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site