Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I've run into a bit of a snag in my code. I've written the class as far as my understanding goes, but I'm really not

I've run into a bit of a snag in my code. I've written the class as far as my understanding goes, but I'm really not quite sure how to proceed. Any suggestions or examples of how some of these methods should be written would be much appreciated! Below are the assignment instructions and the provided ListNode Class, followed by the code I have so far.

image text in transcribed

LISTNODE

public class ListNode { public E data; public ListNode next; }

CODE

public class Deque20 { private ListNode front; // Reference to the front of the list private ListNode back; // Reference to the back of the list private int numElems; // Tracks number of elements in deque private LinkedList list;

public Deque20() { front = list; back = null; numElems = 0; } public void push(E x) // Adds x to the front of the deque { front.data = x; numElems++; } public E pop() // Removes and returns the front of the deque { return remove(); } public void add(E x) // Adds x to the back of the deque { back.data = x; numElems++; } public E remove() // Removes and returns the front of the deque { if (numElems == 0) { throw new IllegalArgumentException(); }else if (numElems == 1){ E temp = front.data; front.data = null; back.data = null; numElems--; return temp; } } public E peek() // Returns the front of the deque without removing it { if (numElems == 0) { throw new IllegalArgumentException(); }else{ return front.data; } } public boolean isEmpty() // Returns true if the deque has no elements { if (numElems == 0) { return true; }else{ return false; } } public int size() // Returns the number of elements in the deque { return numElems; } }

Module 3 Program - Deque Read this entire document carefully. You only get credit if you do everything correctly Background A very useful type of Collection interface is a deque (pronounced "deck"). Java has classes implementing this interface built in. For example Degue dl LinkedList(); - new A deque is simply a list of objects, but access is restricted to adding and removing only from the list's front and back, and these operations are designed to be fast. Later in the semester, we'll see many applications of such lists when we study stacks and queues. I'm having you write a deque now to give you some insight into how Java collection classes work behind the scenes Specification Write a class named Deque20placed in the file Dque20.java . Your deque should support the following public methods public void push(Ex) public E pop () public void add (E ) public E romove public peek ) public boolean isEmpty() public int size Adds x to the front of the deque // Remove nd return3 the front of the deque // Adds x to the back of the deque //Removes and returns the front of the deque // Returna the front of the deque without removing ir // Returna true if the deque ha no element g // Retur1 the number of eleme :t in the degue If the client attempts to pop, remove or peek an empty list, your method should throw a NoSuchElementException .Some collection classes do not alloW null to be added or pushed, but your Deque20 should allow this (ie, d.pshould return null after d.push (aull). You may find it useful to write a toString method to help with your debugging, this is allowed, but is not a required element of your class. None of the methods should use a loop to do its work. This means that you will need to keep two references as fields, one to the front of the list and one to the back, and that you'll need to keep the count of the number of items as a field too. You don't need any other fields besides these three Use the starter code at the end of this writeup. It includes E> as part of its specification. This is how you make a class generic, allowing it to be a deque of any reference type. It's easy to write such classes. Simply put E anywhere you need a type in your code. Conceptually all of those E's are replaced by the user's supplied type Do not modify the ListNode class and do not submit it. I will use this exact file when testing your code Module 3 Program - Deque Read this entire document carefully. You only get credit if you do everything correctly Background A very useful type of Collection interface is a deque (pronounced "deck"). Java has classes implementing this interface built in. For example Degue dl LinkedList(); - new A deque is simply a list of objects, but access is restricted to adding and removing only from the list's front and back, and these operations are designed to be fast. Later in the semester, we'll see many applications of such lists when we study stacks and queues. I'm having you write a deque now to give you some insight into how Java collection classes work behind the scenes Specification Write a class named Deque20placed in the file Dque20.java . Your deque should support the following public methods public void push(Ex) public E pop () public void add (E ) public E romove public peek ) public boolean isEmpty() public int size Adds x to the front of the deque // Remove nd return3 the front of the deque // Adds x to the back of the deque //Removes and returns the front of the deque // Returna the front of the deque without removing ir // Returna true if the deque ha no element g // Retur1 the number of eleme :t in the degue If the client attempts to pop, remove or peek an empty list, your method should throw a NoSuchElementException .Some collection classes do not alloW null to be added or pushed, but your Deque20 should allow this (ie, d.pshould return null after d.push (aull). You may find it useful to write a toString method to help with your debugging, this is allowed, but is not a required element of your class. None of the methods should use a loop to do its work. This means that you will need to keep two references as fields, one to the front of the list and one to the back, and that you'll need to keep the count of the number of items as a field too. You don't need any other fields besides these three Use the starter code at the end of this writeup. It includes E> as part of its specification. This is how you make a class generic, allowing it to be a deque of any reference type. It's easy to write such classes. Simply put E anywhere you need a type in your code. Conceptually all of those E's are replaced by the user's supplied type Do not modify the ListNode class and do not submit it. I will use this exact file when testing your code

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_2

Step: 3

blur-text-image_3

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

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago