Tuesday, September 8, 2009

Java Data Structure, Java LinkList

public class Node {
protected Object obj;
protected Node node;
}


public class SingleLinkList {
Node linkList;
int count;
public SingleLinkList() {
linkList = null;
count = 0;
}
public void insert(Object obj){
if(linkList == null){
linkList = new Node();
linkList.obj = obj;
linkList.node = null;
}else{
Node temp = new Node();
temp.obj = obj;
temp.node = null;
Node last = getLastNode();
last.node = temp;
}
}
public void insertFirst(Object obj){
if(linkList == null){
linkList = new Node();
linkList.obj = obj;
linkList.node = null;
}
else{
Node temp = new Node();
temp.obj = obj;
temp.node = linkList;
linkList = temp;
}
}
public void removeFirst(){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}
else{
System.out.println("Deleting from First :: "+linkList.obj.toString());
linkList = linkList.node;
}
}
public void remove(Object obj){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
if(linkList.obj == obj){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}else{
System.out.println("NOT FOUND");
}
}
else{
Node temp = new Node();
temp = linkList;
Node pre = temp;
while(temp.node!= null&&temp.obj!=obj){
pre = temp;
temp = temp.node;
}
if(temp.obj==obj){
System.out.println("Deleting data :: "+temp.obj.toString());
if(pre==temp){
linkList=temp.node;
}
else{
pre.node = temp.node;
}
}
else{
System.out.println("NOT FOUND");
}
}
}
public void remove(){
if(linkList == null){
System.out.println("Link List does not exist");
}else if(linkList.node == null){
System.out.println("Deleting data :: "+linkList.obj.toString());
linkList = null;
}
else{
// Get node before last node
Node temp = new Node();
temp = linkList;
while(temp.node.node!=null){
temp = temp.node;
}
System.out.println("Deleting data :: "+temp.node.obj.toString());
temp.node = null;
}
}
public Node getLastNode(){
Node temp = new Node();
temp = linkList;
while(temp.node!=null){
temp = temp.node;
}
return temp;
}
public void show(){
Node temp = new Node();
temp = linkList;
while(temp!=null){
System.out.println("Data :: "+temp.obj.toString());
temp = temp.node;
}
}
public static void main(String[] args) {
SingleLinkList singleLinkList = new SingleLinkList();
String str="Binod";
singleLinkList.insert(str);
String str1="MCA";
singleLinkList.insert(str1);
str1="SATYAM";
singleLinkList.insert(str1);
str1="Bangalore";
singleLinkList.insert(str1);
str1="India";
singleLinkList.insertFirst(str1);
str1="SATYAM2";
singleLinkList.insert(str1);
str1="USA";
singleLinkList.insertFirst(str1);
singleLinkList.show();
// singleLinkList.remove();
/*singleLinkList.remove();
singleLinkList.remove();*/
// singleLinkList.removeFirst();
// singleLinkList.show();
singleLinkList.remove("SATYAM2");
singleLinkList.show();
}
}

No comments:

Post a Comment

Please put your feedback or your any question related to this blog. Thanks.