Showing posts with label LinkedList. Show all posts
Showing posts with label LinkedList. Show all posts

Saturday, 10 February 2018

create a link list in java and link all the node to each other

DashZin
Create a link list in java:-

In this tutorial we are going to create LinkedList using java and link all the node to each other but in this code it will not print anything..

package com.dashzin.linklist;

public class LinkList {
     Node head;// head of list

    /* Linked list Node...And This inner class is made static so that
       main() can access it */
     static class Node{
          int data;
          Node next;
         
          Node(int data){
              this.data=data;
              next=null;
          }
     }

     public static void main(String args[]){
    
          // creating four node for linklist
          LinkList lkl=new LinkList();
          lkl.head=new Node(1);
         
          Node first=new Node(2);
          Node sec=new Node(3);
          Node third=new Node(4);
         
          //Link all the node with each other.....
         
          lkl.head.next=first;
          first.next=sec;
          sec.next=third;
     }
}

 If you are not getting how it has done then please just see the previous post of this topic... 






Inserting node at given position of list and print link list data in java

DashZin
Add a node after a given node



package com.dashzin.linklist;

public class AddMiddle {
     Node head;
 
     static class Node{
     int data;
     Node next;
     Node(int data){
          this.data=data;
          next=null;
     }
     }
/* Inserts a new Node at given position of the list. */
public void middle(int new_data, Node prev_node){
     if(prev_node==null){
          System.out.println("previous node con not be empty");
          return;
     }
     // Allocate the Node & Put the data
     Node new_node=new Node(new_data);
     new_node.next=prev_node.next; //Make next of new Node as next of prev_node
    prev_node.next=new_node; //make next of prev_node as new_node
}

/* This method will prints data of linked list starting from head */
public void printList(){
     while(head!=null){
          System.out.print(head.data+" ");
          head=head.next;
     }
}

     public static void main(String args[]){
    
          // creating four node for linklist
          AddMiddle lkl=new AddMiddle();
          lkl.head=new Node(1);
         
          Node first=new Node(2);
          Node sec=new Node(3);
          Node third=new Node(4);
         
          //Link all the node with each other.....
         
          lkl.head.next=first;
          first.next=sec;
          sec.next=third;
         
          // calling middle method to insert node at given position..
          lkl.middle(10, lkl.head.next); /*change the node value means(first.next) to
                                      *change the position of data to insert in list.*/
         
          //calling method to print_list.....
          lkl.printList();

}
}


 If you are not getting how it has done then please just see the previous post of this topic... 








Inserting node at beginning of list and print link list data in java

DashZin
Add a node at the beginning

The new node is always added at the beginning node of the given Linked List. For example if the given Linked List is 50->100->105->220->250 and we add an item 330 at the beginning, then the Linked List becomes 330->50->100->105->220->250



package com.dashzin.linklist;
public class AddFirst {
     Node head;
    
     static class Node{
          int data;
          Node next;
     Node(int data){
          this.data=data;
          next=null;
     }
     }
 /* This method will prints data of linked list starting from head */
     public void printList(){
          while(head!=null){
              System.out.print(head.data+" ");
              head=head.next;
          }
     }
    
     /* Inserts a new Node at front of the list. */
     public void insert(int new_data){
          Node new_node=new Node(new_data); // Allocate the Node & Put the data
          new_node.next=head; //Make next of new Node as head
          head=new_node// Move the head to point to new Node
     }
    
     public static void main(String args[]){
         
          // creating four node for linklist
          AddFirst lkl=new AddFirst();
          lkl.head=new Node(1);
         
          Node first=new Node(2);
          Node sec=new Node(3);
          Node third=new Node(4);
         
          //Link all the node with each other.....
         
          lkl.head.next=first;
          first.next=sec;
          sec.next=third;
         
          // calling insert method to insert node at beginning position..
          lkl.insert(5);
         
          //calling method to print_list.....
          lkl.printList();
         
     }
}


 If you are not getting how it has done then please just see the previous post of this topic...