A Brief Introduction To Linked Lists


If you are a programmer, you must have heard about the concept of linked lists, quite a few times! 

 

Learning about linked lists is essential as they form an important part of the data structure. Hence, we have come up with this blog wherein we are going to explain in depth about linked lists, its categories, how it is represented and what are the functions that it performs.

 

Also, we are going to discuss how to sort a stack and remove loop in linked list. But first, you have to know about what actually linked lists are?

 

What is a Linked List? 

 

Linked lists are defined as the series of data structures which are connected to each other through links. In this sequence, there are certain items. Whereas, each link has some sort of connection with the other link. 

 

After array, linked list is the most useful and popular data structure. When you are studying about linked list, it’s important for you to understand its three main concepts:

 

  • Link: Each of the links stores a data which is called as an element.
  • Next:In a linked list, each link contains a link with the next link which is known as Next.
  • LinkedList: Linked list has some sort of connection link with the first link known as First.

 

Linked List Representation

 

As we know, a linked list is defined as the node series in which the address of your first node gets stored in a pointer named HEAD. Each of these nodes in the linked list has following parts which are:

 

  • Data item
  • Address which points towards the other node

 

As if we want to represent any of these nodes in C language the structure would be:

 

Structure node

 

{

 

Int data;

 

*next;

 

} ;



Linked List Categories

 

When you study linked lists, you should know that there are mainly three types of linked lists. They are discussed as follows:

 

Singly Linked List 

 

This category of linked list contains nodes that have data parts and an address part [ next part]. It means all the points on the next node in the sequence of that node.

 

In singly list you can mainly perform the operations of insertion, traversal and deletion.

 

Doubly Linked List 

 

In this kind of linked list, every node has data parts and mainly two addresses. One address is for the previous node and one address is for the next node. 

 

Circular Linked List 

 

Circular linked list is mostly similar to the singly linked list. It involves the usage of the last node which holds the address of the first node. It will form a circular chain. 

 

After having a clear understanding of the linked list categories, it is important to know about its operations.

 

Linked List Operations

 

Traversal 

 

If we have a linked list ‘ K’ , you can traverse the whole linked list  by starting it from the head note. Suppose if there are n number of nodes, then traversal time complexity will become O (N). 

 

Insertion

 

Inserting the new node at any given position will be achieved by manipulating the next two pointers. Consider prevNode and the newNode set. Set newNode next pointer to the prevNode next pointer and then set prevNode pointer to the newNode pointer.

 

The prevNode pointer denotes the pointer of the node where the newNode is being inserted. As only constant pointers change irrespective of what the size of the linked list is. The complexity will always be O(1)

 

Deletion

 

If we want to delete a node from the given linked list, we have to take certain steps into account. They are as follows:

 

  • Firstly find the previous node of the node which has to be deleted.
  • Secondly you should change the pointer of the next node
  • Then at last, free up the memory present in the last node.

 

In deletion, there would be chances of having a special case in which you have to delete the first node. In such cases, the head of the linked list is updated.

 

Searching

 

In order to search any given value of the list, it's important for you to traverse a linked list. Then compare the present value in the node with it. 

 

Updation

 

When you are going to update a node's value, you have to set the data part to its next value.

 

Firstly, you should update the first node value in which data is equal with the val to set to a new value called newVal.


Comments

Popular posts from this blog

What are the 5 types of inheritance?

Remove duplicates from a sorted linked list

How often can you Buy and Sell the same stock?