Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to implement a linked list using java language the node class package cp213; public final class Node { // The T data. private

I want to implement a linked list using java language

the node class

package cp213; public final class Node { // The T data. private T data = null; // Link to the next Node. private Node next = null; /** * Creates a new node with data and link to next node. Not copy safe as it * accepts a reference to the data rather than a copy of the data. * * @param data * the data to store in the node. * @param next * the next node to link to. */ public Node(final T data, final Node next) { this.data = data; this.next = next; } /** * Returns the node data. Not copy safe as it returns a reference to the * data, not a copy of the data. * * @return The data portion of the node. */ public final T getData() { return this.data; } /** * Returns the next node in the linked structure. * * @return The node that follows this node. */ public final Node getNext() { return this.next; } /** * Links this node to the next node. * * @param next * The new node to link to. */ public final void setNext(final Node next) { this.next = next; } } 

the SingleLinked class

package cp213; import java.lang.reflect.Array; import java.util.Iterator; import java.util.NoSuchElementException; /** * The abstract base class for singly-linked data structures. Provides * attributes and implementations for getSize, peek, isEmpty, toArray, and * iterator methods. The head attribute is the first node in any * singly-linked list. * @param  * data type for base data structure. */ public abstract class SingleLink implements Iterable { /** * Creates an Iterator for the outer class. An iterator allows a program to * walk through the data in a data structure by using the hasNext and next * methods. Typical code: * * 
 Iterator iter = dataStructureObject.iterator(); while(iter.hasNext()){ T data = iter.next(); ... } * 
* * It also allows the user of the enhanced for loop: * *
 for(T data : dataStructureObject){ ... } * 
* * (Replace T with a concrete class such as String or Integer.) */ private class SingleLinkIterator implements Iterator { // current is initialized to beginning of linked list. private Node current = SingleLink.this.head; /* * (non-Javadoc) * * @see java.util.Iterator#hasNext() */ @Override public boolean hasNext() { return this.current != null; } /* * (non-Javadoc) * * @see java.util.Iterator#next() */ @Override public T next() { T result = null; if (this.current == null) { throw new NoSuchElementException(); } else { result = this.current.getData(); this.current = this.current.getNext(); } return result; } } // First node of linked list. protected Node head = null; // Number of elements currently stored in linked list. protected int size = 0; /** * Returns the current number of elements in the linked structure. * * @return the value of size. */ public final int getSize() { return this.size; } /** * Determines whether the linked data structure is empty or not. * * @return true if data structure is empty, false otherwise. */ public final boolean isEmpty() { return this.head == null; } /* * (non-Javadoc) * * @see java.lang.Iterable#iterator() */ @Override public final Iterator iterator() { return new SingleLinkIterator(); } /** * Returns a reference to the first data of the linked structure without * removing that data from the structure. * * @return The data in the head of the structure. */ public final T peek() { return this.head.getData(); } /** * Returns an array of data from a linked data structure. Not thread safe as * it assumes contents of data structure are not changed by an external * thread during the copy loop. If data elements are added or removed by an * external thread while the data is being copied to the array, then the * declared array size may no longer be valid. * * @return an array of data of type T. Returns null if the data structure is * empty. */ @SuppressWarnings("unchecked") public final T[] toArray() { T[] a = null; if (this.head != null) { // Create an array of data based upon the class of the head data. a = (T[]) Array.newInstance(this.head.getData().getClass(), this.size); final Iterator iter = this.iterator(); for (int i = 0; i < this.size; i++) { a[i] = iter.next(); } } return a; } }

And here is the linked list class

package cp213; /** * A simple linked list structure of Node T objects. Only the * T data contained in the stack is visible through the standard * stack methods. Extends the SingleLink class, which already * defines the head node, size, iterator, and toArray. * * @param  * this List data type. */ public class List> extends SingleLink { /** * Appends data to the end of this List. * * @param data * The data to append. */ public void append(final T data) { // your code here } /** * Removes duplicates from this List. The list contains one and only one of * each value formerly present in this List. The first occurrence of each * value is preserved. */ public void clean() { // your code here } /** * Combines contents of two lists into a third. Values are alternated from * the origin lists into this List. The origin lists are empty when * finished. NOTE: data must not be moved, only nodes. * * @param left * The first list to combine with this List. * @param right * The second list to combine with this List. */ public void combine(final List left, final List right) { // your code here } /** * Determines if this List contains key. * * @param key * The key value to look for. * @return true if key is in this List, false otherwise. */ public boolean contains(final T key) { // your code here } /** * Finds the number of times key appears in list. * * @param key * The value to look for. * @return The number of times key appears in this List. */ public int count(final T key) { // your code here } /** * Finds and returns the value in list that matches key. * * @param key * The value to search for. * @return The value that matches key, null otherwise. */ public T find(final T key) { // your code here } /** * Get the nth item in this List. * * @param n * The index of the item to return. * @return The nth item in this List. * @throws ArrayIndexOutOfBoundsException * if n is not a valid index. */ public T get(final int n) throws ArrayIndexOutOfBoundsException { // your code here } /** * Determines whether two lists are identical. * * @param that * The list to compare against this List. * @return true if this List contains the same values in the same order as * that, false otherwise. */ public boolean identical(final List that) { // your code here } /** * Finds the location of a value by key in list. * * @param key * The value to search for. * @return The index of key in this List, -1 otherwise. */ public int index(final T key) { // your code here } /** * Inserts data into this List at index i. * * @param i * The index to insert the new value at. * @param data * The new value to insert into this List. */ public void insert(int i, final T data) { // your code here } /** * Inserts data into the front of this List. * * @param data * The value to insert into the front of this List. */ public void insertFront(final T data) { // your code here } /** * Creates an intersection of two other Lists into this List. Copies data to * this List. left and right Lists are unchanged. * * @param left * The first List to create an intersection from. * @param right * The second List to create an intersection from. */ public void intersection(final List left, final List right) { // your code here } /** * Finds the maximum value in this list. * * @return The maximum value. */ public T max() { // your code here } /** * Finds the minimum value in this list. * * @return The minimum value. */ public T min() { // your code here } /** * Finds, removes, and returns the value in this List that matches key. * * @param key * The value to search for. * @return The value matching key, null otherwise. */ public T remove(final T key) { // your code here } /** * Removes the value at the front of this List. * * @return The value at the front of this List. */ public T removeFront() { // your code here } /** * Finds and removes all values in this List that match key. * * @param key * The value to search for. */ public void removeMany(final T key) { // your code here } /** * Reverses the order of the values in this List. */ public void reverse() { // your code here } /** * Splits the contents of this List into the left and right Lists. Moves * nodes only - does not move data or call the high-level methods insert or * remove. this List is empty when done. The first half of this List is * moved to left, and the last half of this List is moved to right. If the * resulting sizes are not the same, left should have one more element than * right. * * @param left * The first List to move nodes to. * @param right * The second List to move nodes to. */ public void split(final List left, final List right) { // your code here } /** * Splits the contents of this List into the left and right Lists. Moves * nodes only - does not move data or call the high-level methods insert or * remove. this List is empty when done. Nodes are moved alternately from * this List to left and right. * * @param left * The first List to move nodes to. * @param right * The second List to move nodes to. */ public void splitAlternate(final List left, final List right) { // your code here } /** * Creates a union of two other Lists into this List. Copies data to this * list. left and right Lists are unchanged. * * @param left * The first List to create a union from. * @param right * The second List to create a union from. */ public void union(final List left, final List right) { // your code 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

Students also viewed these Databases questions

Question

Discuss communication challenges in a global environment.

Answered: 1 week ago