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

Queue and stacks 1) 2) ALSO Please explain with the aid of memory diagram!!!!! A

ID: 3855157 • Letter: Q

Question

Queue and stacks

1)

2)

ALSO Please explain with the aid of memory diagram!!!!!

A queue of integers is implemented using a circular array public class Int@ueue i private int[i data; private int count private int front private int rear; a. Give the output from the following code IntQueue g = new IntQueue(); q.enqueue (5) q.enqueue (15) q.enqueue (110); int val -q.dequeue ) val = q. dequeue ( ) ; q.enqueue (17) q.enqueue (19) enqueue(21); val = g. dequeue ( ) ; encrudeue while Iaemptvueue)) System.outpzint (adegueueO): Output: b. Write the code for the dequeue method using the variables above

Explanation / Answer

package queue;

/******* Solution for question 1 ***********/

public class IntQueue {

// let max size of the queue is 10

private final static int SIZE = 10;

private int[] data;

private int count;

private int front;

private int rear;

// constructor

public IntQueue() {

data = new int[SIZE];

front = -1;

rear = -1;

count = 0;

}

public void enQueue(int value) {

if ((front == 0 && rear == SIZE - 1) || (rear == front - 1)) {

System.out.println("Queue is full");

return;

}

else if (front == -1) {

// inserting first element

front = 0;

rear = 0;

data[rear] = value;

}

else if ((rear == SIZE - 1) && (front != 0)) {

rear = 0;

data[rear] = value;

}

else {

rear++;

data[rear] = value;

}

// increment the count

count++;

}

public int deQueue() {

if (front == -1) {

System.out.println("Queue is Empty");

return Integer.MIN_VALUE;

}

// else the queue is not empty so decrement count

count--;

// get the value from front because dequeue is happening from front

int val = data[front];

// set the data in front to -1

data[front] = -1;

// now if front and rear are same that mean now the queue is empty so

// start over

if (front == rear) {

front = -1;

rear = -1;

}

// if front is the last index set it to zero

else if (front == SIZE - 1) {

front = 0;

}

// else simply increment the front

else {

front++;

}

// and return the value

return val;

}

public void display() {

if (front == -1) {

System.out.println("empty case");

return;

}

if (rear >= front) {

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

System.out.printf("%d ", data[i]);

}

} else {

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

System.out.printf("%d ", data[i]);

}

for (int i = 0; i <= rear; i++) {

System.out.printf("%d ", data[i]);

}

}

}

// now testing above function

public static void main(String args[]) {

IntQueue q = new IntQueue();

q.enQueue(5);

q.enQueue(15);

q.enQueue(110);

int val = q.deQueue();

val = q.deQueue();

q.enQueue(17);

q.enQueue(19);

q.enQueue(21);

val = q.deQueue();

q.enQueue(23);

q.display();

}

}

/******************** 1 (a) output ***********************************/

17 19 21 23

/**** Solution for question 2 ******/

public class IntegerStack {

private class IntNode {

public int info;

public IntNode next;

IntNode(int num, IntNode ptr) {

info = num;

next = ptr;

}

}

private IntNode top;

private int count;

public IntegerStack() {

top = null;

count = 0;

}

public boolean isEmpty() {

return count == 0;

}

public void push(int value) {

IntNode newNode = new IntNode(value, null);

count++;

newNode.next = top;

top = newNode;

}

public int pop() {

if (isEmpty()) {

System.out.println("stack is empty");

return Integer.MIN_VALUE;

}

count--;

IntNode temp = top;

top = top.next;

int val = temp.info;

return val;

}

public void display() {

while (!isEmpty()) {

System.out.printf("%d ", pop());

}

}

// testing time

public static void main(String args[]) {

IntegerStack s = new IntegerStack();

s.push(6);

s.push(4);

s.push(3);

s.push(1);

int val = s.pop();

s.push(2);

val = s.pop();

val = s.pop();

s.push(7);

s.push(8);

val = s.pop();

s.push(3);

s.display();

}

}

/******************* output for 2 (a) ***********************/

3 7 4 6

// please read the code to understand how the code is working it is simple