Question

Don't forget to have the main method A. Write a class that maintains the top ten...

Don't forget to have the main method

A. Write a class that maintains the top ten scores for a game application, implementing the add and remove methods but using a singly linked list instead of an array.

B. Perform the previous project, but use a doubly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the game entry at index i.

Homework Answers

Answer #1

Answer -1:-

File: DoublyLinkedList.java

// DoublyLinkedList class implementation
public class DoublyLinkedList
{
// ---------------- nested Node class ----------------
public static class Node
{
private GameEntry element;
private Node prev;
private Node next;

public Node(GameEntry e, Node p, Node n)
{
element = e;
prev = p;
next = n;
}
  
public GameEntry getElement()
{
return element;
}

public Node getPrev()
{
return prev;
}

public Node getNext()
{
return next;
}

public void setPrev(Node p)
{
prev = p;
}

public void setNext(Node n)
{
next = n;
}
} // ----------- end of nested Node class -----------

// instance variables
private Node header;
private Node trailer;
private int size = 0;
  
public DoublyLinkedList()
{
header = new Node(null, null, null);
trailer = new Node(null, header, null);
header.setNext(trailer);
}
  
public int size()
{
return size;
}

public boolean isEmpty()
{
return size == 0;
}

public Node getHeader()
{
return header;
}

public Node getTrailer()
{
return trailer;
}

public GameEntry first()
{
if (isEmpty())
return null;
  
return header.getNext().getElement();
}

public GameEntry last()
{
if (isEmpty())
return null;
  
return trailer.getPrev().getElement();
}
  
public void addFirst(GameEntry e)
{
addBetween(e, header, header.getNext());
}

public void addLast(GameEntry e)
{
addBetween(e, trailer.getPrev(), trailer);
}

public GameEntry removeFirst()
{
if (isEmpty())
return null;
  
return remove(header.getNext());
}

public GameEntry removeLast()
{
if (isEmpty())
return null;
return remove(trailer.getPrev());
}
  
private void addBetween(GameEntry e, Node predecessor, Node successor)
{
Node newNode = new Node(e, predecessor, successor);
predecessor.setNext(newNode);
successor.setPrev(newNode);
size++;
}

private GameEntry remove(Node node)
{
Node predecessor = node.getPrev();
Node successor = node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getElement();
}

public String toString()
{
StringBuilder sb = new StringBuilder("(");
Node walk = header.getNext();
while (walk != trailer)
{
sb.append(walk.getElement());
walk = walk.getNext();
if (walk != trailer)
sb.append(", ");
}
sb.append(")");
return sb.toString();
}
  
public void add(GameEntry ge)
{
if(size == 10 && ge.getScore() < header.next.element.getScore())
{
return;
}
  
if (isEmpty())
{
Node newNode = new Node(ge, header, trailer);
header.next = newNode;
trailer.prev = newNode;
}
else if(ge.getScore() < header.next.element.getScore())
{
Node newNode = new Node(ge, header, header.next);
header.next = newNode;
header.next.prev = newNode;
}
else if(ge.getScore() > trailer.prev.element.getScore())
{
Node newNode = new Node(ge, trailer.prev, trailer);
trailer.prev.next = newNode;
trailer.prev = newNode;
}
else
{
Node current = header.next;
Node previous = null;
  
while(current != trailer && ge.getScore() > current.element.getScore())
{
previous = current;
current = current.next;
}
  
Node newNode = new Node(ge, previous, previous.next);
previous.next = newNode;
previous.next.prev = newNode;
}
  
incrementSize();
  
if(size > 10)
{
header.next = header.next.next;
decrementSize();
}
}

public GameEntry remove(int i)
throws IndexOutOfBoundsException
{
if (i < 0 || i >= size)
{
throw new IndexOutOfBoundsException(
"Invalid index: " + i);
}
  
int count = 0;
Node current = header.next;
Node previous = null;
while (current != trailer && count < i)
{
previous = current;
current = current.next;
count++;
}
GameEntry result = null;
if (i == 0)
{
result = header.next.element;
header.next = header.next.next;
}
else if (i == size - 1)
{
result = trailer.prev.element;
previous.next = trailer;
trailer = previous.next;
}
else
{
result = previous.next.element;
previous.next = current.next;
current.next.prev = previous;
}
decrementSize();
  
if (size == 0)
trailer = null;
  
return result;
}

public void incrementSize()
{
size++;
}
  
public void decrementSize()
{
size--;
}
  
} // end of DoublyLinkedList class

