TileNode.Java public class TileNode{ // instance variables private int data; pri
ID: 3594717 • Letter: T
Question
TileNode.Java
public class TileNode{
// instance variables
private int data;
private TileNode link;
public TileNode()
{}
// constructor
public TileNode(int initialData, TileNode initialLink) {
data = initialData;
link = initialLink;
}
// the calling object is a reference to the
// node before the location where I want to add
public void addNodeAfter(int item){
link = new TileNode(item, link);
}
public int getData( ) {
return data;
}
public TileNode getLink( ){
return link;
}
public static TileNode[ ] split ( TileNode original, int sValue ) {
TileNode cursor = original;
TileNode answer[ ] = new TileNode [ 2 ];
TileNode list1=null, list1cursor=null;
TileNode list2=null, list2cursor=null;
while ( cursor != null ) {
if ( cursor.getData() < sValue ) {
if ( list1cursor == null) { // list 1 is empty
list1 = new TileNode( cursor.getData(), null );
list1cursor = list1;
}
else {
list1cursor.setLink(new TileNode(cursor.getData(), null));
list1cursor = list1cursor.getLink();
}
cursor = cursor.link;
} // end if data < sValue
else {
if ( list2cursor == null) { // list 2 is empty
list2 = new TileNode( cursor.getData(), null );
list2cursor = list2;
}
else {
list2cursor.setLink(new TileNode(cursor.getData(), null));
list2cursor = list2cursor.getLink();
}
cursor = cursor.link;
} // end data >= splitValue
} // end while
answer[0] = list1;
answer [1] = list2;
return answer;
}// end method
public static String listToString( TileNode head ) {
String answer = "";
TileNode cursor = head;
while (cursor != null) {
answer = answer + cursor.getData();
if (cursor.getLink() == null)
answer = answer + " End of List";
else
answer = answer + " --> ";
cursor = cursor.getLink();
}
return answer;
}
// call this method using IntNode.listCopy(reference to the head)
// it will duplicate the list and return a reference which
// is the head of the new list
public static TileNode listCopy(TileNode source){
TileNode copyHead;
TileNode copyTail;
// Handle the special case of the empty list.
if (source == null)
return null;
// Make the first node for the newly created list.
copyHead = new TileNode(source.data, null);
copyTail = copyHead;
// Make the rest of the nodes for the newly created list.
while (source.link != null){
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
// Return the head reference for the new list.
return copyHead;
}
public static int listLength(TileNode head){
int answer = 0;
for (TileNode cursor = head; cursor != null; cursor = cursor.link)
answer++;
return answer;
}
// search for a particular data value in the list
// return a reference to the node where the value
// was found
// if the value isn't in the list, return null
public static TileNode listSearch(TileNode head, int target){
TileNode cursor;
for (cursor = head; cursor != null; cursor = cursor.link)
if (target == cursor.data)
return cursor;
return null;
}
// this method is called using the node
// that is before the one we want to remove
public void removeNodeAfter( ){
link = link.link;
}
// mutator
public void setData(int newData){
data = newData;
}
public void setLink(TileNode newLink){
link = newLink;
}
//added portion for the lab
//wont compile
public static void printList(TileNode head){
if(head == null)
return;
TileNode node = head;
while(true) {
System.out.println(node.data);
if(node.link != null)
node=node.link;
else
break;
}
}
public static TileNode insertAtHead(TileNode head, TileNode tile){
TileNode node = new TileNode();
node.data = tile.data;
node.link = head;
return node;
}
public static TileNode reverse(TileNode original){
TileNode prev = null;
TileNode cur = original;
TileNode link = null;
while(cur != null) {
link = cur.link;
cur.link = prev;
prev = cur;
cur = link;
}
original = prev;
return original;
}
}
Explanation / Answer
Here is the code for QwirkleHandTest.java
class QwirkleHandTest
{
public static void main(String[] args)
{
TileNode qwirkleHand = null;
QwirkleBag game1 = new QwirkleBag();
//Draw 6 tiles out of the bag, and add them to the player's hand.
for(int t = 1; t <= 6; t++)
qwirkleHand = TileNode.insertAtHead(qwirkleHand, new TileNode(game1.draw(), null));
//Display the tiles in the player's hand.
TileNode.printList(qwirkleHand);
}
}
But for this to work, you need to provide the QwirkleBag.java class as well. TileNode.java will not be enough to run the code successfully.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.