Results 1 to 6 of 6

Thread: Problem using Linked list in java

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Problem using Linked list in java

    Hello,
    I need help because I get errors that I can not fix. Being more precise i have a problem in the Linked List, I am trying it but I can not fix
    Here is my code.
    Code:
    import java.util .*;
     
        Public class LinkedList{
    	private Link hd = null;
    	
    	class Link{
    	    private Link nxt;
    	    private String ele;
    	    
    	    Public Link(String ele, nxt Link){
    		this.ele = ele;
    		this.nxt = nxt;
    	    }
    	}
    	
    	Public void add(String text){
    	    hd = new Link(text, hd);
    	}
    	
    	Public Intl size(){
    	    Intl size = 0;
    	    while(hd! = null){
    		size + +;
    hd = hd.nxt;
    	    }
    	}
    	
    	Public String toString(){
    	    return ele + " " + nxt.function toString() {
        [native code]
    }();
    	}
        }

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Problem using Linked list in java

    Hello,
    Link, a class is an internal class and an attribute of your class LinkedList, therefore you access them with
    Code:
    LinkedList.list
    I think you are not clear with the concept of the basic of java. I recommend you to read the basic tutorial or documentation on java , then try to write programs.

  3. #3
    Join Date
    Dec 2009
    Posts
    211

    Re: Problem using Linked list in java

    Hey
    I have modified your code, see if it works now
    Code:
    import java.util .*;
     
        Public class LinkedList{
    	private Link hd = null;
    	
    	class Link{
    	    private Link nxt;
    	    private String ele;
    	    
    	    Public Link(String ele, nxt Link){
    		this.ele = ele;
    		this.nxt = nxt;
    	    }
    	}
    	
    	Public void add(String text){
    	    hd = new Link(text, hd);
    	}
    	
    	Public Intl size(){
    	    Intl size = 0;
    	    while(hd! = null){
    		size + +;
    hd = hd.nxt;
    	    }
    	    return size;
    	}
    	
    	Public String toString(){
    	    StringBuilder sb = new StringBuilder();
    sb.append("eles of the list:);
    	    while(hd! = null){
    		sb.append(hd.ele).append(" ");
    hd = hd.nxt;
    	    }
    	    return sb.function toString() {
        [native code]
    }();
    	}
        }

  4. #4
    Join Date
    Nov 2009
    Posts
    330

    Re: Problem using Linked list in java

    Hello,
    I advise you to make separate classes for Link and LinkedList is better for Conception. And then where is the method which displays your list and its size? The above post which is posted contains a code, see if the that works first and if not then post back with the error. But before then you have to try by your self first.

  5. #5
    Join Date
    Nov 2009
    Posts
    359

    Re: Problem using Linked list in java

    Hello,
    Head represents an instance variable, change your method as follows
    Code:
    Public Intl sz(){
           Intl sz = 0;
           Link lk = head;
           while(lk! = null){
          sz + +;
           lk = lk.next;
           }
           return sz;
       }
    But do not just copy / paste, try to understand why your original method did not work. If you have other questions to which you can not find a response mature reflection , Post them on the forum.

  6. #6
    Join Date
    Dec 2009
    Posts
    213

    Re: Problem using Linked list in java

    Hello,
    This is my modified code, if you have any idea about it then please do post back , even this is not working
    Code:
    Public class Link{
        private Link nxt;
        private Final String ele;
        
        Public Link(String ele, nxt Link){
    	this.ele = ele;
    	this.nxt = nxt;
        }
        
        Public Link getnxt(){
    	return nxt;
        }
        
        Public String getele(){
    	return ele;
        }
    }

Similar Threads

  1. Problem with Java linked list
    By XeroX in forum Software Development
    Replies: 1
    Last Post: 13-06-2012, 03:37 PM
  2. <Identifier> Expected Error Circular Linked List Java
    By JGriff254 in forum Software Development
    Replies: 1
    Last Post: 23-03-2010, 02:25 PM
  3. Dynamic Linked List In Jsp
    By rashmi_ay in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 10:50 PM
  4. Better way to sort a linked list in c++
    By Juany in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 04:30 PM
  5. What is a linked list?
    By Migueel in forum Software Development
    Replies: 5
    Last Post: 28-11-2009, 11:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,207,858.95636 seconds with 17 queries