top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

program to implement linkedlist using java

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

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social 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
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...