Saturday, June 9, 2018

Linked List creation in Java from scratch

1. Simple linked list creation statically:

class node{
    int data;
    node next;

    node(int item){
        data = item;
        next = null;
    }


}

class linked_list{

    node create_list()
    {
        node head = new node(2);
        head.next = new node(3);
        head.next.next = new node(4);
        head.next.next.next = new node(5);
        return head;

    }


    void get_list(node head)
    {
        node current = head;
        while(current != null)
        {
            System.out.println(current.data);
            current = current.next;
        }

    }

}

public class list {


    public static void main(String[] args)
    {

        linked_list start = new linked_list();
        node head = start.create_list();
        start.get_list(head);
    }


}

2. Simple within the same class:

class list
{
    int data;
    list next;
    list(int date)
    {
        data = date;
        next = null;
    }
}
public class graph {
    void add_data(list root,int item)
    {
        list current = root;
        while(current.next != null)
        {
            current = current.next;

        }
        current.next = new list(item);

    }

    void get_data(list root)
    {
        list current = root;
        while(current != null)
        {
            System.out.println(current.data);
            current = current.next;
        }
    }



    public static void main(String[] args)
    {
        list root = new list(5);

        graph obj = new graph();
        obj.add_data(root,3);
        obj.add_data(root,4);
        obj.add_data(root,7);

        obj.get_data(root);
    }
}





2.With recursive insert:

Note to always store head in an object so that each time it doesnot change. Otherwise we need to
keep track of head and end seperately.

class node{
    int data;
    node next;

    node(int item){
        data = item;
        next = null;
    }

    public int getData() {
        return data;
    }

    public node getNext() {
        return next;
    }

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

    public void setNext(node next) {
        this.next = next;
    }
    public void insert_number(int data)
    {
        if(next == null){
            next = new node(data);

        }

        else{
            next.insert_number(data);
        }
    }
}

class linked_list{
    node head;

    linked_list(){
        head = null;
    }
    

    void insert_node(int data){
        if(head == null)
        {
            head = new node(data);
        }
        else{
            head.insert_number(data);
        }
/////or even can be done without recursion
//   note the n.next should be used.
/// public void insert_number(int data)
//{
//       node end = new node(data);
//  
      node n = this;//      
  while(n.next!= null)
//       {
//            n = n.next;
//        }
//        n.next = end;
//    }


}
    void get_list()
    {
        node current = head;
        while(current != null)
        {
            System.out.println(current.data);
            current = current.next;
        }

    }

}

public class list {


    public static void main(String[] args)
    {

        linked_list start = new linked_list();


        start.insert_node(2);
        start.insert_node(3);
        start.insert_node(4);
        start.insert_node(5);
        start.insert_node(6);
        start.insert_node(7);

        start.get_list();
    }





}




3: To keep track of head and end seperately:

class node{
    int data;
    node next;

    node(int item){
        data = item;
        next = null;
    }

    public int getData() {
        return data;
    }

    public node getNext() {
        return next;
    }

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

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

}

class linked_list{
    node head;
    node end;

    linked_list(){
        head = null;
        end = head;
    }


    void insert_node(int data){
        if(head == null)
        {
            head = new node(data);
            end = head;
        }
        else{
            node nptr = new node(data);
            end.setNext(nptr);
            end = nptr;

        }


    }
    void get_list()
    {
        node current = head;
        while(current != null)
        {
            System.out.println(current.data);
            current = current.next;
        }

    }

}

public class list {


    public static void main(String[] args)
    {

        linked_list start = new linked_list();


        start.insert_node(2);
        start.insert_node(3);
        start.insert_node(4);
        start.insert_node(5);
        start.insert_node(6);
        start.insert_node(7);

        start.get_list();
    }





}



So overall with just only one class node is 

class node{
    int data;
    node next;

    node(int item){
        data = item;
        next = null;
    }

   
    public void insert_number(int data)
    {
       node end = new node(data);
        node n = this;
        while(n.next!= null)
        {
            n = n.next;
        }
        n.next = end;
    }

    void get_list()
    {
        node current = this;
        while(current != null)
        {
            System.out.println(current.data);
            current = current.next;
        }

    }
}



public class list {


    public static void main(String[] args)
    {

       node start = new node(1);


        start.insert_number(2);
        start.insert_number(3);
        start.insert_number(4);
        start.insert_number(5);
        start.insert_number(6);
        start.insert_number(7);

        start.get_list();
    }





}

No comments:

Post a Comment