0 votes
437 views
in Data Structure And Algorithm by
How to linked list insert at position java?

2 Answers

0 votes
by (3.7k points)
See the following running example to insert data at given posion in linked list:-

package com.ds.linkedlist;

public class Node<T> {

    private T data;
    private Node<T> next;
   
    public Node(){
        setData(null);
        setNext(null);
    }
   
    public Node(T data){
        setData(data);
        setNext(null);
    }
   
    public T getData(){
        return data;
    }
   
    public void setData(T data){
        this.data = data;
    }
   
    public Node<T> getNext(){
        return next;
    }
   
    public void setNext(Node<T> next){
        this.next = next;
    }
   
}

package com.ds.linkedlist;

import java.util.HashMap;

public class SingleLinkedList<T> {
   
    Node<T> head = null;
    public void addFirst(T data){
        if(data == null){
            return;
        }
        if(head == null){
            head = new Node<T>(data);
        }else{
            Node<T> newNode = new Node<T>(data);
            newNode.setNext(head);
            head = newNode;
        }
    }
    public void addAfter(T after, T data){
        if(after == null || data == null){
            return;
        }
        if(head == null){
            addFirst(data);
        }else{
            Node<T> tempNode = head;
            while(tempNode != null){
                if(tempNode.getData() == after){
                    Node<T> newNode = new Node<T>(data);
                    Node<T> tmp = tempNode.getNext();
                    newNode.setNext(tmp);
                    tempNode.setNext(newNode);
                    break;
                }
                tempNode = tempNode.getNext();
            }
        }
       
    }
   
    public void addAfterOnPosition(int position, T data){
        if(data == null){
            return;
        }
        if(head == null){
            addFirst(data);
        }else{
            Node<T> tempNode = head;
            int index=1;
            while(tempNode != null){
                if(index == position){
                    Node<T> newNode = new Node<T>(data);
                    Node<T> tmp = tempNode.getNext();
                    newNode.setNext(tmp);
                    tempNode.setNext(newNode);
                    break;
                }
                index++;
                tempNode = tempNode.getNext();
            }
        }
       
    }
    public void getNodeData(){
        Node<T> tempNode = head;
        if(tempNode==null){
            return;
        }
        while(tempNode != null){
            System.out.print(tempNode.getData()+" ");
            tempNode = tempNode.getNext();
        }
    }
}

package com.ds.linkedlist;

public class LinkedListTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SingleLinkedList<Integer> singleLinkedList = new SingleLinkedList<Integer>();
        singleLinkedList.addFirst(10);
        singleLinkedList.addFirst(20);
        singleLinkedList.addFirst(30);
        singleLinkedList.addFirst(30);
        singleLinkedList.addFirst(40);
        System.out.println("Node List:-");
        singleLinkedList.getNodeData();
        singleLinkedList.addAfterOnPosition(2, 5);
        System.out.println("After adding data on specified position");
        singleLinkedList.getNodeData();
        singleLinkedList.addAfterOnPosition(1, 55);
        System.out.println("After adding data on specified position");
        singleLinkedList.getNodeData();
        singleLinkedList.addAfter(20, 100);
        System.out.println("After adding data on specified node");
        singleLinkedList.getNodeData();
    }
}

Output:-
Node List:-
40 30 30 20 10
After adding data on specified position
40 30 5 30 20 10
After adding data on specified position
40 55 30 5 30 20 10
After adding data on specified node
40 55 30 5 30 20 100 10
0 votes
by (2.8k points)
See the following code to insert data into linked list at the given position:-

public Node insertAt(int data, int position) {

Node newNode = new Node(data);

if(position > count) {

System.out.println("The given position is out of range");

return head;

}

if(head == null) {

head = newNode;

count++;

return head;

}

Node current = head;

Node prev = head;

while(current != null) {

if(position==1) {

newNode.next = current;

prev.next = newNode;

count++;

break;

}

position--;

prev = current;

current = current.next;

}

return head;

}

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...