If you are not getting how it has done then please just see the previous post of this topic...
Saturday, 10 February 2018
Inserting node at given position of list and print link list data in java
Inserting node at given position of list and print link list data,Inserting node at given position of list and print link list data in java,Inserting node in middle of list and print link list data in java
Add a node after a given node
If you are not getting how it has done then please just see the previous post of this topic...
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...
DashZin
Author & Editor
Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.
February 10, 2018
Data Structures, LinkedList
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment