In this lab, we will write an application to store a deck of cards in a linked list, and then write
methods to sort and shuffle the deck.
Copy your completed LinkedList class from Lab 4 into the LinkedList.java file below.
Complete all methods of the Card class as described by the Javadoc comments. The class
contains both a suit and a rank. A suit is one of the categories into which the cards of a deck are
divided. The rank is the relative importance of the card within its suit.
Note that the Card constructor must convert any rank and suit letters to uppercase.
For the equals() method, be sure to follow the steps outlined in Lesson 4. How to implement the
compareTo() method is also covered in Lesson 4.
Note that you are not allowed to add any additional methods or member variables to this class or
you will not receive credit for this assignment.
Complete all methods of the CardApp class in the CardApp.java file as described by the Javadoc
comments.
You may add as many methods as you would like to this file, but are not allowed to add any
additional member variables.
The CardApp program must prompt for and allow the user to enter the name of any input file as
shown in the Example output below.
Implement the shuffle() method as specified in the comments for shuffle(). After you have
shuffled the deck of cards, write the result into a file named shuffled.txt.
Implement the sort() method using bubble sort from Lesson 4. First sort by suit in alphabetical
order and then by rank from 2 to A. The pseudocode for bubble sort is as follows:
After you have sorted the deck of cards, write the result to a file named sorted.txt.
The CardApp.java file also contains the main() method of the application. Use Develop mode to
test your CardApp code along with your Card and LinkedList code.
All input and output files must contain a list of cards, with each card stored on its own line. See
the example files cards1.txt and cards2.txt for example file formats.
[[[cards1.txt]]]
2H
3H
4H
5H
6H
7H
8H
9H
10H
JH
[[[cards2.txt]]]
AS
2S
3S
4S
5S
6S
7S
8S
9S
10S
JS
QS
KS
AC
2C
3C
4C
5C
6C
7C
8C
9C
10C
JC
QC
KC
AH
2H
3H
4H
5H
6H
7H
8H
9H
10H
JH
QH
KH
AD
2D
3D
4D
5D
6D
7D
8D
9D
10D
JD
QD
KD
[[[CardApp.java]]]
/**
* CardApp.java
* @author Your name
* @author Partner's name
* CIS 22C, Applied Lab 1
*/
public class CardApp {
private LinkedList list;
/**
* User interface prompts user, reads and writes files.
*/
public static void main(String[] args) {
}
/**
* Default constructor to initialize the deck
*/
public CardApp() {
}
/**
* Inserts a new Card into the deck
* @param card a playing Card
*/
public void addCard(Card card) {
}
/**
* Shuffles cards following this algorithm:
* First swaps first and last card
* Next, swaps every even card with the card 3
* nodes away from that card. Stops when it
* reaches the 3rd to last node
* Then, swaps ALL cards with the card that is
* 2 nodes away from it, starting at the 2nd card
* and stopping stopping at the 3rd to last node
*/
public void shuffle() {
}
/**
* Implements the bubble sort algorithm
* to sort cardList into sorted order, first by suit
* (alphabetical order)
* then by rank from 2 to A
*/
public void sort() {
}
/**
* Returns the deck of cards with each card separated
* by a blank space and a new line character at the end.
* @return The deck of cards as a string.
*/
@Override public String toString() {
return "";
}
}
[[[card.java]]]
/**
* Card.java
* @author Your name
* @author Partner's name
* CIS 22C, Applied Lab 1
*/
public class Card implements Comparable{
private String rank;
private String suit;
/**
* Constructor for the Card class
* @param rank the rank of card from 2 to A
* @param suit the suit of card C, D, H, or S
*/
public Card(String rank, String suit) {
}
/**
* Returns the card's rank
* @return rank a rank from 2 (low) to A (high)
*/
public String getRank() {
return "";
}
/**
* Returns the card's suit
* @return C, D, H, or S
*/
public String getSuit() {
return "";
}
/**
* Updates the card's rank
* @param rank a new rank
*/
public void setRank(String rank) {
}
/**
* Updates the card's suit
* @param suit the new suit
*/
public void setSuit(String suit) {
}
/**
* Concatenates rank and suit
* @return card rank and suit
*/
@Override public String toString() {
return "";
}
/**
* Overrides the equals method for Card
* Compares rank and suit and
* follows the equals formula given in
* Lesson 4 and also in Joshua Block's text
* @param obj another Object to compare for
* equality
* @return whether obj is a Card and, if so,
* of equal rank and suit
*/
@Override public boolean equals(Object obj) {
return false;
}
/**
* Orders two cards first by suit (alphabetically)
* Next by rank. "A" is considered the high card
* Order goes 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
* @param card another Card to compare to this Card
* @return a negative number if this comes before c
* and a positive number if c comes before this
* and 0 if this and c are equal according to the above
* equals method
*/
@Override public int compareTo(Card card) {
return -1;
}
}
}
[[[LinkedList.java]]] mport java.util. NoSuchElementException; ublic class LinkedList T{
private class Node { private T data; private Node next; private Node prev; public Node(T data)
{ this.data = data; this. next = null; this.prev = null; } } private int length; private Node first;
private Node last; private Node iterator; /**** CONSTRUCTORS ****/ / * Instantiates a new
Linkedlist with default values * @postcondition hello */ public LinkedList() { clear (); first =
null; last = null; iterator = null; length =; } / * Converts the given array into a Linkedlist *
@param array the array of values to insert into this LinkedList * Qpostcondition array form */
public LinkedList(T[] array) { // Iterate over the elements in the array and add them to the
LinkedList if (array == null ){ clear(); }else{ for ( T item : array) { addLast (item); } } }
/** * Instantiates a new LinkedList by copying another List * @param original the LinkedList to
copy * @postcondition a new List object, which is an identical, * but separate, copy of the
LinkedList original */ public LinkedList(LinkedList original) { // Create a new LinkedList
LinkedList newList = new LinkedList<>(); // Copy each element from the original List to the
new List Node current = original.first; while (current != null) { newList.addLast(current.data);
current = current.next; } // Update the fields of the current instance to match the new List
this.first = newList.first; this.last = newList.last; this.iterator = newList.iterator; this.length =
newList.length; } / * Instantiates a new LinkedList by copying another List * @param original
the Linkedlist to copy * @postcondition a new List object, which is an identical, * but separate,
copy of the Linkedlist original */ public LinkedList(LinkedList T original) { // Create a new
LinkedList LinkedList T newList = new LinkedList langlerangle() ; // Copy each element from
the original list to the new list Node current = original.first; while (current != null) {
newList.addLast (current.data); current = current. next; } // Update the fields of the current
instance to match the new list this.first = newList.first; this.last = newList.last; this.iterator =
newList. iterator; this.length = newList.length; } /**** ACCESSORS ****/ / * Returns the
value stored in the first node * @precondition get first data element * @return the value stored at
node first * @throws NoSuchElementException no such element exception */ public T getFirst()
throws NoSuchElementException { if (first == null) throw new NoSuchElementException();
return first.data; } / * Returns the value stored in the last node * @precondition get last data
element * Qreturn the value stored in the node last * @throws NoSuchElementException no
such element exception */ public T getLast() throws NoSuchElementException { if (last ==
null) throw new NoSuchElementException(); return last.data; }
/ * removes the element referenced by the iterator * @precondition != null * @postcondition !=
null * @throws NullpointerException null pointer exception */ public void removeIterator()
throws NullpointerException { if (iterator == null) { throw new NullPointerException("Iterator
is off end."); } if (iterator.prev != null) { iterator. prev . next = iterator . next; } if (iterator. next
!= null) { iterator . next. prev = iterator . prev; } if (iterator == first) { first = iterator . next; }
if (iterator == last) { last = iterator. prev; } iterator = null; length--; } /** * places the iterator at
the first node * @postcondition position == first */ public void positionIterator(){ iterator =
first; } /** * Moves the iterator one node towards the last * @precondition != null *
@postcondition != null * @throws NullPointerException null pointer exception */ public void
advanceIterator() throws NullPointerException { if (iterator == null) { throw new
NullPointerException("Iterator is off end."); } iterator = iterator . next; } /** * Moves the
iterator one node towards the first * @precondition not empty * (apostcondition l= null
iterator . next. prev = newNode; } iterator. next = newNode; length++; } / * removes the
element at the front of the LinkedList * Qprecondition remove first element * @postcondition
remove first element * @throws NoSuchElementException no such element exception */ public
void removefirst() throws NoSuchElementException { if (first == null) throw new
NoSuchElementException(); if (length ==1 ) { first = null; last = null; iterator = null; } else {
first = first. next; first.prev = first; } length--; return; } / * removes the element at the end of the
Linkedlist * @precondition remove last element * Qpostcondition remove last element *
@throws NoSuchElementException no such element exception / public void removelast()
throws NoSuchElementException { if (last == null) throw new NoSuchElementException(); if
(length ==1 ) { first = null; last = null; iterator = null; } else { if (iterator == last) { iterator =
null; } last = last.prev; last. next = null; } length--; return;
* Returns the data stored in the iterator node * @precondition get iterator * Qreturn the data
stored in the iterator node * Qthrow NullpointerException null pointer exception / public T
getiterator() throws NullPointerException { if (iterator == null) { throw new
NullPointerException("Iterator is off end."); } return iterator.data; } /** * Returns the current
length of the LinkedList * Qreturn the length of the LinkedList from to n */ public int
getLength() { return length; } / * Returns whether the Linkedlist is currently empty * Qreturn
whether the Linkedlist is empty */ public boolean isEmpty() { return length ==; } / * Returns
whether the iterator is offEnd, i.e. null * @return whether the iterator is null */ public boolean
offEnd() { return iterator == null; } /**** MUTATORS ****/
* @throws NullPointerException null pointer exception */ public void reverseIterator() throws
NullPointerException { if (iterator == null) { throw new NullPointerException("Iterator is off
end."); } iterator = iterator prev; } /**** ADDITIONAL OPERATIONS ****/ / * Re-sets
Linkedlist to empty as if the * default constructor had just been called */ public void clear() {
first = null; last = null; iterator = null; length =; } / * Converts the Linkedlist to a string, with
each value separated by a blank * Line At the end of the String, place a new line character *
@return the LinkedList as a String */ @override public string tostring() { StringBuilder result =
new StringBuilder(); Node temp = first; while (temp != null) {
result.append(temp.data).append(" "); temp = temp. next; } return result.tostring ()+"n"; } / *
Determines whether the given object is * another LinkedList, containing * the same data in the
same order * @param obj another object * @return whether there is equality */
@SuppressWarnings("unchecked") //good practice to remove warning here @override public
boolean equals(object obj) { if (obj == this) { return true; }else if(!(obj instanceof LinkedList))
{
for (int i=0; i< length - numMoves % length; i++ ) { iterator = iterator . next; } first = iterator;
last = iterator .prev; last. next = null; } / * Splices together two Linkedlists to create a third List
* which contains alternating values from this list * and the given parameter * For example:
[1,2,3] and [4,5,6][1,4,2,5,3,6] * For example: [1,2,3,4] and [5,6][1,5,2,6,3,4] * For example:
[1,2] and [3,4,5,6][1,3,2,4,5,6] * @param list the second LinkedList * Qreturn a new LinkedList,
which is the result of * interlocking this and list * Qpostcondition this and list are unchanged */
public LinkedList T altLists (LinkedList T list) { LinkedList T result = new LinkedList
langlerangle() ; clear(); Node currentThis = this.first; Node currentlist = null; if(list != null) {
currentList = list.first; } while (currentThis != null || currentList != null) { if (currentThis !=
null) { result.addLast (currentThis.data); currentThis = currentThis. next; } if (currentlist !=
null) { result.addLast (currentList.data); currentList = currentList. next; } }
}else{ LinkedList TL= (LinkedList T ) obj; if(length != L. length) { return false; }else{ Node
temp1 = this.first; Node temp2 = L.first; while(temp1 != null) { if ( temp1. data == null || temp2.
data == null ){ if(temp1.data != temp2.data) { return false; } }else if(!
(temp1.data.equals(temp2.data))){ return false; } temp1 = temp1. next; temp2 = temp2. next; }
return true; } } } / CHALLENGE METHODS*/ / * Moves all nodes in the List towards the
end * of the list the number of times specified * Any node that falls off the end of the list as it *
moves forward will be placed the front of the list * For example: [1,2,3,4,5], numMoves
=2[4,5,1,2,3] * For example: [1,2,3,4,5], numMoves =4[2,3,4,5,1] * For example: [1,2,3,4,5],
numMoves =7[4,5,1,2,3] * @param numMoves the number of times to move each node. *
Qprecondition numMoves >=0 * @postcondition iterator position unchanged (i.e. still
referencing * the same node in the list, regardless of new Location of Node) * @throws
IllegalArgumentException when numMoves <0 */ public void spinList(int numMoves) throws
IllegalArgumentException{ if (numMoves < ) { throw new
IllegalArgumentException("numMoves cannot be negative"); } if(isEmpty()) { return; }
/** * Creates a new first element * Qparam data the data to insert at the front of the LinkedList *
Qpostcondition add first data element / public void addFirst( T data) { Node newNode = new
Node ( data ); if (length == ) { first = newNode; last = newNode; } else { newNode. next =
first; first.prev = newNode; first = newNode; } length++; return; } 1** * Creates a new last
element * Qparam data the data to insert at the end of the LinkedList * @postcondition add last
data element / public void addLast( T data) { Node newNode = new Node ( data ); if (length ==
) { first = newNode; last = newNode; } else { last. next = newNode; newNode.prev = last; last
= newNode; } length++; return; } / * Inserts a new element after the iterator * Qparam data the
data to insert * @precondition != null * Qthrows NullPointerException null pointer exception */
public void addIterator( T data) throws NullPointerException if (iterator == null) { throw new
NullPointerException("Iterator is off end."); } Node newNode = new Node ( data );
newNode.prev = iterator; newNode. next = iterator . next;

In this lab, we will write an application to store a deck of cards i.pdf

  • 1.
    In this lab,we will write an application to store a deck of cards in a linked list, and then write methods to sort and shuffle the deck. Copy your completed LinkedList class from Lab 4 into the LinkedList.java file below. Complete all methods of the Card class as described by the Javadoc comments. The class contains both a suit and a rank. A suit is one of the categories into which the cards of a deck are divided. The rank is the relative importance of the card within its suit. Note that the Card constructor must convert any rank and suit letters to uppercase. For the equals() method, be sure to follow the steps outlined in Lesson 4. How to implement the compareTo() method is also covered in Lesson 4. Note that you are not allowed to add any additional methods or member variables to this class or you will not receive credit for this assignment. Complete all methods of the CardApp class in the CardApp.java file as described by the Javadoc comments. You may add as many methods as you would like to this file, but are not allowed to add any additional member variables. The CardApp program must prompt for and allow the user to enter the name of any input file as shown in the Example output below. Implement the shuffle() method as specified in the comments for shuffle(). After you have shuffled the deck of cards, write the result into a file named shuffled.txt. Implement the sort() method using bubble sort from Lesson 4. First sort by suit in alphabetical order and then by rank from 2 to A. The pseudocode for bubble sort is as follows: After you have sorted the deck of cards, write the result to a file named sorted.txt. The CardApp.java file also contains the main() method of the application. Use Develop mode to test your CardApp code along with your Card and LinkedList code. All input and output files must contain a list of cards, with each card stored on its own line. See the example files cards1.txt and cards2.txt for example file formats. [[[cards1.txt]]] 2H 3H 4H 5H 6H 7H 8H
  • 2.
  • 3.
    6H 7H 8H 9H 10H JH QH KH AD 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD [[[CardApp.java]]] /** * CardApp.java * @authorYour name * @author Partner's name * CIS 22C, Applied Lab 1 */ public class CardApp { private LinkedList list; /** * User interface prompts user, reads and writes files. */ public static void main(String[] args) { }
  • 4.
    /** * Default constructorto initialize the deck */ public CardApp() { } /** * Inserts a new Card into the deck * @param card a playing Card */ public void addCard(Card card) { } /** * Shuffles cards following this algorithm: * First swaps first and last card * Next, swaps every even card with the card 3 * nodes away from that card. Stops when it * reaches the 3rd to last node * Then, swaps ALL cards with the card that is * 2 nodes away from it, starting at the 2nd card * and stopping stopping at the 3rd to last node */ public void shuffle() { } /** * Implements the bubble sort algorithm * to sort cardList into sorted order, first by suit * (alphabetical order) * then by rank from 2 to A */ public void sort() { } /** * Returns the deck of cards with each card separated * by a blank space and a new line character at the end. * @return The deck of cards as a string. */
  • 5.
    @Override public StringtoString() { return ""; } } [[[card.java]]] /** * Card.java * @author Your name * @author Partner's name * CIS 22C, Applied Lab 1 */ public class Card implements Comparable{ private String rank; private String suit; /** * Constructor for the Card class * @param rank the rank of card from 2 to A * @param suit the suit of card C, D, H, or S */ public Card(String rank, String suit) { } /** * Returns the card's rank * @return rank a rank from 2 (low) to A (high) */ public String getRank() { return ""; } /** * Returns the card's suit * @return C, D, H, or S */ public String getSuit() { return ""; }
  • 6.
    /** * Updates thecard's rank * @param rank a new rank */ public void setRank(String rank) { } /** * Updates the card's suit * @param suit the new suit */ public void setSuit(String suit) { } /** * Concatenates rank and suit * @return card rank and suit */ @Override public String toString() { return ""; } /** * Overrides the equals method for Card * Compares rank and suit and * follows the equals formula given in * Lesson 4 and also in Joshua Block's text * @param obj another Object to compare for * equality * @return whether obj is a Card and, if so, * of equal rank and suit */ @Override public boolean equals(Object obj) { return false; } /** * Orders two cards first by suit (alphabetically) * Next by rank. "A" is considered the high card * Order goes 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
  • 7.
    * @param cardanother Card to compare to this Card * @return a negative number if this comes before c * and a positive number if c comes before this * and 0 if this and c are equal according to the above * equals method */ @Override public int compareTo(Card card) { return -1; } } } [[[LinkedList.java]]] mport java.util. NoSuchElementException; ublic class LinkedList T{ private class Node { private T data; private Node next; private Node prev; public Node(T data) { this.data = data; this. next = null; this.prev = null; } } private int length; private Node first; private Node last; private Node iterator; /**** CONSTRUCTORS ****/ / * Instantiates a new Linkedlist with default values * @postcondition hello */ public LinkedList() { clear (); first = null; last = null; iterator = null; length =; } / * Converts the given array into a Linkedlist * @param array the array of values to insert into this LinkedList * Qpostcondition array form */ public LinkedList(T[] array) { // Iterate over the elements in the array and add them to the LinkedList if (array == null ){ clear(); }else{ for ( T item : array) { addLast (item); } } }
  • 8.
    /** * Instantiatesa new LinkedList by copying another List * @param original the LinkedList to copy * @postcondition a new List object, which is an identical, * but separate, copy of the LinkedList original */ public LinkedList(LinkedList original) { // Create a new LinkedList LinkedList newList = new LinkedList<>(); // Copy each element from the original List to the new List Node current = original.first; while (current != null) { newList.addLast(current.data); current = current.next; } // Update the fields of the current instance to match the new List this.first = newList.first; this.last = newList.last; this.iterator = newList.iterator; this.length = newList.length; } / * Instantiates a new LinkedList by copying another List * @param original the Linkedlist to copy * @postcondition a new List object, which is an identical, * but separate, copy of the Linkedlist original */ public LinkedList(LinkedList T original) { // Create a new LinkedList LinkedList T newList = new LinkedList langlerangle() ; // Copy each element from the original list to the new list Node current = original.first; while (current != null) { newList.addLast (current.data); current = current. next; } // Update the fields of the current instance to match the new list this.first = newList.first; this.last = newList.last; this.iterator = newList. iterator; this.length = newList.length; } /**** ACCESSORS ****/ / * Returns the value stored in the first node * @precondition get first data element * @return the value stored at node first * @throws NoSuchElementException no such element exception */ public T getFirst() throws NoSuchElementException { if (first == null) throw new NoSuchElementException(); return first.data; } / * Returns the value stored in the last node * @precondition get last data element * Qreturn the value stored in the node last * @throws NoSuchElementException no such element exception */ public T getLast() throws NoSuchElementException { if (last == null) throw new NoSuchElementException(); return last.data; } / * removes the element referenced by the iterator * @precondition != null * @postcondition != null * @throws NullpointerException null pointer exception */ public void removeIterator() throws NullpointerException { if (iterator == null) { throw new NullPointerException("Iterator is off end."); } if (iterator.prev != null) { iterator. prev . next = iterator . next; } if (iterator. next != null) { iterator . next. prev = iterator . prev; } if (iterator == first) { first = iterator . next; } if (iterator == last) { last = iterator. prev; } iterator = null; length--; } /** * places the iterator at the first node * @postcondition position == first */ public void positionIterator(){ iterator = first; } /** * Moves the iterator one node towards the last * @precondition != null * @postcondition != null * @throws NullPointerException null pointer exception */ public void advanceIterator() throws NullPointerException { if (iterator == null) { throw new NullPointerException("Iterator is off end."); } iterator = iterator . next; } /** * Moves the
  • 9.
    iterator one nodetowards the first * @precondition not empty * (apostcondition l= null iterator . next. prev = newNode; } iterator. next = newNode; length++; } / * removes the element at the front of the LinkedList * Qprecondition remove first element * @postcondition remove first element * @throws NoSuchElementException no such element exception */ public void removefirst() throws NoSuchElementException { if (first == null) throw new NoSuchElementException(); if (length ==1 ) { first = null; last = null; iterator = null; } else { first = first. next; first.prev = first; } length--; return; } / * removes the element at the end of the Linkedlist * @precondition remove last element * Qpostcondition remove last element * @throws NoSuchElementException no such element exception / public void removelast() throws NoSuchElementException { if (last == null) throw new NoSuchElementException(); if (length ==1 ) { first = null; last = null; iterator = null; } else { if (iterator == last) { iterator = null; } last = last.prev; last. next = null; } length--; return; * Returns the data stored in the iterator node * @precondition get iterator * Qreturn the data stored in the iterator node * Qthrow NullpointerException null pointer exception / public T getiterator() throws NullPointerException { if (iterator == null) { throw new NullPointerException("Iterator is off end."); } return iterator.data; } /** * Returns the current length of the LinkedList * Qreturn the length of the LinkedList from to n */ public int getLength() { return length; } / * Returns whether the Linkedlist is currently empty * Qreturn whether the Linkedlist is empty */ public boolean isEmpty() { return length ==; } / * Returns whether the iterator is offEnd, i.e. null * @return whether the iterator is null */ public boolean offEnd() { return iterator == null; } /**** MUTATORS ****/ * @throws NullPointerException null pointer exception */ public void reverseIterator() throws NullPointerException { if (iterator == null) { throw new NullPointerException("Iterator is off end."); } iterator = iterator prev; } /**** ADDITIONAL OPERATIONS ****/ / * Re-sets Linkedlist to empty as if the * default constructor had just been called */ public void clear() { first = null; last = null; iterator = null; length =; } / * Converts the Linkedlist to a string, with each value separated by a blank * Line At the end of the String, place a new line character * @return the LinkedList as a String */ @override public string tostring() { StringBuilder result = new StringBuilder(); Node temp = first; while (temp != null) { result.append(temp.data).append(" "); temp = temp. next; } return result.tostring ()+"n"; } / * Determines whether the given object is * another LinkedList, containing * the same data in the same order * @param obj another object * @return whether there is equality */ @SuppressWarnings("unchecked") //good practice to remove warning here @override public
  • 10.
    boolean equals(object obj){ if (obj == this) { return true; }else if(!(obj instanceof LinkedList)) { for (int i=0; i< length - numMoves % length; i++ ) { iterator = iterator . next; } first = iterator; last = iterator .prev; last. next = null; } / * Splices together two Linkedlists to create a third List * which contains alternating values from this list * and the given parameter * For example: [1,2,3] and [4,5,6][1,4,2,5,3,6] * For example: [1,2,3,4] and [5,6][1,5,2,6,3,4] * For example: [1,2] and [3,4,5,6][1,3,2,4,5,6] * @param list the second LinkedList * Qreturn a new LinkedList, which is the result of * interlocking this and list * Qpostcondition this and list are unchanged */ public LinkedList T altLists (LinkedList T list) { LinkedList T result = new LinkedList langlerangle() ; clear(); Node currentThis = this.first; Node currentlist = null; if(list != null) { currentList = list.first; } while (currentThis != null || currentList != null) { if (currentThis != null) { result.addLast (currentThis.data); currentThis = currentThis. next; } if (currentlist != null) { result.addLast (currentList.data); currentList = currentList. next; } } }else{ LinkedList TL= (LinkedList T ) obj; if(length != L. length) { return false; }else{ Node temp1 = this.first; Node temp2 = L.first; while(temp1 != null) { if ( temp1. data == null || temp2. data == null ){ if(temp1.data != temp2.data) { return false; } }else if(! (temp1.data.equals(temp2.data))){ return false; } temp1 = temp1. next; temp2 = temp2. next; } return true; } } } / CHALLENGE METHODS*/ / * Moves all nodes in the List towards the end * of the list the number of times specified * Any node that falls off the end of the list as it * moves forward will be placed the front of the list * For example: [1,2,3,4,5], numMoves =2[4,5,1,2,3] * For example: [1,2,3,4,5], numMoves =4[2,3,4,5,1] * For example: [1,2,3,4,5], numMoves =7[4,5,1,2,3] * @param numMoves the number of times to move each node. * Qprecondition numMoves >=0 * @postcondition iterator position unchanged (i.e. still referencing * the same node in the list, regardless of new Location of Node) * @throws IllegalArgumentException when numMoves <0 */ public void spinList(int numMoves) throws IllegalArgumentException{ if (numMoves < ) { throw new IllegalArgumentException("numMoves cannot be negative"); } if(isEmpty()) { return; } /** * Creates a new first element * Qparam data the data to insert at the front of the LinkedList * Qpostcondition add first data element / public void addFirst( T data) { Node newNode = new Node ( data ); if (length == ) { first = newNode; last = newNode; } else { newNode. next = first; first.prev = newNode; first = newNode; } length++; return; } 1** * Creates a new last element * Qparam data the data to insert at the end of the LinkedList * @postcondition add last data element / public void addLast( T data) { Node newNode = new Node ( data ); if (length ==
  • 11.
    ) { first= newNode; last = newNode; } else { last. next = newNode; newNode.prev = last; last = newNode; } length++; return; } / * Inserts a new element after the iterator * Qparam data the data to insert * @precondition != null * Qthrows NullPointerException null pointer exception */ public void addIterator( T data) throws NullPointerException if (iterator == null) { throw new NullPointerException("Iterator is off end."); } Node newNode = new Node ( data ); newNode.prev = iterator; newNode. next = iterator . next;