Posts

Showing posts with the label LinkedList

double linked list in Java (Java)

Image
A doubly linked list is a list that contains links to next and previous nodes. Unlike singly linked lists where traversal is only one way, doubly linked lists allow traversals in both ways. Source code import java.util.NoSuchElementException; public class DoublyLinkedListImpl<E> {     private Node head;     private Node tail;     private int size;         public DoublyLinkedListImpl() {         size = 0;     }     /**      * this class keeps track of each element information      * @author java2novice      *      */     private class Node {         E element;         Node next;         Node prev;         public Node(E element, Node next, Node prev) {             this.element = element; ...

Linked List | Types of Linked Lists (Java)

Image
One disadvantage of using arrays to store data is that arrays are static structures and therefore cannot be easily extended or reduced to fit the data set. Arrays are also expensive to maintain new insertions and deletions.  A linked list is a linear data structure where each element is a separate object. Each element (we will call it a  node ) of a list is comprising of two items - the data and a reference to the next node. The last node has a reference to  null . The entry point into a linked list is called the  head  of the list. It should be noted that head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference. A linked list is a dynamic data structure. The number of nodes in a list is not fixed and can grow and shrink on demand. Any application which has to deal with an unknown number of objects will need to use a linked list. Advantages: Linked lists are a dynamic data structure,...