Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

MyQueue - Create your own MyQueue class, be sure to use generic data types like we did with MyStack, store first/last node, make sure all

MyQueue - Create your own MyQueue class, be sure to use generic data types like we did with MyStack, store first/last node, make sure all methods execute in constant time, create a test class to show the Queue works correctly

That below is MyStack.java as an example Please keep the format like that

public class MyStack {

private MyNode start;

private int size;

public MyStack()

{

start = null;

size = 0;

}

public void push(E obj)

{

insert(obj, 0);//constant runtime

}

public E pop()

{

return delete(0);//constant runtime

}

public E peek()

{

return get(0);//constant runtime

}

public boolean isEmpty()

{

return size == 0;

}

private String printList()

{

MyNode current = start;

String output = "[";

while(current != null)

{

output += current.toString() + ", ";

current = current.next;

}

output += "]";

return output;

}

private void insert(E obj, int index)

{

MyNode temp = new MyNode(obj);

if(index <= 0 || size == 0)//insert at start

{

if(start == null)

start = temp;

else

{

temp.next = start;

start = temp;

}

}

else

{

if(index > size)

index = size;

MyNode current = start;

for(int i = 1; i < index; i++)

{

current = current.next;

}

temp.next = current.next;//rest of list comes after new value

current.next = temp;//new value is after current

}

size++;

}

private void add(E obj)

{

insert(obj, size);

}

/*

* Test edge cases:

* 0 -> 1 -> 2 -> 3

* delete 1 (middle)

* 0 -> 2 -> 3

* delete 0 (begin)

* 2 -> 3

* delete 3 (end)

* 2

*/

private E remove(E obj)

{

if(start == null)//prevent runtime error on empty list

return null;

if(obj.equals(start.data))//first item needs to be deleted

{

E temp = start.data;

start = start.next;

size--;

return temp;

}

else

{

MyNode current = start;

while(current.next != null && !current.next.data.equals(obj))

{

current = current.next;

}

if(current.next != null)

{

E temp = current.next.data;

current.next = current.next.next;

size--;

return temp;

}

else//end of list, did not find value to delete

return null;

}

}

private E delete(int index)

{

if(index < 0 || index >= size)

return null;

else

{

if(index == 0)

{

E temp = start.data;

start = start.next;

size--;

return temp;

}

else

{

MyNode current = start;

for(int i = 1; i < index; i++)

current = current.next;

E temp = current.next.data;

current.next = current.next.next;

size--;

return temp;

}

}

}

private E find(E obj)

{

if(start == null)//prevent runtime error on empty list

return null;

if(obj.equals(start.data))//first item needs to be deleted

{

return start.data;

}

else

{

MyNode current = start;

while(current.next != null && !current.next.data.equals(obj))

{

current = current.next;

}

if(current.next != null)

{

return current.next.data;

}

else//end of list, did not find value to delete

return null;

}

}

private E get(int index)

{

if(index < 0 || index >= size)

return null;

else

{

if(index == 0)

{

return start.data;

}

else

{

MyNode current = start;

for(int i = 1; i < index; i++)

current = current.next;

return current.next.data;

}

}

}

private class MyNode

{

MyNode next = null;

E data = null;

public MyNode(E obj)

{

next = null;

data = obj;

}

public String toString()

{

return data.toString();

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

MyNode other = (MyNode) obj;

return data.equals(other.data);

}

}

}

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 Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

Does an Agency Fund have a fund balance? Why or why not?

Answered: 1 week ago

Question

Compute an expression for P max t1st2 B(s) > x

Answered: 1 week ago