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

Thread: print linkedlist

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default print linkedlist

    i have no insert method and still its adding node in linkedlist. code below have no error so you can run to see the problem better.

    if u run this it will ask u to enter a command. i only have two commands exit and display. display command will print linklist note it will be empty bc no insert method. but it is adding empty string and int zero. which i dont want.

    i think this is bc of empty constructor. but i dont know how to do it so it wont add any thing. any ideas?



        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.util.Scanner;
     
        public class aaa
        {
        	/*** CREATE NODE ***/
        	private class node
        	{
        		private String name;
        		private int age;
        		private node next;
     
        		/*** Constructor ***/
        		public node()
        		{
        			name = "";
        			age = 0;
        			next = null;
        		}
        		public node(String n, int a)
        		{
        			name = n;
        			age = a;
        			next = null;
        		}/*** End of constructor ***/
        	}/*** End of Node class ***/
     
     
     
     
     
        	/*** Linked List Constructor ***/
        	private node head_node;
        	private node current_node;
     
        	public aaa()
        	{
        		head_node = new node();
        		current_node = new node();
        	}
     
     
        	/*** Insert Method ***/
        	public void insert(String name, int age)
        	{
     
        	}/*** End of Insert Method ***/
     
     
     
        	/*** Delete Method ***/
     
        	/*** Display Method ***/
        	public void display()
        	{
        		current_node = head_node;
        		System.out.println("name\tage");
        		while(current_node != null)
        		{
        			System.out.println(current_node.name+"\t"+current_node.age);
        			current_node = current_node.next;
     
        		}
        	}/*** End of Display Method/
     
     
     
        	/*** Main Method ***/
        	public static void main(String[] args) throws IOException
        	{
        		aaa m = new aaa();
     
        		String cmd = "";
        		int age = 0;
        		String name = "";
     
        		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        		Scanner sc = new Scanner(System.in);
     
        		System.out.print("Enter a Command: ");
        		cmd = br.readLine();
        		while(!(cmd.equals("exit")))
        		{
        			if(cmd.equals("insert"))
        			{
     
        			}
        			else if(cmd.equals("display"))
        			{
        				m.display();
        			}
        			System.out.print("Enter a Command: ");
        			cmd = br.readLine();
        		}
        	}
        }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: print linkedlist

    If your nodes are supposed to represent real people then get rid of the no argument constructor. It can't really serve any purpose as the people will have names and ages.

    In your display() method you have a loop like this:

    while(current_node != null)
    {
        System.out.println(current_node.name+"\t"+current_node.age);
        current_node = current_node.next;
    }

    Basically this prints name and age and then moves to the next node. It keeps doing this until it hits null when it stops. null is serving a very important role here: it is marking the end of the list.

    When you create a linked list instance it does not have any data in it. In that case it would make sense for the head_node to be null. If you make it anything else you are putting phoney data into the list. So the assignment to head_node in the aaa() constructor can be removed.

    The current_node instance variable can be removed altogether. The only place you use a current node is in the display() method, and it might as well be a local variable that you declare in that method. It is a very good idea for variables to have the smallest scope possible: so if something is only used to keep track of a value within a method make it a local variable of that method and keep instance variables for those cases where you really intend multiple methods and constructors to access the same value.

    ---

    Your code should follow standard Java conventions. Begin class names with a Capital letter, and make them descriptive. Use camelCase for variables and methods, again choosing names that are descriptive and self documenting.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: print linkedlist

    ah i c. thanks i got insert method working . i had a question here.
    my delete from middle is not seem to be working. i think the loop ever stop and that the problem. but iam not sure what iam doing wrong here.


    name = n


    if(...)  //if head is null
    {
    ...   
    }
    else
    {
    /*** delete from begining ***/
    if(headNode.name.equals(n))
    	{
    		head_node = head_node.next; 
    	}		
     
    /*** delete from middle ***/
    while(currentNode != null && currentNode.next != null)
    	{
    		if(currentNode.next.name.equals(n))
    		{
    			delNode = currentNode.next;
    			currentNode.next = currentNode.next.next;
    			delNode = null;
    		}
    	}//End of while loop
    }//End of else statment

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: print linkedlist

    It's hard to tell without real code.

    Post the code, what you see when you run it, and what you expect to see.

Similar Threads

  1. [SOLVED] linkedlist concurrentmodificationexception
    By dEvilKinG in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2013, 12:20 PM
  2. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  3. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM
  4. addInOrder LinkedList
    By PeskyToaster in forum Collections and Generics
    Replies: 1
    Last Post: April 6th, 2012, 06:16 AM
  5. LinkedList of String
    By oxnume in forum Collections and Generics
    Replies: 3
    Last Post: April 6th, 2012, 12:12 AM