DEV Community

Muhammad Ali Khan
Muhammad Ali Khan

Posted on

Linked Lists Algorithms: A Comprehensive Overview

Data structures play a crucial role in organizing and managing data efficiently in computer science. Two fundamental data structures, arrays, and linked lists, represent distinct approaches to storing and accessing data. While arrays use contiguous memory locations, linked lists employ a more flexible, node-based structure. This paper explores the characteristics, advantages, and drawbacks of linked lists compared to arrays, delving into real-world applications and considerations.

Arrays: Contiguous Memory Storage : Arrays are widely used for their simplicity and efficiency in accessing elements. In an array, elements are stored in contiguous memory locations, allowing for direct access using indices. This results in constant-time access to any element, making arrays suitable for scenarios where frequent random access is essential. However, arrays have limitations in terms of flexibility and dynamic memory management.

Linked Lists: Flexible Node-Based Structure: Linked lists, on the other hand, do not rely on contiguous memory allocation. Instead, they consist of nodes, where each node stores data and a reference to the next node in the sequence. This non-contiguous structure provides flexibility, enabling efficient insertion and deletion of elements at any position within the list. However, this flexibility comes at the cost of increased complexity and potential performance trade-offs.

*Key Differences and Use Cases
*

The decision to use arrays or linked lists depends on the specific requirements of a given application. Arrays excel in scenarios where constant-time access to elements is crucial, such as mathematical computations or searching algorithms. Linked lists, with their dynamic structure, are more suitable for situations involving frequent insertions and deletions, like task scheduling or network packet processing.

*Real-world Applications
*

In real-world scenarios, the choice between arrays and linked lists is often dictated by the specific requirements of the application. For instance, in database systems, arrays might be preferred for fast querying, while linked lists could be chosen for efficient management of a dynamic list of tasks in an operating system scheduler.

*Considerations: Linked Lists vs Arrays
*

While linked lists offer flexibility, they come with overhead due to the additional memory required for storing references between nodes. This overhead can impact both memory usage and execution speed. Understanding the trade-offs is crucial when deciding which data structure is best suited for a given task.

*Avoidance of Linked Lists: Myth or Reality?
*

The question of whether to avoid linked lists is a topic of debate. While they provide essential flexibility, linked lists may not always be the optimal choice. In scenarios where constant-time access is critical and memory efficiency is less of a concern, arrays may be a preferable option.

*Doubly Linked Lists: Enhancing Flexibility
*

Doubly linked lists extend the functionality of linked lists by including a reference to the previous node in each node. This additional reference simplifies reverse traversals but comes with increased memory requirements. Below are three examples of using doubly linked lists in C++:

Node definition
struct Node {
int data;
Node* next;
Node* prev;
};

*Inserting a node at the beginning
*

void insertAtBeginning(Node*& head, int newData) {
Node* newNode = new Node{newData, head, nullptr};
if (head != nullptr) {
head->prev = newNode;
}
head = newNode;
}

Deleting a node

void deleteNode(Node*& head, Node* target) {
if (target->prev != nullptr) {
target->prev->next = target->next;
} else {
head = target->next;
}
if (target->next != nullptr) {
target->next->prev = target->prev;
}
delete target;
}
Below is a comprehensive C++ implementation of a singly linked list that covers various cases and concepts, including creating a list, inserting nodes at different positions, deleting nodes, searching, and displaying the list. Additionally, it includes a brief example of a doubly linked list for further understanding.

