Tag Archives: Linked List

C program to delete element from circular linked list

Write a C program to delete element from circular linked list by key. In this article I will explain how to delete by key element from circular linked list. I will explain logic and program to delete element from circular linked list by key.

Continue reading C program to delete element from circular linked list

C program to swap two nodes in a linked list

How to swap two nodes in a linked list is a popular problem in data structure. In this post I will explain how to swap two nodes in a linked list. I will explain to swap two nodes without swapping data in a linked list.

Write a C program to create a linked list and accept two node position from user. The program should swap two nodes in linked list without swapping their data.

Continue reading C program to swap two nodes in a linked list

C program to search an element in linked list

Search is one of the most common operation on performed any data structure. In this post I will explain how to search an element in linked list (iterative and recursive) using C program. I will explain both ways to search, how to search an element in linked list using loop and recursion.

Write a C program to create a function to search an element in linked list. If element exists in the linked list then, it should return its index otherwise -1.

Continue reading C program to search an element in linked list

C program to delete all nodes by key in a linked list

Write a C program to create a linked list and delete all nodes by key. How to delete element by key of a linked list in C program. In previous few programs, I explained how to delete first, last or nth element from a singly linked list. In this post I will explain how to delete all nodes by key in a linked list, delete first node by key or delete last node by key in C program.

Continue reading C program to delete all nodes by key in a linked list

C program to insert a node in Circular Linked List

Write a program to create a circular linked list and insert a new node at the beginning or at any position in the given list. How to insert a new node at the beginning of a circular linked list in C. How to insert a new node at any position in a circular linked list in C. Algorithm to insert new node in Circular linked list in C program.

Inserting new node in a Circular linked list

Continue reading C program to insert a node in Circular Linked List

Data Structure : Circular Linked List

A circular linked list is basically a linear linked list that may be singly or doubly. The only difference is that there is no any NULL value terminating the list. In fact in the list every node points to the next node and last node points to the first node, thus forming a circle. Since it forms a circle with no end to stop hence it is called as circular linked list.

In circular linked list there can be no starting or ending node, whole node can be traversed from any node. In order to traverse the circular linked list only once we need to traverse entire list until the starting node is not traversed again.
A circular linked list can be implemented using both singly linked list and doubly linked list. Here is the logical structure of a circular linked list.

Basic structure of Circular linked list

Circular linked list
Circular Linked List
Doubly Circular Linked List
Doubly Circular Linked List

Advantages of a Circular linked list

  • Entire list can be traversed from any node.
  • Circular lists are the required data structure when we want a list to be accessed in a circle or loop.
  • Despite of being singly circular linked list we can easily traverse to its previous node, which is not possible in singly linked list.

Disadvantages of Circular linked list

  • Circular list are complex as compared to singly linked lists.
  • Reversing of circular list is a complex as compared to singly or doubly lists.
  • If not traversed carefully, then we could end up in an infinite loop.
  • Like singly and doubly lists circular linked lists also doesn’t supports direct accessing of elements.

Applications/Uses of Circular linked list in real life

  • Circular lists are used in applications where the entire list is accessed one-by-one in a loop. Example: Operating systems may use it to switch between various running applications in a circular loop.
  • It is also used by Operating system to share time for different users, generally uses Round-Robin time sharing mechanism.
  • Multiplayer games uses circular list to swap between players in a loop.

Basic operations on Circular linked list

C program to delete a node from doubly linked list

Write a C program to create a doubly linked list and delete a node from beginning, end or at any position of the linked list. How to delete a node from beginning of a doubly linked list. How to delete a node from end of a doubly linked list. How to delete a node from any position of a doubly linked list in C. Algorithm to delete a node from doubly linked list.

Doubly linked list deletion of a node

Continue reading C program to delete a node from doubly linked list

C program to insert node in a Doubly linked list

Write a C program to create a doubly linked list and insert a new node in beginning, end or at any position in the list. How to insert a new node at beginning of a Doubly linked list. How to insert a new node at the end of a doubly linked list. How to insert a new node at any position of a doubly linked list in C. Algorithm to insert node in a doubly linked list.

Insertion of new node in a doubly linked list

Continue reading C program to insert node in a Doubly linked list