Java implementation of the Linked List is given below.
package lab.linkedlist;
public class LinkedList
{
public ListNode headnode;
public ListNode currentNode;
public ListNode lastnode;
public LinkedList()
{
headnode=null;
currentNode=null;
lastnode=null;
}
public void insertBegining(Object info)
{
ListNode tmpnode=new ListNode();
tmpnode.data=info;
if(headnode==null)
{
tmpnode.nextnode=null;
lastnode=tmpnode;
}
else
{
tmpnode.nextnode=headnode;
}
headnode=tmpnode;
}
public Object getFirstinList()
{
if (headnode!=null)
{
return headnode.data;
}
else
{
return null;
}
}
public void deleteFirstInList()
{
if(headnode!=null)
{
headnode=headnode.nextnode;
}
else
{
return;
}
}
public Object getNextNode()
{
ListNode temp=currentNode;
if(currentNode==null)
{return null;}
else{
currentNode=currentNode.nextnode;
return temp.data ;
}
}
public void startIterator()
{
currentNode=headnode;
}
public void insertEnd(Object info)
{
ListNode tmpnode=new ListNode();
tmpnode.data=info;
if(headnode==null)
{
headnode=tmpnode;
}
else
{
lastnode.nextnode=tmpnode;
}
tmpnode.nextnode=null;
lastnode=tmpnode;
}
}
package lab.general;
public class Node {
public Object data;
public Node(Object dat){
data=dat;
}
public Node()
{
data=null;
}
}
package lab.linkedlist;
public class LinkedList
{
public ListNode headnode;
public ListNode currentNode;
public ListNode lastnode;
public LinkedList()
{
headnode=null;
currentNode=null;
lastnode=null;
}
public void insertBegining(Object info)
{
ListNode tmpnode=new ListNode();
tmpnode.data=info;
if(headnode==null)
{
tmpnode.nextnode=null;
lastnode=tmpnode;
}
else
{
tmpnode.nextnode=headnode;
}
headnode=tmpnode;
}
public Object getFirstinList()
{
if (headnode!=null)
{
return headnode.data;
}
else
{
return null;
}
}
public void deleteFirstInList()
{
if(headnode!=null)
{
headnode=headnode.nextnode;
}
else
{
return;
}
}
public Object getNextNode()
{
ListNode temp=currentNode;
if(currentNode==null)
{return null;}
else{
currentNode=currentNode.nextnode;
return temp.data ;
}
}
public void startIterator()
{
currentNode=headnode;
}
public void insertEnd(Object info)
{
ListNode tmpnode=new ListNode();
tmpnode.data=info;
if(headnode==null)
{
headnode=tmpnode;
}
else
{
lastnode.nextnode=tmpnode;
}
tmpnode.nextnode=null;
lastnode=tmpnode;
}
}
package lab.general;
public class Node {
public Object data;
public Node(Object dat){
data=dat;
}
public Node()
{
data=null;
}
}
package lab.linkedlist;
import lab.general.Node;
public class ListNode extends Node {
ListNode nextnode;
}
No comments:
Post a Comment