Hi I am meant to create methods for a doubly linked list to add, add at the end and remove nodes from the list this is my list so far and i have added comments explaining the pseudo code behind it. The idea behind the program is its a hot potato game so when the music stops, or in our case once the hot potato has been passed n times, then the person holding the hot potato is out. The game continues until there is only 1 person left (the winner).
_____________________ Person Class(This Is Complete)________________
public class Person {
String name;
Person next;
Person previous;
// a double linked list
Person (Person previous, String name, Person next) {
this.name = name;
this.previous = previous;
this.next = next;
}
@Override
public String toString () {
String result = name;
for (Person head = next; head!=null; head=head.next) {
result += "," + head.name;
}
return result;
}
}
_________Main Class(Completed but need to get names from txt file)_______
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// read in the names of the players in the game
// you should read them in from a text fil called names.txt
String[] names = {"Gordon", "Carol","Lindsay","Ewan","Jamie","Lynda"};
//String[] names = {"Gordon","Carol","Ewan"};
//String[] names = {"Gordon"};
//String[] names = {};
// create a new HotPotato game
HotPotato list = new HotPotato();
// set n to an appropriate value
int n = 5;
// add the names to the HotPotato game
for(int i = 0; i<names.length; i++){
list.addEnd(names[i]);
}
if (list.size==0) {
System.out.println ("Empty list.");
} else if (list.size==1) {
System.out.println ("Only: " + list.remove());
} else {
System.out.println (list);
System.out.println ("First: " + list.remove());
Person q = list.list;
while (list.size > 1) {
System.out.println (list);
q = list.cycle (q, n-1);
System.out.println ("Next: " + list.remove(q));
}
System.out.println ("Winner: " + list.remove());
}
}
}
____________HotPotato Class(Need to compete methods _______________
_____________added comments for tips but unsure of coding it)___________
public class HotPotato {
Person list;
int size=0;
public HotPotato() {
}
public void add (String name) {
// TO DO
// check if the list is empty
// if empty add name to the list, with appropriate links
// else add name to the list, with appropriate links
// finally increment the size
}
public void addEnd (String name) {
Person ins = new Person(null, name, null);
Person current;
if(list == null){
list = ins;
}
else {
current = list;
while (current.next != null)
{
current = current.next;
}
current.next = ins;
size++;
}
}
// TO DO
// check if the list is empty
// if empty add name to the list, with appropriate links
// else lop thoutght he list untl you come to the end of the lits
// then add name to the list, with approriate links
// finally increment the size
public String remove () {
// TO DO
// set up a temporary String variable and set it to list.name
// get the next entry in the list
// check that it isnt null and that the netxc elent isn't null
// and remove it by seting the links to appropriate values
// decrement the size
// return the temporary String variable
}
public String remove (Person p) {
// TO DO
if (p==null) {
throw new IllegalArgumentException ();
}
if (p==list) {
return remove();
}
// set up a temporary String variable and set it to p.name
// check if previous is not null and
// if so then change the links appropriately
// check if next is not null and
// if so then change the links appropriately
// decrement the size
// return the temporary String variable
}
public Person cycle (Person p, int i) {
Person r = p;
while (i>0) {
if (r.next == null) {
r = list;
} else {
r = r.next;
}
i--;
}
return r;
}
@Override
public String toString () {
return list.toString();
}
}
