Question: 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
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).
Here's the program 3 list:
public class List { // put all fields from ListAsLinkedList class here //references to head and tail nodes of this list private Node head, tail; //count of current number of elements in this list private int count; //constructor initializing empty list public ListAsLinkedList() { head=null; tail=null; count=0; } @Override public void append(char value) { //new node with given value Node newNode=new Node(value); //if head is null, adding as both head and tail if(head==null){ head=newNode; tail=newNode; } else{ //appending to tail and updating tail tail.next=newNode; tail=newNode; } //updating count count++; } @Override public void prepend(char value) { //creating a new node with given value Node newNode=new Node(value); //if head is null, adding as both head and tail if(head==null){ head=newNode; tail=newNode; }else{ newNode.next=head; head=newNode; } //updating count count++; } @Override public void deleteAt(int position) { if(position>=0 && position NOW HERE IS THE STACK.JAVA 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() { } } AND FINALLY HERE IS THE QUEUE.JAVA 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() { } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
