-
-
Save verkaufer/486633bc47fd1c735f44 to your computer and use it in GitHub Desktop.
GuestLinkedList Lab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GuestList{ | |
//head of the list | |
private GuestListNode head; | |
public GuestList(){ | |
this.head = head; | |
} | |
public void addGuest(String name, int numGuests) | |
{ | |
head = this.head; | |
GuestListNode tail = null; | |
GuestListNode current = head; | |
while(current == null){ | |
GuestListNode toAdd = new GuestListNode(name, numGuests); | |
current = toAdd; | |
current = current.getNext(); | |
} | |
//start with head, move through list and compare to sort alphabetically | |
while(current != null){ | |
GuestListNode newGuest = current; | |
if(name.compareTo(current.getName()) < 0) | |
{ //if comes before | |
//get current to temp var | |
GuestListNode temp = current; | |
//set current to newGuest | |
current = newGuest; | |
//set newguest next to temp var | |
newGuest.setNext(temp); | |
} | |
else if(name.compareTo(current.getName()) > 0) | |
{ //if comes after | |
newGuest.setNext(current.getNext()); | |
current.setNext(newGuest); | |
} | |
current.getNext(); | |
} | |
} | |
public int countGuests() | |
{ | |
int count = 0; | |
GuestListNode current = head; | |
while(current != null){ | |
count +=current.getNumGuests(); | |
current = current.getNext(); | |
} | |
return count; | |
} | |
public String toString(){ | |
GuestListNode current = head; | |
String s = "Number of guests: "+ this.countGuests()+ "\n"; | |
while(current != null) | |
{ | |
s += current.getName() + "\t" + current.getNumGuests() + "\n"; | |
current = current.getNext(); | |
} | |
return s; | |
} | |
public static void main(String[] args){ | |
GuestList g1 = new GuestList(); | |
g1.addGuest("Calypso", 1); | |
g1.addGuest("Odysseus", 3); | |
g1.addGuest("Zeus", 4); | |
System.out.print(g1); | |
} | |
} | |
class GuestListNode{ | |
private String name; | |
private int numGuests; | |
private GuestListNode next; | |
private Object data; | |
public GuestListNode(String name, int numGuests) | |
{ | |
this.name = name; | |
this.numGuests = numGuests; | |
} | |
public GuestListNode() | |
{ | |
next = null; | |
} | |
public String getName(){ | |
return name; | |
} | |
public int getNumGuests(){ | |
return numGuests; | |
} | |
public void setNumGuests(int n){ | |
this.numGuests = n; | |
} | |
public GuestListNode getNext(){ | |
return this.next; | |
} | |
public void setNext(GuestListNode next){ | |
this.next = next; | |
} | |
public GuestListNode getLast() | |
{ | |
GuestListNode current; | |
current = this; | |
while(current.getNext() != null) | |
{ | |
current = current.getNext(); | |
} | |
return current; | |
} | |
public String toString(){ | |
String s = "A guest with the name of "+ this.getName() +" and "+ this.getNumGuests() + " guests."; | |
return s; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment