Linked List Data Structure in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

This article is a detailed resource that introduces linked lists in C. It covers the fundamentals, including node structure and the three main types of linked lists (singly, doubly, and circular). Practical C code for various operations, such as insertion and deletion, is provided.

The article emphasizes the advantages and disadvantages of linked lists in C and explores their real-world applications, including web browsers, operating systems, compilers, databases, and graphics software.

Linked List Data Structure

A linked list is a linear data structure that consists of a sequence of nodes. Each node contains a data field and a reference (link) to the next node in the sequence. The first node in the list is called the head node, and the last node in the list is called the tail node.

Node Structure

A node in a linked list typically consists of two components:

  • Data: This field stores the actual value or data associated with the node.
  • Next Pointer: This field stores the memory address (reference) of the next node in the sequence.

Head and Tail

The head and tail pointers are used to access the linked list. The head pointer points to the first node in the list, and the tail pointer points to the last node in the list.

Example:

Here is a diagram of a linked list with three nodes:

Types of linked lists

There are three main types of linked lists in C:

1. Singly Linked Lists.

  • Singly linked lists in C are the simplest type of linked list.
  • Each node in a singly linked list contains a data field and a pointer to the next node in the list.
  • The last node in the list points to null, indicating the end of the list.
  • Example: Stack, queue, linked list implementation of a dynamic array

2. Doubly Linked Lists.

  • Doubly linked lists in C are more complex to implement than singly linked lists, but they are more efficient for certain operations, such as insertion, deletion, and traversal in both directions.
  • Each node in a doubly linked list contains a data field, a pointer to the next node in the list, and a pointer to the previous node in the list.
  • Example: LRU cache, undo/redo history, doubly linked list implementation of a binary tree

3. Circular Linked Lists

  • Circular linked lists are the most complex type of linked list to implement, but they can be very efficient for certain operations, such as traversal and queueing.
  • In a circular linked list, the last node in the list points back to the first node in the list, forming a loop.
  • Example: Circular buffer, circular queue, circular linked list implementation of a hash table.

Representation of Linked List in C

A linked list in C can be represented in a variety of ways, but the most common representation is to use a node structure. A node structure typically contains two fields:

  • Data: This field stores the actual data associated with the node.
  • Next: This field contains a pointer to the next node in the list.

The first node in the list is called the head node, and the last node in the list is called the tail node. The head node points to the first node in the list, and the tail node points to null, indicating the end of the list.

What is Linked List in C

Why is Linked List Data Structure needed?

Linked lists in C are needed because they offer a number of advantages over other data structures, such as arrays:

  • Dynamic size: Linked lists in C can grow and shrink dynamically as needed. This is because nodes can be added and removed from the list without having to shift the other elements in the list.
  • Efficient insertion and deletion: Inserting and deleting elements from a linked list is typically more efficient than doing the same operations on an array.
  • Efficient memory usage: Linked lists in C can be more efficient in terms of memory usage than arrays, especially when the list is sparse (contains many empty elements). This is because each node in a linked list only stores the data and the reference to the next node.

Linked List Utility

Linked lists in C are a versatile data structure with a wide range of utilities. Some of the most common utilities of linked lists include:

  • Storage: Linked lists in C can be used to store any type of data, including integers, strings, objects, and even other linked lists.
  • Queues: Linked lists in C can be used to implement queues, which are FIFO (first-in, first-out) data structures.
  • Stacks: Linked lists in C can be used to implement stacks, which are LIFO (last-in, first-out) data structures.
  • Graphs: Linked lists in C can be used to implement graphs, which are data structures that represent relationships between entities.
  • Undo/redo: Linked lists in C can be used to implement undo/redo functionality. This allows users to reverse their actions if they make a mistake.

Linked List Implementation in C

Output

Linked List Complexity

OperationSingly Linked ListDoubly Linked ListCircular Linked List
Insertion at the beginningO(1)O(1)O(1)
Insertion at the endO(n)O(1)O(1)
Insertion at a given positionO(n)O(n)O(n)
Deletion at the beginningO(1)O(1)O(1)
Deletion at the endO(n)O(1)O(1)
Deletion at a given positionO(n)O(n)O(n)
Searching for an elementO(n)O(n)O(n)
Accessing an element by indexO(n)O(n)O(n)
Traversing the listO(n)O(n)O(n)

Linked List Applications

  1. Web browsers use linked lists to store the history of visited websites.
  2. Operating systems use linked lists to store the process queue, memory management, and file system.
  3. Compilers use linked lists to store symbol tables, parse trees, and code generation.
  4. Databases use linked lists to store data in tables and indexes.
  5. Graphics and animation software use linked lists to store objects and their relationships.

Operations on Linked Lists in C

1. Insertion

Insertion in a linked list in C is the process of adding a new node to the linked list.

insert_begin()

Insert Begin Function in C

insert_end()

Insert End Function in C

insert_pos()

Insert Pos Function in C

2. Deletion

Deletion in a linked list is the process of removing a node from the linked list.

delete_begin()

Delete Begin Function in C

delete_end()

Delete End Function in C

delete_pos()

Delete Pos Function in C

3. Searching

Searching in a linked list in C involves the process of locating a specific element or value within the list.

To perform a linear search in a linked list in C:

  1. Traverse the linked list from the beginning. Compare the data in the current node to the data that you are searching for.
  2. If the data in the current node matches the data that you are searching for, then return the current node.
  3. If the data in the current node does not match the data that you are searching for, then move to the next node.
  4. Repeat steps 2-4 until the desired node is found or the end of the linked list is reached.

Advantages of Linked Lists

  1. Dynamic Size
  2. Efficient Insertion and Deletion
  3. Memory Efficiency
  4. Easy Implementation of Abstract Data Types
  5. More Efficient Sorting

Disadvantages of Linked Lists

  1. No Random Access: Linked lists in C do not provide random access to elements like arrays do.
  2. Extra Memory Usage: Linked lists in C require extra memory compared to arrays. Each element in a linked list requires a reference to the next element.
  3. More Complex Implementation: Implementing a linked list is more complex than implementing an array because it requires managing pointers and dynamically allocating memory.

Conclusion

  1. LinkedList is a Linear Data Structure which don’t store elements at contiguous memory locations.
  2. A node contains two fields data, next field to store the reference of next node.
  3. Node is just a blueprint of structure.
  4. LinkedList in C is a preferred Data Structure because of its efficient insertion and deletion.
  5. Doubly LinkedList, Circular LinkedList are variation of Singly linked list implementation in c.