Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Having trouble with linked list

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Having trouble with linked list

    Post what you have already.

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


    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;
    	}
    }

    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]

  4. #4
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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.

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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).
    Last edited by Kerr; March 21st, 2011 at 06:17 PM.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default 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.
    Last edited by copeg; March 21st, 2011 at 08:52 PM.

Similar Threads

  1. Help with linked list
    By joecool594 in forum Collections and Generics
    Replies: 3
    Last Post: November 28th, 2010, 11:33 AM
  2. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 02:33 AM
  3. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 10:40 AM
  4. ClassCastException in Double Linked List toString
    By Rastabot in forum Collections and Generics
    Replies: 2
    Last Post: April 24th, 2009, 11:48 AM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM