2 Replies - 2799 Views - Last Post: 18 March 2009 - 03:08 AM Rate Topic: -----

#1 shahab.burki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-February 09

Help Me implement A Linked List in Java

Posted 18 March 2009 - 02:48 AM

Hi,
I am implementing Linked List in Java. I am using three classes for this i.e Data Class, Node Class and Linked List Class. Now the problem is that a searchNode method in my linked List class doesn't work properly. I have tried to debug it it, but can't understand. Please if you can help me.

The code is

class Data {
   
	private String name;  //String which is the Data Part of the Node
   
	public Data(){
		name=null;
	}
	
	public Data(String str){
		name=str;
	}
	//Mutator method for setting the string
	public void setData(String str){
		name=str;
	}
	//Mutator method for getting the string
	public String getData(){
		return (name);
	}
	   
}


/*This is the Node Class Which creates the Nodes*/



public class Node{
	private Data data; //The Data Object to hold data part of the Node
	private Node next; // The Node Object which holds reference to the next Node
   
	//default constructor for Node
	public  Node(){
	   
		next=null;
	}
	
	public Node(Data data, Node next){
		this.data=data;
		this.next=next;
	}
	//Method for setting the Node's Data
	public void setData(Data data)
	{
		this.data=data;
	}
   
	//Method for setting the pointing part of the Node
	public void setNext(Node next){
		this.next=next;
	}
	//Method for getting the Node's Data
	public Data getData(){
		return (data);
	}
	//Method for getting the Nodes' pointing part
	public Node getNext(){
		return (next);
	}
   
	/*public static void main(String args[])
	{
		System.out.println("Hay I am Successful");
	}*/
}


import java.io.*;

public class LinkList{
	private Node firstNode,lastNode;
   
   
	public LinkList(){
		firstNode=null;
		lastNode=null;
	}
   
	public void setLastNode(Node node)
	{
		lastNode=node;
	}
	public void setFirstNode(Node node)
	{
		firstNode=node;
	}
	public Node getFirstNode()
	{
		return (firstNode);
	}
	public Node getLastNode()
	{
		return (lastNode);
	}
   
	public Node createNode(Data data){
		Node newNode=new Node(data,null);
		//newNode.setData(data);
		//newNode.setNext(null);
	   
		return (newNode);
	}
	public boolean emptyList(){
		
		if(this.getFirstNode()==null){
			return true;
		}
		else{
			return false;
		}
  }
   
	public void insertNode(Node node)
	{
		if(this.emptyList())
		{
			this.setFirstNode(node);
			this.setLastNode(node);
		}
	   
		else
		{
			Node temp;
			temp=this.getFirstNode();
		   
			while(temp.getNext()!=null){
				temp=temp.getNext();
			}
			temp.setNext(node);
			this.setLastNode(node);
		   
		}
	}
	
   public String searchNode(String str){
	   
	   String notFound="Not Found";
	   if(this.emptyList()){
		   return (notFound);
	   }
	   else{
		   
		   Node temp=this.getFirstNode();
		   while(temp!=null){
			   		if(temp.getData().getData()==str)
			   			notFound=str;
			   		else
			   			temp=temp.getNext();
			   }
			   
	   }
			  
		return (notFound);   
		  
	   }
		  
		  
 
	  
		
   
		 
	public void showList()
	{
		if(this.emptyList()){
			System.out.println("List is Empty");
		}
	   
		Node temp;
		temp=this.getFirstNode();
	   
		while(temp!=null)
		{
			System.out.println("The Current Node is :"+temp.getData().getData());
			System.out.println();
			temp=temp.getNext();
		}
	   
	}
   
   public static void main(String args[]) throws IOException
	{
		LinkList myList=new LinkList();
		BufferedReader consoleInput=new BufferedReader(new InputStreamReader(System.in));
		Data data=new Data();
		Node newNode=new Node();
		String input;
		System.out.println("Enter Some Names");
		System.out.println("Enter stope to quite");
		
		while ((input = consoleInput.readLine()).equalsIgnoreCase("stop") == false) {
			Data d = new Data();
			d.setData(input);
			myList.insertNode(myList.createNode(d));
		}   
	   
					 // myList.showList();
		
		System.out.println("Enter Some Names");
		BufferedReader searchInput=new BufferedReader(new InputStreamReader(System.in));
		String searchString=searchInput.readLine();
		
	   
		
		System.out.println(myList.searchNode(searchString));
		
		
		
}
}


Thanks

Shahab

Is This A Good Question/Topic? 0
  • +

Replies To: Help Me implement A Linked List in Java

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: Help Me implement A Linked List in Java

Posted 18 March 2009 - 02:53 AM

** Renamed title to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

#3 TyVeryMuch  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 24-March 07

Re: Help Me implement A Linked List in Java

Posted 18 March 2009 - 03:08 AM

I believe this line is wrong:
if(temp.getData().getData()==str)


when comparing Strings in java you should use the equals function, example:
if(temp.getData().getData().equals(str))


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1