Simple LinkedList in Java

public class SimpleLinkedListImpl implements SimpleLinkedList {
private Node head;
private Node current;

@Override
public void add(String obj) {
if(head==null)
{
head=new Node(obj);
current=head;

}
else
{
Node temp=new Node(obj);
current.setNext(temp);
current=temp;
}
// TODO Auto-generated method stub

}

@Override
public String get(String obj) {
// TODO Auto-generated method stub
Node tmp = head;
while(tmp != null)
{
System.out.println(” tmp data = ” + tmp.getData());
if(tmp.getData().equalsIgnoreCase(obj))
return tmp.getData();

tmp = tmp.getNext();
}
return null;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

SCAELinkedList list = new SCAELinkedListImpl();
list.add(“John”);
list.add(“Harry”);
list.add(“Henry”);
String test = “Henry”;
String result = list.get(test);
System.out.println(” result = ” + result);
if(result!= null && result.equalsIgnoreCase(test))
{
System.out.println(” Found ” + test + ” ! in the list”);
}
else
{
System.out.println(” No ” + test + ” in the list”);
}
}

}

class Node{

String data;
Node next;

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

public Node getNext() {
return next;
}

public void setNext(Node next) {
this.next = next;
}

public Node(String data){
this.data = data;

}
}

Advertisements

Leave a comment

Filed under Java, java_tutorial

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s