Linked List

Data structure

Linked List - A data structure in which each element contains a pointer to the next element, thus forming a linear list.

Commonly used in
C++
Java

Linked List, Data Structure, ADt, Java, C++

Linked List

    A Linked List is a list of elements, or nodes, that are linked to one another such that each element references the next. Linked lists and arrays are similar since they both store collections of data, however an array has a fixed size, at least in most commonly used languages; where as a linked list as an arbitrary length, which allows the list to grow and shrink as needed by the client. 

     Another advantage is that a linked list is a flexible data structure: nodes (items) may be added to it or deleted from it at will. However, a linked list does not allow random access, which is the ability to access an arbitrary element of a sequence in equal time. With that said a programmer does not need to worry about how many items a program will have to contain when using a link list.  This ability allows programmers to write larger programs, which requires much less maintenance and memory. A common source of problems in program maintenance is the need to increase the capacity of a program to handle ever increasing collections.

    Linked lists can be implemented in many different programming languages, but it is used must commonly in OOD languages such as C++ and Java.    They also come in four different types: Singly, Doubly, Circularly Double-Linked, and Circular Doubly-Linked List

 
 

Types of Linked Lists

 
Singly - Each node as one reference, which points to the next node or to a null value or empty list if it is the final node.[1]
 
                      
  
 
 
Doubly - Each node contains two references, one to the next node and one to the previous node or to a null value or empty list if it is the final node.[1][2]
 
 
 
Circularly - The last node (tail) references the first node (head) in the list.[1][2]
 
        
                     
 
Circular Doubly - The first node can references the next node and last node.  The last node can also reference the first node and the previous node.[3][2]
 
 
 
Comments- Can someone make a cooler diagram of a circular doubly - linked list.
 
Common Terms
Head - Beginning of List
Tail - End of List
 
 
Example Code
 
public class LinkedList 
{
  /**
   * This interface defines the methods required by any object that can be
   * linked into a linked list.
   */
  public interface Linkable {
    public Linkable getNext(); // Returns the next element in the list

    public void setNext(Linkable node); // Sets the next element in the list
  }

  // This class has a default constructor: public LinkedList() {}

  /** This is the only field of the class. It holds the head of the list */
  Linkable head;

  /** Return the first node in the list */
  public synchronized Linkable getHead() {
    return head;
  }

  /** Insert a node at the beginning of the list */
  public synchronized void insertAtHead(Linkable node) {
    node.setNext(head);
    head = node;
  }

  /** Insert a node at the end of the list */
  public synchronized void insertAtTail(Linkable node) {
    if (head == null)
      head = node;
    else {
      Linkable p, q;
      for (p = head; (q = p.getNext()) != null; p = q)
        /* no body */;
      p.setNext(node);
    }
  }

  /** Remove and return the node at the head of the list */
  public synchronized Linkable removeFromHead() {
    Linkable node = head;
    if (node != null) {
      head = node.getNext();
      node.setNext(null);
    }
    return node;
  }

  /** Remove and return the node at the end of the list */
  public synchronized Linkable removeFromTail() {
    if (head == null)
      return null;
    Linkable p = head, q = null, next = head.getNext();
    if (next == null) {
      head = null;
      return p;
    }
    while ((next = p.getNext()) != null) {
      q = p;
      p = next;
    }
    q.setNext(null);
    return p;
  }

  /**
   * Remove a node matching the specified node from the list. Use equals()
   * instead of == to test for a matched node.
   */
  public synchronized void remove(Linkable node) {
    if (head == null)
      return;
    if (node.equals(head)) {
      head = head.getNext();
      return;
    }
    Linkable p = head, q = null;
    while ((q = p.getNext()) != null) {
      if (node.equals(q)) {
        p.setNext(q.getNext());
        return;
      }
      p = q;
    }
  }

  /**
   * This is a test class that implements the Linkable interface
   */
  static class LinkableInteger implements Linkable {
    int i; // The data contained in the node

    Linkable next; // A reference to the next node in the list

    public LinkableInteger(int i) {
      this.i = i;
    // Constructor

    public Linkable getNext() {
      return next;
    // Part of Linkable

    public void setNext(Linkable node) {
      next = node;
    // Linkable

    public String toString() {
      return i + "";
    // For easy printing

    public boolean equals(Object o) { // For comparison
      if (this == o)
        return true;
      if (!(o instanceof LinkableInteger))
        return false;
      if (((LinkableInteger) o).i == this.i)
        return true;
      return false;
    }
  }

  /**
   * The test program. Insert some nodes, remove some nodes, then print
   * out all elements in the list. It should print out the numbers 4, 6,
   * 3, 1, and 5
   */
  public static void main(String[] args) {
    LinkedList ll = new LinkedList(); // Create a list
    ll.insertAtHead(new LinkableInteger(1)); // Insert some stuff
    ll.insertAtHead(new LinkableInteger(2));
    ll.insertAtHead(new LinkableInteger(3));
    ll.insertAtHead(new LinkableInteger(4));
    ll.insertAtTail(new LinkableInteger(5));
    ll.insertAtTail(new LinkableInteger(6));
    System.out.println(ll.removeFromHead()); // Remove and print a node
    System.out.println(ll.removeFromTail()); // Remove and print again
    ll.remove(new LinkableInteger(2)); // Remove another one

    // Now print out the contents of the list.
    for (Linkable l = ll.getHead(); l != null; l = l.getNext())
      System.out.println(l);
  }

}
 
 
 

References

  1. Wikipedia. Nov. 10, 2008. Accessed Nov.10,2008. < http://en.wikipedia.org/wiki/Linked_list >
    Wiki
  2. Carrano, Frank M. and Prichard, Janet. J Data Abstraction & Problem Solving With Java. 2nd Edition. Addison Wesley.
  3. Model Engineering College. Resources. Accessed Nov. 10, 2008. < http://www.mec.ac.in/resources/notes/notes/ds/ll_files/image018.jpg>
    http://www.mec.ac.in/resources/notes/notes/ds/ll_files/image018.jpg
  4. Linked List example. java2s.com. Accessed Nov. 10,2008. < http://www.java2s.com/Code/Java/Collections-Data-Structure/LinkedListexample.htm >
    Source Code

Comments

Article rating:
Your rating:
Moderated collaboration
All signed in users can suggest edits to the knol, but these need approval from an author before being published
Version: 6
Versions
Last edited: Nov 29, 2008 7:31 AM.

Categories

Based on community consensus.

Activity for this knol

This week:

15pageviews

Totals:

546pageviews