Linked List Data Structure

A linked list is a linear data structure used to stores similar data in memory. The elements in the linked list are not stored in adjacent memory locations but are stored randomly. The linked list is a collection of elements known as nodes which contains two fields of information. One contains the data items and the other contains the link or address of the successor list element. A node can be represented as,   


There are different types of linked lists that are formatted by changing the link position. They are,

Difference between the linked list and an array

Array

Linked list

  • An array is the organizing of fixed-sized similar elements in the contiguous memory location.
  • In arrays, the number of elements is fixed and does not change.
  • Elements of an array are stored in contiguous or adjacent memory locations.
  • Arrays are easier to maintain than a linked list.
  • A linked list is a linear data structure used to store similar data in memory.
  • The number of data items stored in a linked list varies.
  • Elements in a linked list are stored anywhere in memory, and the order of elements is maintained by links between them.
  • Linked lists are difficult to maintain.

Singly Linked List

A singly linked list is a list in which each node is linked with others. A node is a structure element containing data and link field.

 

What is Node?

The node in the list need not be stored in the contiguous memory location. A node can contain any number of data fields but only one link field.

Advantage Of Single Linked List

Disadvantages Of Single Linked List

Suppose we want to delete an element at the position i, we need the node at position (i-1) to link to (i+1) node. This can only be obtained by traversal from the first addressed node to (i-1) position.

Basics Operations on the Singly Linked List

Single Lined list traversal

The singly linked list can be traversed in the direction from the first node to the last node using the pointer root or head or FIRST.

Algorithm: Traversal (FIRST)

Single Lined list Insertion

The insertion of a node into a singly linked list can be at different locations i.e., at beginning of list at a specified location in the list, or at the end of the list.

Insertion of the node at the Beginning of list

To inserts a node at the beginning. Make the link field of new node point to the first node and make the FIRST pointer to point to the new node.

Algorithm: insert at the beginning (element, FIRST)

Insertion of node at the Specified Position

To insert a new node between the ith and (i+1)th node, the list should be initially traversed up to ith node, then the link field of ith node should be made to point to a new node and the link field of the new node is made to point to (i+1)th node.

Algorithm: insert at a specified location (element, FIRST, loc)

[quote] temp=temp -> link[traversing the list up to ith position]

                                     [repeat step 6 until i=loc-1]

new -> link=templink

temp -> link=new[inserting the node at the specified location]

[/quote]

Insert of node at the End of List

To insert a node at the end of the singly linked list traverse the list up to the last node, then make the link of the last node point to a new node. Make the link field of new node point to NULL.

Algorithm: insert at the end (element, FIRST)

Single Lined list Deletion

A node can be deleted from the singly linked list in two ways, i.e., logically, physically. In logical deletion, the node is no more present in the list (i.e., reference to the node is broken) but physically present in the memory. But, in physical deletion of the node, the node is permanently deleted by deallocating (free) the memory allocated to the node.

The deletion of a node at different locations in the list, i.e., in the beginning, at the specified location, at the end can be implemented through the following algorithms.

Deletion of node at the Beginning

Algorithm: delete at the Beginning (FIRST)

[quote]

               FIRST=FIRSTlink[making the FIRST pointer to point to the second node]

              [logically deleting the first node]

[/quote]

Deletion of node at End

To delete a node at the end, traverse the list up to last but one node and store and store NULL in its link field.

Algorithm: delete at the end (FIRST)