Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your objective is to implement a stack and a queue using the list implementation you did in program #3. You must use the linked list

Your objective is to implement a stack and a queue using the list implementation you did in program #3. You must use the linked list implementation of a list. You are to use your list operations to accomplish the operations of a stack and queue.

Getting started:

Grab your ListAsLinkedList class and your Node class from program #3

In the List.java file given to you, where it says "public class List", put your ListAsLinkedList class's fields and methods. To be clear, this creates a new class called List (that does NOT implement IList) that is the exact same as your ListAsLinkedList class. DON'T FORGET to rename the constructor to "List".

Where it says "class Node", put your Node class's fields and methods.

You will not need the IList interface nor the ListAsArray class, so do NOT include them.

The Stack.java and Queue.java files given to you are where you will implement your stack and queue. They have been "stubbed out" meaning method "stubs", or blank methods, have been created for you. These need to be given an implementation that will carry out the operations

To be clear, you are NOT recreating a linked list in your Stack and Queue classes. I should NOT see any reference to the Node datatype nor the head field in your Stack and Queue files. Instead, use the given list field on line 5 and perform List operations (i.e. append, prepend, deleteAt, size, getValueAt, and positionOf) on that list field. Use 1 or more of these operations on your list field to mimic the operations of a Stack (push, pop, peek, and isEmpty) and Queue(enqueue, dequeue, front, and isEmpty). Not following these directions will result in failing this assignment as it defeats the purpose of teaching you how to use a fully implemented abstract data type to create another abstract data type (i.e. using a fully implemented List to create a Stack and Queue). Add generics

Queue template:

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 } public void enqueue(char value) { } public char dequeue() { } public char front() { } public boolean isEmpty() { } } 

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 } public void push(char value) { } public char pop() { } public char peek() { } public boolean isEmpty() { } } 

Necessary Code Needed from Program 3:

LinkedList Code:

public class List { // put all fields from ListAsLinkedList class here class ListAsLinkedList 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; } } 
/** 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; } } 

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

Advanced Oracle Solaris 11 System Administration

Authors: Bill Calkins

1st Edition

0133007170, 9780133007176

More Books

Students also viewed these Databases questions

Question

What do you see as your responsibilities?

Answered: 1 week ago