File: SinglyLinkedListDriver.java

// SinglyLinkedListDriver class implementation
public class SinglyLinkedListDriver
{
public static void main(String[] args)
{
SinglyLinkedList highscores = new SinglyLinkedList();
String[] names = { "Rob", "Mike", "Rose", "Jill", "Jack", "Anna", "Paul", "Bob" };
int[] scores = { 750, 1105, 590, 740, 510, 660, 720, 400 };
  
for (int i = 0; i < names.length; i++)
{
GameEntry gE = new GameEntry(names[i], scores[i]);
System.out.println("Adding " + gE);
highscores.add(gE);
}
  
System.out.println("\nTraversing the linked list:");
SinglyLinkedList.Node node = highscores.getHead();
while (node != null)
{
System.out.printf("%s -> ", node.getElement());
node = node.getNext();
}
System.out.println("null");
  
System.out.println("\nRemoving " + highscores.remove(3));
  
System.out.println("\nTraversing the linked list:");
node = highscores.getHead();
while (node != null)
{
System.out.printf("%s -> ", node.getElement());
node = node.getNext();
}
System.out.println("null");
}
} // end of SinglyLinkedListDriver class

File: GameEntry.java

// GameEntry class implementation
public class GameEntry
{
private String name;
private int score;

public GameEntry(String n, int s)
{
name = n;
score = s;
}

public String getName()
{
return name;
}

public int getScore()
{
return score;
}

public String toString()
{
return "(" + name + ", " + score + ")";
}
} // end of GameEntry class

Sample Output:

Adding (Rob, 750)
Adding (Mike, 1105)
Adding (Rose, 590)
Adding (Jill, 740)
Adding (Jack, 510)
Adding (Anna, 660)
Adding (Paul, 720)
Adding (Bob, 400)

Traversing the linked list:
(Bob, 400) -> (Jack, 510) -> (Rose, 590) -> (Anna, 660) -> (Paul, 720) -> (Jill, 740) -> (Rob, 750) -> (Mike, 1105) -> null

Removing (Anna, 660)

Traversing the linked list:
(Bob, 400) -> (Jack, 510) -> (Rose, 590) -> (Paul, 720) -> (Jill, 740) -> (Rob, 750) -> (Mike, 1105) -> null

-----------------------------------------------------------------------

Answer 2 (DoublyLinkedList):

File: DoublyLinkedList.java

