Results 1 to 2 of 2
  1. #1
    RadicalNH is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Linked list with 3 variables

    Im learning link lists and dont really understand them yet. What im trying to figure out is how I would add another variable to student lets just say "gpa". how would I go about implementing that in the linked list? Im lost right now... Thanks for any help

    Java Code:
    GradeList grades = null;
    		GradeList student = null;
    
    		//link list
    		student = new GradeList("Mike", 92.0);
    		student.setNext(grades);
    		grades = student

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Linked list with 3 variables

    Without seeing the GradeList class, it's impossible to say. I can try to explain linked lists a bit, though.

    Java Code:
            student = new GradeList("Mike", 92.0);     // You've created some GradeList object with values "Mike" and 92.0 in it.... 
            student.setNext(grades);                       // You set the next item in the list to be the grades list... which is null here 
            grades = student                                  // Now you set grades = student, so grades contains "Mike" and 92.0 and points to null.  (Any call that acts on grades.getNext() will end poorly.)
    Typical use of a Linked List:
    Java Code:
    public class Sandbox {
    
    	private static class StringList{
    		private String value;				// value of list item 
    		private StringList next;			// reference to next list item
    		public StringList(String strIn){
    			value = strIn;
    			next = null;
    		}
    		
    		public void setNext(StringList next){
    			this.next = next; 
    		}
    		public StringList getNext(){
    			return next;
    		}
    	}
    	
    	public static void main(String[] args) {	
    		StringList root = new StringList("Hello");	// New StringList with "Hello" value and null pointer to next
    		root.setNext(new StringList("World!"));		// Set the next value to a new StringList with "World!" value (and null pointer to next)
    
    		StringList walk = root;						// This is for iterating through list, always want to keep track of root, or 
    													// you'll not be able to get back to it!
    		do{
    			System.out.println(walk.value);			// Print the string value
    			walk = walk.getNext();					// go to the next list item
    		}while(walk != null);						// we're done, but can still get to the front of the list with root
    	}
    }
    Last edited by SJF; 10-27-2012 at 02:16 AM. Reason: truncated line

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2012, 04:07 AM
  2. 2 variables get linked
    By JavaNoob84 in forum New To Java
    Replies: 4
    Last Post: 11-30-2011, 09:41 AM
  3. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  4. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  5. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •