Having trouble with linked list
I am trying to create a linked list that will only allow 10 items in the list and put the numbers in order and have the lowest one removed if one is inputted that is higher then the lowest one. I am having problems constraining it to just 10 and also getting it to put it in numeric order from lowest to highest. Any help will be greatly appreciated. Thanks.
Re: Having trouble with linked list
Post what you have already.
Re: Having trouble with linked list
Here is my attempt for this problem. I found it easier to create my own linked list. I have 3 classes. The node class (LNode), the linked list class(LList) and the main app class (scoreApp).
The LList class contains 2 main functions. The topTenInsert function will call the private orderInsert function and add player/score to list. Then if list is greater than 10 scores it will delete the 11th node keeping the list at 10 entries.
I'm very much a beginner so this code may be flawed. But it seems to work okay. I welcome any comments on how I could approach this problem better or improvement on my coding.
Dele
Code Java:
public class LNode {
public String name;
public int score;
public LNode next;
// Node Constructor
public LNode (String n, int s) {
name = n;
score = s;
next = null;
}
}
Code Java:
public class LList {
LNode head;
LNode tail;
private int size = 0;
// LinkList Constructor
public LList(){
head = null;
tail = null;
}
// Return true if list is empty
private boolean isEmpty() {
return head == null;
}
// Return size of list
private int listSize() {
return size;
}
// Insert data in a new node, and place node in descending order of this list
private void orderInsert(String n, int s) {
// Create new node
LNode node = new LNode(n, s);
// List empty, First node
if(isEmpty()) {
head = node;
tail = node;
}
// New node greater than first node place at start of list
else if (head.score <= node.score){
node.next = head;
head = node;
}
// list already has a node, so place new node in correct position of list
else {
LNode frontPtr = head.next;
LNode backPtr = head;
// Move to correct position
while((frontPtr.score > node.score) && (frontPtr.next != null)) {
backPtr = backPtr.next;
frontPtr = frontPtr.next;
}
// Correct position found on list so insert new node here.
if((frontPtr != null) && (frontPtr.score <= node.score)) {
backPtr.next = node;
node.next = frontPtr;
}
else {
// Insert new node at end of list
frontPtr.next = node;
tail = node;
}
}
size++;
}
// Insert top 10 scores only
public void topTenInsert(String n, int s) {
// ordered insert to list
orderInsert(n, s);
// delete last node if list greater than 10 scores
if (listSize() > 10) {
LNode currentPtr = head;
while(currentPtr.next != tail){
currentPtr = currentPtr.next;
}
// Point tail to second last node(what currentPtr points to)
tail = currentPtr;
currentPtr.next = null;
}
}
// Print list
public void printList() {
// temporary pointer
LNode temp = head;
System.out.println("List Contents: ");
while(temp != null) {
System.out.print(temp.name + " " + temp.score + " ");
temp = temp.next;
}
}
}
[highlight = Java]
public class ScoreApp {
/**
* ScoreApp
* Craig Delehanty
* 25/02/2011
*/
public static void main(String[] args) {
LList game = new LList();
game.topTenInsert("Fred", 67);
game.topTenInsert("Tom", 87);
game.topTenInsert("Mary", 58);
game.topTenInsert("Allen", 73);
game.topTenInsert("Ann", 89);
game.topTenInsert("Karen", 69);
game.topTenInsert("Ted", 35);
game.topTenInsert("Marsha", 62);
game.topTenInsert("Alex", 47);
game.topTenInsert("Corrina", 71);
game.topTenInsert("Jack", 71);
game.topTenInsert("Bruce", 43);
game.topTenInsert("Vera", 89);
game.topTenInsert("Jill", 13);
System.out.println("Top 10 scores:");
game.printList();
}
}
[/highlight]
Re: Having trouble with linked list
A small note. You increase the size every time something is placed in the list. This means that if you reach 10 and then insert a new item, the size will be 11. Then 12. Then 13. So it will not tell the actual size of the linked list after you reach 10 elements.
Re: Having trouble with linked list
The final list size is always 10. The 11th item will be inserted but then the last node is immediately deleted as the list size is greater than 10.
Dele
Re: Having trouble with linked list
I know, but the variable size, which I assume is the one that stores the size of the list, is always increased when something is inserted ;). That, or I am blind, lol. It can be a problem if you want to get the proper size back with the listSize method or if you insert a few million stuff (integer overflow).
Re: Having trouble with linked list
Just for kicks (don't want to dissuade you from doing it this way as learning linked lists such as this are a hugely important concept to learn and fun to use) - an alternative method would be to use a PriorityQueue (Java Platform SE 6) Extend the class to override the add, which you can call super.add, check the size, if its greater than 10 then poll the queue.