`template
struct Node {
T data;
Node* next;

Node(const T& value) : data(value), next(nullptr) {}
Enter fullscreen mode Exit fullscreen mode

};
template
class LinkedList {
private:
Node* head;

public:
LinkedList() : head(nullptr) {}
void insertAtBeginning(const T& data) {
Node* newNode = new Node(data);
newNode->next = head;
head = newNode;
}
void insertAtEnd(const T& data) {
Node* newNode = new Node(data);

    if (!head) {
        head = newNode;
        return;
    }

    Node<T>* current = head;
    while (current->next) {
        current = current->next;
    }

    current->next = newNode;
}
void insertAtPosition(const T& data, int position) {
    if (position < 0) {
        std::cerr << "Invalid position\n";
        return;
    }

    Node<T>* newNode = new Node<T>(data);

    if (position == 0) {
        newNode->next = head;
        head = newNode;
        return;
    }

    Node<T>* current = head;
    for (int i = 0; i < position - 1 && current; ++i) {
        current = current->next;
    }

    if (!current) {
        std::cerr << "Invalid position\n";
        delete newNode;
        return;
    }

    newNode->next = current->next;
    current->next = newNode;
}
void deleteNode(const T& data) {
    Node<T>* current = head;
    Node<T>* prev = nullptr;

    while (current && current->data != data) {
        prev = current;
        current = current->next;
    }

    if (!current) {
        std::cerr << "Node not found\n";
        return;
    }

    if (!prev) {
        head = current->next;
    } else {
        prev->next = current->next;
    }

    delete current;
}
bool search(const T& data) const {
    Node<T>* current = head;

    while (current) {
        if (current->data == data) {
            return true;
        }
        current = current->next;
    }

    return false;
}

void display() const {
    Node<T>* current = head;

    while (current) {
        std::cout << current->data << " -> ";
        current = current->next;
    }

    std::cout << "nullptr\n";
}

~LinkedList() {
    Node<T>* current = head;
    Node<T>* next = nullptr;

    while (current) {
        next = current->next;
        delete current;
        current = next;
    }

    head = nullptr;
}
Enter fullscreen mode Exit fullscreen mode

};

int main() {
LinkedList myList;
myList.insertAtEnd(1);
myList.insertAtEnd(2);
myList.insertAtBeginning(0);
myList.insertAtPosition(3, 3);
myList.display(); // Output: 0 -> 1 -> 2 -> 3 -> nullptr

myList.deleteNode(2);
myList.display(); // Output: 0 -> 1 -> 3 -> nullptr

std::cout << "Search for 1: " << (myList.search(1) ? "Found" : "Not found") << std::endl; // Output: Found

struct DoublyNode {
    int data;
    DoublyNode* next;
    DoublyNode* prev;

    DoublyNode(int value) : data(value), next(nullptr), prev(nullptr) {}
};

DoublyNode* head = new DoublyNode(1);
DoublyNode* second = new DoublyNode(2);
DoublyNode* third = new DoublyNode(3);

head->next = second;
second->prev = head;
second->next = third;
third->prev = second;

delete head;
delete second;
delete third;

return 0;
Enter fullscreen mode Exit fullscreen mode

}`

Let’s explore different algorithms and variations for the linked list code. We’ll look at algorithms for finding the middle element, detecting and removing duplicates, and performing a recursive reversal.

`template
struct Node {
T data;
Node* next;

Node(const T& value) : data(value), next(nullptr) {}
Enter fullscreen mode Exit fullscreen mode

};

// Singly Linked List class
template
class LinkedList {
private:
Node* head;

public:
LinkedList() : head(nullptr) {}

// ... Previous code for insertion, deletion, search, and display ...

// Find the middle element of the linked list
Node<T>* findMiddle() const {
    if (!head) {
        return nullptr;
    }

    Node<T>* slow = head;
    Node<T>* fast = head;

    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }

    return slow;
}

// Remove duplicates from an unsorted linked list using a hash set
void removeDuplicates() {
    std::unordered_set<T> uniqueSet;
    Node<T>* current = head;
    Node<T>* prev = nullptr;

    while (current) {
        if (uniqueSet.count(current->data)) {
            prev->next = current->next;
            delete current;
            current = prev->next;
        } else {
            uniqueSet.insert(current->data);
            prev = current;
            current = current->next;
        }
    }
}

// Recursive reversal of the linked list
void recursiveReverse(Node<T>* current, Node<T>* prev = nullptr) {
    if (!current) {
        head = prev;
        return;
    }

    Node<T>* next = current->next;
    current->next = prev;
    recursiveReverse(next, current);
}

// Destructor to free memory
~LinkedList() {
    // ... Previous code for deleting nodes ...
}
Enter fullscreen mode Exit fullscreen mode

};

int main() {
// Example of finding the middle element
LinkedList middleList;
for (int i = 1; i <= 5; ++i) {
middleList.insertAtEnd(i);
}
Node* middleNode = middleList.findMiddle();
std::cout << "Middle Element: " << (middleNode ? middleNode->data : -1) << std::endl; // Output: 3

// Example of removing duplicates
LinkedList<int> duplicatesList;
for (int i = 1; i <= 5; ++i) {
    duplicatesList.insertAtEnd(i);
    duplicatesList.insertAtEnd(i); // Introducing duplicates
}
duplicatesList.display(); // Output: 1 -> 1 -> 2 -> 2 -> 3 -> 3 -> 4 -> 4 -> 5 -> 5 -> nullptr
duplicatesList.removeDuplicates();
duplicatesList.display(); // Output: 1 -> 2 -> 3 -> 4 -> 5 -> nullptr

// Example of recursive reversal
LinkedList<int> recursiveReverseList;
for (int i = 1; i <= 5; ++i) {
    recursiveReverseList.insertAtEnd(i);
}
recursiveReverseList.display(); // Output: 1 -> 2 -> 3 -> 4 -> 5 -> nullptr
recursiveReverseList.recursiveReverse(recursiveReverseList.getHead());
recursiveReverseList.display(); // Output: 5 -> 4 -> 3 -> 2 -> 1 -> nullptr

return 0;
Enter fullscreen mode Exit fullscreen mode

}`

Conclusion

Linked lists and arrays are fundamental data structures, each with its strengths and weaknesses. Understanding their characteristics, use cases, and trade-offs is essential for making informed decisions in software development. Whether it’s constant-time access or dynamic element manipulation, the choice between arrays and linked lists significantly impacts the efficiency and performance of an algorithm or system.

Top comments (0)