Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP DUE IN 30 MINUTES PLEASE Make sure everything is working correctly before moving on to this step. After things are working, you need to

HELP DUE IN 30 MINUTES PLEASE

Make sure everything is working correctly before moving on to this step. After things are working, you need to turn the Stack and Queue classes into generic classes. This requires several changes including a change to the List and Node classes to turn them into generic classes as well. Refer to the notes for help. After making these changes, you must modify the Main class to use your generic Stack and Queue classes. This means giving both Stack and Queue a value for the generic type parameter both at declaration and at instantiation. As an example, if you had the following code:

List list; // declaration

list = new List(); // instantiation

you would change it into:

List list; // declaration

list = new List (); // instantiation

where "Datatype" is the data type you want to use, either String, Integer, Character, or something else. You only need to specify the datatype during declaration and instantiation, not every time you use the object.

Stack Template:

public class Stack { /** List objects to hold our stack items. Use List operations to implement the methods below */ private List list; public Stack() { // instantiate list here list = newList(); } public void push(char value) { // push an item onto the stack list.prepend(value); } public char pop() { // pop an item off the stack char popVal = list.getValueAt(0); list.deleyeAt(0); return popVal; } public char peek() { // peek at the item on the top off the stack return list.getValueAt(0); } public boolean isEmpty() { // returns true if the stack is empty return list.size() == 0; } }

Queue Template:

/** Queue abstract data type */ public class Queue { /** List objects to hold our queue items. Use List operations to implement the methods below */ private List list; public Queue() { // instantiate list here list = newList(); } public void enqueue(char value) { // add an item to the back of the queue list.append(value); } public char dequeue() { // remove and return the item from the front of the queue char deqVal = list.getValueAt(0); list.delteAt(0); return deqVal; } public char front() { // return the iteam at the front of the queue return list.getValueAt(0); } public boolean isEmpty() { // return true if the queue is empty return list.size() == 0; } }

List Template:

/** Linked List implementation of our List abstract data type */ public class List { // put all fields from ListAsLinkedList class here class List implements IList { private Node head; private int size; ListAsLinkedList() { } public void append(char var1) { if (this.head == null) { this.head = new Node(var1); ++this.size; } else { Node var2; for(var2 = this.head; var2.next != null; var2 = var2.next) { } var2.next = new Node(var1); ++this.size; } } public void prepend(char var1) { Node var2 = new Node(var1); var2.next = this.head; this.head = var2; ++this.size; } public void deleteAt(int var1) { if (var1 >= 0 && var1 < this.size) { if (var1 == 0) { this.head = this.head.next; --this.size; } else { Node var2 = this.head; for(int var3 = 0; var3 < var1 - 1; ++var3) { var2 = var2.next; } var2.next = var2.next.next; --this.size; } } } public int size() { return this.size; } public char getValueAt(int var1) { if (var1 >= 0 && var1 < this.size) { Node var2 = this.head; for(int var3 = 0; var3 < var1; ++var3) { var2 = var2.next; } return var2.value; } else { return '\u0000'; } } public int positionOf(char var1) { Node var2 = this.head; for(int var3 = 0; var3 < this.size; ++var3) { if (var2.value == var1) { return var3; } var2 = var2.next; } return -1; } } // put all methods from ListAsLinkedList class here } /** A linked list node for our linked list */ class Node { // put all fields from Node class here class Node { char value; Node next; public Node(char var1) { this.value = var1; } } // put all methods from Node class here }

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

More Books

Students also viewed these Databases questions

Question

Explain the meaning of ergonomics.

Answered: 1 week ago

Question

Compare the current team to the ideal team.

Answered: 1 week ago

Question

a. Do team members trust each other?

Answered: 1 week ago