Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Problem using Linked list in java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 18-02-2010
Member
 
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]
}();
	}
    }

Reply With Quote
  #2  
Old 18-02-2010
Member
 
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.
Reply With Quote
  #3  
Old 18-02-2010
Member
 
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]
}();
	}
    }
Reply With Quote
  #4  
Old 18-02-2010
Member
 
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.
Reply With Quote
  #5  
Old 18-02-2010
Member
 
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.
Reply With Quote
  #6  
Old 18-02-2010
Member
 
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;
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem using Linked list in java"
Thread Thread Starter Forum Replies Last Post
Problem with Java linked list XeroX Software Development 1 13-06-2012 03:37 PM
<Identifier> Expected Error Circular Linked List Java JGriff254 Software Development 1 23-03-2010 02:25 PM
Dynamic Linked List In Jsp rashmi_ay Software Development 5 23-02-2010 10:50 PM
Better way to sort a linked list in c++ Juany Software Development 5 13-02-2010 04:30 PM
What is a linked list? Migueel Software Development 5 28-11-2009 11:03 PM


All times are GMT +5.5. The time now is 10:58 AM.