Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The implementation is in Java about a doublylinkedlist. Starter code is given. THANK YOU STARTERCODE: import java.util.AbstractList; /** * TODO * @author TODO * @since

The implementation is in Java about a doublylinkedlist. Starter code is given. THANK YOU

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedSTARTERCODE:

import java.util.AbstractList; /** * TODO * @author TODO * @since TODO */ public class DoublyLinkedList extends AbstractList { /* DLL instance variables */ private int nelems; private Node head; private Node tail; /** * Node for chaining together to create a linked list */ protected class Node { /* Node instance variables */ T data; Node next; Node prev; /** * Constructor to create singleton Node */ private Node(T element) { // TODO: complete constructor } /** * Constructor to create singleton link it between previous and next * * @param element Element to add, can be null * @param nextNode successor Node, can be null * @param prevNode predecessor Node, can be null */ private Node(T element, Node nextNode, Node prevNode) { // TODO: complete implementation } /** * Set the element * * @param element new element */ public void setElement(T element) { // TODO: complete implementation } /** * Accessor to get the Nodes Element */ public T getElement() { // TODO: complete implementation return null; } /** * Set the next node in the list * * @param n new next node */ public void setNext(Node n) { // TODO: complete implementation } /** * Get the next node in the list * * @return the successor node */ public Node getNext() { // TODO: complete implementation return null; } /** * Set the previous node in the list * * @param p new previous node */ public void setPrev(Node p) { // TODO: complete implementation } /** * Accessor to get the prev Node in the list * * @return predecessor node */ public Node getPrev() { // TODO: complete implementation return null; } /** * Remove this node from the list. * Update previous and next nodes */ public void remove() { // TODO: complete implementation } } /** * Creates a new, empty doubly-linked list. */ public DoublyLinkedList() { // TODO: complete default constructor } /** * Add an element to the end of the list * * @param element data to be added * @return whether or not the element was added * @throws NullPointerException if data received is null */ @Override public boolean add(T element) throws NullPointerException { // TODO: Implementation for throwing exceptions followed by // implementation of adding the new data return true; } /** * Adds an element to a certain index in the list, shifting exist elements * create room. Does not accept null values. * * TODO: Javadoc comments */ @Override public void add(int index, T element) throws IndexOutOfBoundsException, NullPointerException { // TODO: Implementation for throwing exceptions followed by // implementation of adding the new data } /** * Clear the linked list */ @Override public void clear() { // TODO: implement clear } /** * Determine if the list contains the data element anywhere in the list. * * TODO: Javadoc comments */ @Override public boolean contains(Object element) { T data = (T)element; // TODO: Fill in implementation return false; } /** * Retrieves the element stored with a given index on the list. * * TODO: Javadoc comments */ @Override public T get(int index) throws IndexOutOfBoundsException { // TODO: Fill in implementation to get the node at index return null; } /** * Helper method to get the Nth node in our list * * TODO: Javadoc comments */ private Node getNth(int index) { // TODO: implement return null; } /** * Determine if the list empty * * TODO: javadoc comments */ @Override public boolean isEmpty() { // TODO: implement isEmpty return true; } /** * Remove the element from position index in the list * * TODO: javadoc comments */ @Override public T remove(int index) throws IndexOutOfBoundsException { // TODO: Fill in implementation return null; } /** * Set the value of an element at a certain index in the list. * * TODO: javadoc comments */ @Override public T set(int index, T element) throws IndexOutOfBoundsException, NullPointerException { // TODO: Fill in implmentation return null; } /** * Retrieves the amount of elements that are currently on the list. * * TODO: javadoc comments */ @Override public int size() { // TODO: complete implementation return 0; } /** * String representation of this list in the form of: * "[(head) -> elem1 -> elem2 -> ... -> elemN -> (tail)]" * * TODO: javadoc comments */ @Override public String toString() { return null; } /* ==================== EXTRA CREDIT ==================== */ /** * Remove nodes whose index is a multiple of base * * TODO: javadoc comments */ public void removeMultipleOf(int base) { // TODO: complete implementation } /** * Swap the nodes between index [0, splitIndex] of two lists * * TODO: javadoc comments */ public void swapSegment(DoublyLinkedList other, int splitIndex) { // TODO: complete implementation } }
Part 2 - Implementing DoublyLinkedList In this part of the PA, you will be implementing your own version of LinkedList in a class called DoublyLinkedlist in the file DoublyLinkedList.java. You will be making two classes in this file. In a typical implementation, it will contain an outer class called DoublyLinkedlist. However, inside of this will be an inner class called Node (examples of inner and outer classes). You will use the Node class in building your linked list. Each node object will have 3 fields: . . elem - this contains the information stored in this position next - a reference to the next node prev - a reference to the previous node . You will chain node objects together using next and prev to create a list. prev Reference to an element next head tail 0 ? 4 ? 0 Now that we have a chain of nodes, we need some way of accessing them. Your DoublyLinkedList class with have three fields to handle this: 0 head - a node with no value whose next field is the start of the list tail - a node with no value whose prev field is the end of the list size - the number of nodes in the list . For your implementation, you must use a dummy head and tail, as this greatly simplifies the implementation of various methods. In the Node class you will implement the following methods: private Node (T element) Creates a singleton node. Creates a node that is linked to other nodes. private Node (T element, Node nextNode, Node prevNode) Alters the data stored at this node. public void setElement(T element) public T getElement() Returns the data stored at this node. public void setNext (Node n) Set the successor node. public Node getNext() Access the successor node. public void setPrev (Node p) Set the predecessor node. public Node getPrev () Access the predecessor node. public void remove() Unlinks the node from its list. In DoublyLinkedList class you will need to implement the following methods: public DoublyLinkedList() Creates an empty linked-list with a dummy head and tail. public boolean add (T element) public boolean add (T element) Appends a node containing the specified data to the end of the list. Returns true if the item is added and false otherwise. @throws NullPointerException - if data received is null public void add (int index, T element) Inserts a node containing the specified data at the desired location. @throws IndexOutOfBoundsException - index is outside the range [0, size] @throws NullPointerException - if data received is null public void clear() Removes all elements from the list public boolean contains (Object element) Returns true if the list contains the specified element at least once. Note: This method overrides an older Java method that did not use generics. Before generics, programmers had to use type casting to ensure that parameters passed in were of the correct type. Since we cannot change the method signature when overriding a method we must pass in the parameter as an Object and then cast it to type T (this code is given in the starter code). public T get(int index) public T get(int index) Access the data contained in the node at the specified index. @throws IndexOutOfBoundsException - index is outside the range [0, size - 1] private Node getNth (int index) Helper method for accessing the node in the specified index of the list. Note: you should use this method as little as possible considering its O(n) run time. If you need to get access to a Node at a certain index multiple times in the same method, please call getNth() only once and store it into a local variable. public boolean isEmpty() Return true if the list contains no elements, false otherwise. public T remove (int index) public T remove (int index) Remove the element at the specified index from the list and return the data it contained @throws IndexOutOfBoundsException - index is outside the range [0, size - 1] public T set (int index, T element) Alters the data stored in the node at the specified index and returns the data previously at the specified position. @throws IndexOutOfBoundsException - index is outside the range [0, size - 1] @throws NullPointerException - if data received is null public int size () Return the number of elements stored in the list. public String toString() Return a String representation of the list. In the output string, elements are represented using their string representations. Examples: Elements in list: [ ] Output: [(head) -> (tail)) Elements in list: [3, 4, 5] Output: [(head) -> 3 -> 4 -> 5 -> (tail))" - ->

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

50 Tips And Tricks For MongoDB Developers Get The Most Out Of Your Database

Authors: Kristina Chodorow

1st Edition

1449304613, 978-1449304614

More Books

Students also viewed these Databases questions

Question

8. Explain the contact hypothesis.

Answered: 1 week ago

Question

7. Identify four antecedents that influence intercultural contact.

Answered: 1 week ago