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 5 of 5

Thread: linked lists

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default linked lists

    import java.io.*;
     
    public class LinkedList 
    {
    	private class node
    	{
    		int number;
    		node next;
    	}
     
    	private node current = new node();
     
    	LinkedList()
    	{
    		current=null;
    	}
     
    	public void insert(int n)
    	{
    		if(current != null)
    		{
    			while(current != null)
    			{
    			current = current.next;
    			}
    			current = new node();
    			current.number = n;
    			current.next = null;
    		}
    		else
    		{
    		current = new node();
    		current.number = n;
    		current.next = null;
    		}
    	}
     
    	public void PrintList()
    	{
    		int count = 0;
    		while(current != null)
    		{
    			System.out.println(current.number);
    			current=current.next;
    			count++;
    			System.out.println(count);
    		}
    	}
     
     
    	public static void main(String[] args)
    	{
    		LinkedList myList=new LinkedList();
     
    		for(int i=0;i<10;i++)
    		{
    		myList.insert(i);
    		}
     
    		myList.PrintList();
     
    	}
     
    }

    This compiles but it only runs once it seems. The program outputs 9 which is the end of the loop in main and 1 which counts how many times the while loop happens in the print method. Therefore I must be overriding the same memory location in my insert method, but why?

    --- Update ---

    I changed a few things around but still the same result:

    import java.io.*;
     
    public class LinkedList 
    {
    	private class node
    	{
    		int number;
    		node next;
    	}
     
    	private node current = new node();
     
    	LinkedList()
    	{
    		current=null;
    	}
     
    	public void insert(int n)
    	{
    		if(current==null)
    		{
    			current = new node();
    			current.number = n;
    			current.next = null;
    		}
    		else
    		{
    		while(current != null)
    		{
    			current = current.next;
    		}
    		current = new node();
    		current.number = n;
    		current.next = null;
    		}
    	}
     
    	public void PrintList()
    	{
    		int count = 0;
    		while(current != null)
    		{
    			System.out.println(current.number);
    			current=current.next;
    			count++;
    			System.out.println(count);
    		}
    	}
     
    	public static void main(String[] args)
    	{
    		LinkedList myList=new LinkedList();
     
    		for(int i=0;i<10;i++)
    		{
    		myList.insert(i);
    		}
     
    		myList.PrintList();
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: linked lists

    Take a price of paper and draw out the LinkedList when it is empty and then step through the logic to add a node and then add another node.

    Time to try some debugging. For testing only add 3 items to the list. Add println statements to all the methods that are called and print out all the values for all the variables used.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    GregBrannon (April 25th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: linked lists

    Java's naming convention is that class names begin with capital letters, method and variable names begin with lowercase letters. Please follow Java's naming conventions.

  5. #4
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: linked lists

    Will do Greg and thanks norm I got it figured out.

    I am about to start programming depth and breath first search for fun but I was wondering is there a library in Java for those data structures?

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: linked lists

    I got it figured out
    Please mark this thread as solved.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Generalized Linked Lists
    By CrazyCoder in forum Java Theory & Questions
    Replies: 1
    Last Post: January 22nd, 2014, 09:34 PM
  2. Linked Lists and Nodes
    By tomlisi92 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 22nd, 2013, 05:07 AM
  3. [SOLVED] Linked Lists
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 08:39 AM
  4. Linked Lists
    By Jnoobs in forum Java Theory & Questions
    Replies: 1
    Last Post: October 23rd, 2010, 04:09 PM
  5. Problems in linked lists
    By Hotzero in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2010, 09:25 AM