// DoublyLinkedList class implementation
public class DoublyLinkedList
{
// ---------------- nested Node class ----------------
public static class Node
{
private GameEntry element;
private Node prev;
private Node next;

public Node(GameEntry e, Node p, Node n)
{
element = e;
prev = p;
next = n;
}
  
public GameEntry getElement()
{
return element;
}

public Node getPrev()
{
return prev;
}

public Node getNext()
{
return next;
}

public void setPrev(Node p)
{
prev = p;
}

public void setNext(Node n)
{
next = n;
}
} // ----------- end of nested Node class -----------

// instance variables
private Node header;
private Node trailer;
private int size = 0;
  
public DoublyLinkedList()
{
header = new Node(null, null, null);
trailer = new Node(null, header, null);
header.setNext(trailer);
}
  
public int size()
{
return size;
}

public boolean isEmpty()
{
return size == 0;
}

public Node getHeader()
{
return header;
}

public Node getTrailer()
{
return trailer;
}

public GameEntry first()
{
if (isEmpty())
return null;
  
return header.getNext().getElement();
}

public GameEntry last()
{
if (isEmpty())
return null;
  
return trailer.getPrev().getElement();
}
  
public void addFirst(GameEntry e)
{
addBetween(e, header, header.getNext());
}

public void addLast(GameEntry e)
{
addBetween(e, trailer.getPrev(), trailer);
}

public GameEntry removeFirst()
{
if (isEmpty())
return null;
  
return remove(header.getNext());
}

public GameEntry removeLast()
{
if (isEmpty())
return null;
return remove(trailer.getPrev());
}
  
private void addBetween(GameEntry e, Node predecessor, Node successor)
{
Node newNode = new Node(e, predecessor, successor);
predecessor.setNext(newNode);
successor.setPrev(newNode);
size++;
}

private GameEntry remove(Node node)
{
Node predecessor = node.getPrev();
Node successor = node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getElement();
}

public String toString()
{
StringBuilder sb = new StringBuilder("(");
Node walk = header.getNext();
while (walk != trailer)
{
sb.append(walk.getElement());
walk = walk.getNext();
if (walk != trailer)
sb.append(", ");
}
sb.append(")");
return sb.toString();
}
  
public void add(GameEntry ge)
{
if(size == 10 && ge.getScore() < header.next.element.getScore())
{
return;
}
  
if (isEmpty())
{
Node newNode = new Node(ge, header, trailer);
header.next = newNode;
trailer.prev = newNode;
}
else if(ge.getScore() < header.next.element.getScore())
{
Node newNode = new Node(ge, header, header.next);
header.next = newNode;
header.next.prev = newNode;
}
else if(ge.getScore() > trailer.prev.element.getScore())
{
Node newNode = new Node(ge, trailer.prev, trailer);
trailer.prev.next = newNode;
trailer.prev = newNode;
}
else
{
Node current = header.next;
Node previous = null;
  
while(current != trailer && ge.getScore() > current.element.getScore())
{
previous = current;
current = current.next;
}
  
Node newNode = new Node(ge, previous, previous.next);
previous.next = newNode;
previous.next.prev = newNode;
}
  
incrementSize();
  
if(size > 10)
{
header.next = header.next.next;
decrementSize();
}
}

public GameEntry remove(int i)
throws IndexOutOfBoundsException
{
if (i < 0 || i >= size)
{
throw new IndexOutOfBoundsException(
"Invalid index: " + i);
}
  
int count = 0;
Node current = header.next;
Node previous = null;
while (current != trailer && count < i)
{
previous = current;
current = current.next;
count++;
}
GameEntry result = null;
if (i == 0)
{
result = header.next.element;
header.next = header.next.next;
}
else if (i == size - 1)
{
result = trailer.prev.element;
previous.next = trailer;
trailer = previous.next;
}
else
{
result = previous.next.element;
previous.next = current.next;
current.next.prev = previous;
}
decrementSize();
  
if (size == 0)
trailer = null;
  
return result;
}

public void incrementSize()
{
size++;
}
  
public void decrementSize()
{
size--;
}
  
} // end of DoublyLinkedList class

File: DoublyLinkedListDriver.java

// DoublyLinkedListDriver class implementation
public class DoublyLinkedListDriver
{
public static void main(String[] args)
{
DoublyLinkedList highscores = new DoublyLinkedList();
String[] names = { "Rob", "Mike", "Rose", "Jill", "Jack", "Anna", "Paul", "Bob" };
int[] scores = { 750, 1105, 590, 740, 510, 660, 720, 400 };
  
for (int i = 0; i < names.length; i++)
{
GameEntry gE = new GameEntry(names[i], scores[i]);
System.out.println("Adding " + gE);
highscores.add(gE);
}
  
System.out.println("\nTraversing the linked list:");
System.out.println(highscores);
  
System.out.println("\nRemoving " + highscores.remove(3));
  
System.out.println("\nTraversing the linked list:");
System.out.println(highscores);
}
} // end of DoublyLinkedListDriver class

File: GameEntry.java

// GameEntry class implementation
public class GameEntry
{
private String name;
private int score;

public GameEntry(String n, int s)
{
name = n;
score = s;
}

public String getName()
{
return name;
}

public int getScore()
{
return score;
}

public String toString()
{
return "(" + name + ", " + score + ")";
}
} // end of GameEntry class

Sample Output:

Adding (Rob, 750)
Adding (Mike, 1105)
Adding (Rose, 590)
Adding (Jill, 740)
Adding (Jack, 510)
Adding (Anna, 660)
Adding (Paul, 720)
Adding (Bob, 400)

Traversing the linked list:
((Bob, 400), (Jack, 510), (Rose, 590), (Anna, 660), (Paul, 720), (Jill, 740), (Rob, 750), (Mike, 1105))

Removing (Anna, 660)

Traversing the linked list:
((Bob, 400), (Jack, 510), (Rose, 590), (Paul, 720), (Jill, 740), (Rob, 750), (Mike, 1105))

-----------------------------------------------

Please give me a UPVOTE. Thank you :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
c++ Program Description You are going to write a computer program/prototype to process mail packages that...
c++ Program Description You are going to write a computer program/prototype to process mail packages that are sent to different cities. For each destination city, a destination object is set up with the name of the city, the count of packages to the city and the total weight of all the packages. The destination object is updated periodically when new packages are collected. You will maintain a list of destination objects and use commands to process data in the list....
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT