Linked list

Data Structure

Data Structure Programming Examples

A linked list is made up of a series of objects, called the nodes of the list. Because a list node is a distinct object. Linked lists are among the simplest and most common data structures.

The principal benefit of a linked list over a conventional array is that the list elements can easily be inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk, while an array has to be declared in the source code, before compiling and running the program. Linked lists allow insertion and removal of nodes at any point in the list, and can do so with a constant number of operations if the link previous to the link being added or removed is maintained during list traversal.

Singly Linked list

Singly linked lists contain nodes which have a data field as well as a next field, which points to the next node in line of nodes. Operations that can be performed on singly linked lists include insertion, deletion and traversal.

Doubly Linked list

Different from a singly linked list, a doubly linked list allows us to go in both directions -- forward and reverse. Such lists allow for a great variety of quick update operations, including insertion and removal at both ends, and in the middle. A node in a doubly linked list stores two references -- a next link, which points to the next node in the list, and a prev link, which points to the previous node in the list.

/* Singly Linked list */
/* Insert node at beginning */
addFirst(String newData):
  create a new node v containing newData
  head = v
  size = size + 1

/* Insert node at end */
addLast(String newData):
  create a new node v containing newData

  /* list is empty */
  if (head == null) {  
      head = v

  /* list is not empty */
  } else {
  tail = v
  size = size + 1

/* Delete node */
  if (head = = null) then
    Indicate an error: the list is empty
  tmp = head
  head = head.getNext()
  size = size - 1

/* Traverse linked list */
  curNode = head
  while (curNode != null)  {
     /* print out the contents of the current node */
     curNode = curNode.getNext()


/* Doubly Linked list */
/* Insert node at beginning */

    /* the current first node */
    w = header.getNext() 

/* Insert node at end */
 addAfter(v, z): 
    w = v.getNext()

/* Delete node */

    /* the current last node */
    v = trailer.getPrev()
    if (v = = header) then
        Indicate an error: the list is empty
    prev = v.getPrev()

          /****** Output ******/

Singly Linked list in C

Singly Linked list in C++

Singly Linked list in JAVA

Doubly Linked list in C

Doubly Linked list in C++

Doubly Linked list in JAVA

Learn more about Singly Linked list and Doubly Linked list