Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a question here in java The subject is a queue I have two Node, Queue classes here Class Queue: public class Queue {

I have a question here in java The subject is a queue

I have two Node, Queue classes here

Class Queue:

public class Queue

{

private Node _head, _tail;

public Queue()

{

_head = null;

_tail = null;

}

public boolean isEmpty()

{

return _head == null;

}

public void enqueue (int num)

{

Node temp = new Node (num, null);

if (_tail!=null)

_tail.setNext(temp);

_tail = temp;

if (_head == null)

_head = temp;

}

public int dequeue()

{

int temp = _head.getValue();

_head = _head.getNext();

return temp;

}

public int first()

{

return _head.getValue();

}

public String toString()

{

String s = "";

Node temp = _head;

while (temp != null)

{

s = s + temp.getValue() + "\t";

temp = temp.getNext();

}

return s;

}

public static int what (Queue q, int x)

{

if (q.isEmpty() || x!= (q.getHead()).getValue())

return 1;

x = q.dequeue();

return 1+ what (q, x);

}

public static int what1(Queue q)

{

int y;

if (q.isEmpty())

y = 0;

else

{

int x = q.dequeue();

int z = x * what (q, x);

y = 1 + what1(q);

q.enqueue(z);

}

return y;

}

}

Class Node

public class Node {

private int _value;

private Node _next;

public Node(int val, Node n) {

_value = val;

_next = n;

}

public Node(int val) {

_value = val;

_next = null;

}

public Node getNext() {

return _next;

}

public void setNext(Node node) {

_next = node;

}

public int getValue() {

return _value;

}

public void setValue(int val) {

_value = val;

}

public String toString() {

return ("" + _value);

}

}

There are two parts to this question and I have to choose the right answer

The Queue class additionally has two static classes And I have this turn: 2 3 1 1 2 2 2 4 4 4 5 (Number 2 - the top of the line Number 5 - Queue Tail)

part 1:

One correct answer must be chosen

The value returned from the what1 method when enabled on the q queue is:

a. 11 b. 5 c. 6 d. There is no correct answer a-c

part 2:

One correct answer must be chosen

After activating the what1 method on the q queue above, the queue will look like this (from left to right, the queue head on the left and the queue tail on the right)

a. 2 3 1 1 2 2 2 4 4 4 5 b. 5 4 4 4 2 2 2 1 1 3 2 c. 2 3 2 6 12 5 d. 5 12 6 2 3 2 e. There is no correct answer a-d

Please attach the tester and Output to me

Thank you!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

0764549634, 9780764549632

More Books

Students also viewed these Databases questions