top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

program to implement linkedlist using java

+3 votes
350 views
program to implement linkedlist using java
posted Sep 22, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Assuming that you want single linklist -

public class Node
{
    public Object data; //the data stored in this node
    public Node next; //store a reference to the next node in this singlylinkedlist
    public Node(Object data,Node next){
        this.data =data;
        this.next =next;
    }
}

public class SinglyLinkeList
{
    Node start;
    public SinnglyLinkedList()
    {
      this.start=null;
    }

    public void addFront(Object newData)
    {
        Node cache = this.start; //store a reference to the current start node
        this.start = new Node(newData,cache); //assign our start to a new node that has newData and points to our old start
    }
    public addRear(Object newData)
    {
        Node cache = start; 
        Node current = null;

        while((current = cache.next) != null) //find the last Node
            cache = cache.next;

        cache.next = new Node(newData,null); //create a new node that has newData and points to null
    }

    public Object getFront()
    {
        return this.start.data; // return the front object's data
    }
}
answer Sep 22, 2013 by Deepankar Dubey
...