Answers for "java linked list implementation"

3

Linked List implementation

public class LinkedList {

    private Node head;
    private int length = 0;

    public LinkedList() {
        this.head = new Node(null);
    }

    public int size() {
        return length;
    }


     // Adds an element to the end of the list
    public void add(Object data)  {

        Node node = new Node(data);
        Node iterator = head;
        while (iterator.getNext() != null){
            iterator = iterator.getNext();
        }
        iterator.setNext(node);
        length++;
    }


     // Obtains an element by index
    public Object get(int index) {

        if (head.getNext() == null || index >= length){
            return null;
        }

        Node iterator = head.getNext();
        int counter = 0;

        while(counter < index){

            iterator = iterator.getNext();
            counter++;
        }
        return iterator.getData();

    }


     // Returns the index of the element in the list
    public int indexOf(Object data) {
        Node obj=head;
        for (int i = 0; i < length; i++) {
            obj = obj.getNext();
            if (obj.getData().equals(data)) {
                return i;
            }
        }
        return -1;
        //throw new Exception("Data not found");
    }


     // Removes an element from the list
    public boolean remove(Object data) {

        if (head.getNext() == null){
            return false;
        }

        Node iterator = head;

        while(iterator.getNext() != null){

            if (iterator.getNext().getData().equals(data)){
                iterator.setNext(iterator.getNext().getNext());
                length--;
                return true;
            }

            iterator = iterator.getNext();
        }

        return false;
    }

    private class Node {

        private Object data;
        private Node next;

        public Node(Object data) {
            this.data = data;
            next = null;
        }

        public Object getData() {
            return data;
        }

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

        public Node getNext() {
            return next;
        }

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

}
Posted by: Guest on June-08-2020
19

java linked list functions

import java.util.LinkedList;
LinkedList<Integer> myList = new LinkedList<Integer>();
myList.add(0);
myList.remove(0);//Remove at index 0
myList.size();
myList.get(0);//Return element at index 0
Posted by: Guest on June-24-2020
3

Linked List implementation

public class LinkedList {

    private Node head;
    private int length = 0;

    public LinkedList() {
        this.head = new Node(null);
    }

    public int size() {
        return length;
    }


     // Adds an element to the end of the list
    public void add(Object data)  {

        Node node = new Node(data);
        Node iterator = head;
        while (iterator.getNext() != null){
            iterator = iterator.getNext();
        }
        iterator.setNext(node);
        length++;
    }


     // Obtains an element by index
    public Object get(int index) {

        if (head.getNext() == null || index >= length){
            return null;
        }

        Node iterator = head.getNext();
        int counter = 0;

        while(counter < index){

            iterator = iterator.getNext();
            counter++;
        }
        return iterator.getData();

    }


     // Returns the index of the element in the list
    public int indexOf(Object data) {
        Node obj=head;
        for (int i = 0; i < length; i++) {
            obj = obj.getNext();
            if (obj.getData().equals(data)) {
                return i;
            }
        }
        return -1;
        //throw new Exception("Data not found");
    }


     // Removes an element from the list
    public boolean remove(Object data) {

        if (head.getNext() == null){
            return false;
        }

        Node iterator = head;

        while(iterator.getNext() != null){

            if (iterator.getNext().getData().equals(data)){
                iterator.setNext(iterator.getNext().getNext());
                length--;
                return true;
            }

            iterator = iterator.getNext();
        }

        return false;
    }

    private class Node {

        private Object data;
        private Node next;

        public Node(Object data) {
            this.data = data;
            next = null;
        }

        public Object getData() {
            return data;
        }

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

        public Node getNext() {
            return next;
        }

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

}
Posted by: Guest on June-08-2020
1

java linked list

//Insert, Show Nodes - java-LinkedList
public class Node {
    int data;
    Node next;
    
  	//main
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insert(9);
        list.insert(10);
        list.insert(15);
        list.insert(19);
        list.show();
    }
}
//
class LinkedList {
    Node head;
    //insert a node - method
    public void insert(int data) {
        Node node = new Node();
        node.data = data;
        node.next = null;

        //check if node is single
        if (head == null) {
            head = node;
        } else {
          	// go to the end of linkedList
            Node n = head;
            while (n.next != null) {
                n = n.next;
            }
            //set the last node to be node
            n.next = node;
        }
    }
    //print the list of nodes
    public void show() {
        Node node = head;
        //iterate and print
        while (node.next != null) {
            System.out.println(node.data);
            node = node.next;
        }
        //print the last one that the node.next == null;
        System.out.println(node.data);
    }
}
Posted by: Guest on October-20-2021
19

java linked list functions

import java.util.LinkedList;
LinkedList<Integer> myList = new LinkedList<Integer>();
myList.add(0);
myList.remove(0);//Remove at index 0
myList.size();
myList.get(0);//Return element at index 0
Posted by: Guest on June-24-2020
0

linked list in java

// Java program to iterate the elements  
// in an LinkedList 
    
import java.util.*;  
    
public class GFG {  
    
    public static void main(String args[])  
    {  
        LinkedList<String> ll  
            = new LinkedList<>();  
    
        ll.add("Geeks");  
        ll.add("Geeks");  
        ll.add(1, "For");  
    
        // Using the Get method and the  
        // for loop  
        for (int i = 0; i < ll.size(); i++) {  
    
            System.out.print(ll.get(i) + " ");  
        }  
    
        System.out.println();  
    
        // Using the for each loop  
        for (String str : ll)  
            System.out.print(str + " ");  
    }  
}
Posted by: Guest on May-13-2021
1

java linked list

//Insert, Show Nodes - java-LinkedList
public class Node {
    int data;
    Node next;
    
  	//main
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insert(9);
        list.insert(10);
        list.insert(15);
        list.insert(19);
        list.show();
    }
}
//
class LinkedList {
    Node head;
    //insert a node - method
    public void insert(int data) {
        Node node = new Node();
        node.data = data;
        node.next = null;

        //check if node is single
        if (head == null) {
            head = node;
        } else {
          	// go to the end of linkedList
            Node n = head;
            while (n.next != null) {
                n = n.next;
            }
            //set the last node to be node
            n.next = node;
        }
    }
    //print the list of nodes
    public void show() {
        Node node = head;
        //iterate and print
        while (node.next != null) {
            System.out.println(node.data);
            node = node.next;
        }
        //print the last one that the node.next == null;
        System.out.println(node.data);
    }
}
Posted by: Guest on October-20-2021
0

linked list in java

// Java program to iterate the elements  
// in an LinkedList 
    
import java.util.*;  
    
public class GFG {  
    
    public static void main(String args[])  
    {  
        LinkedList<String> ll  
            = new LinkedList<>();  
    
        ll.add("Geeks");  
        ll.add("Geeks");  
        ll.add(1, "For");  
    
        // Using the Get method and the  
        // for loop  
        for (int i = 0; i < ll.size(); i++) {  
    
            System.out.print(ll.get(i) + " ");  
        }  
    
        System.out.println();  
    
        // Using the for each loop  
        for (String str : ll)  
            System.out.print(str + " ");  
    }  
}
Posted by: Guest on May-13-2021

Code answers related to "java linked list implementation"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language