ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP2117Sorted Linked List
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP2117

    Data Structures

    Progress0 / 37 done
    Topics37
    1Abstract Data Types2Complexity Analysis3Big Oh Notation4Stacks (Linked Lists and Array Implementations)5Recursion and analyzing recursive algorithms6Divide and Conquer Algorithms7Sorting Algorithms8Selection Sort9Insertion Sort10Merge Sort11Quick Sort12Bubble Sort13Heap Sort14Shell Sort15Radix Sort16Bucket Sort17Queue18Dequeuer19Priority Queues (linked and array implementations of queues)20Linked List and Its Various Types21Sorted Linked List22Searching an Unsorted Array23Binary Search for Sorted Arrays24Hashing and Indexing25Open Addressing and Chaining26Trees and Tree Traversals27Binary Search Trees28Heaps29M-Way Trees30Balanced Trees31Graphs32Breadth-First Traversal33Depth-First Traversal34Topological Order35Shortest Path36Adjacency Matrix and Adjacency List Implementations37Memory Management and Garbage Collection
    2 min read
    Topic 21 of 37
    Topic Study Note Illustration
    21

    Sorted Linked List

    A sorted linked list is a variation of a linked list where the nodes are arranged in a sorted order based on their values. Every time a...

    Sorted Linked List

    A sorted linked list is a variation of a linked list where the nodes are arranged in a sorted order based on their values. Every time a new node is inserted, it is placed in its correct position to maintain the sorted order. This makes it different from a regular linked list, where new elements can be added at any arbitrary position (start, end, or any specific index).

    Operations on a Sorted Linked List

    1. Insertion: The new node is inserted in the correct position to maintain the sorted order.
    2. Traversal: Similar to a regular linked list, traversal happens from the head node to the last node.
    3. Deletion: Like a regular linked list, a node can be deleted from the beginning, end, or any position.

    C++ Implementation of Sorted Insertion

    #include <iostream>
    using namespace std;
    
    struct Node {
        int data;
        Node* next;
    };
    
    class SortedLinkedList {
        Node* head;
    
    public:
        SortedLinkedList() {
            head = nullptr;
        }
    
        // Function to insert a node in a sorted manner
        void insertSorted(int value) {
            Node* newNode = new Node();
            newNode->data = value;
            newNode->next = nullptr;
    
            // If the list is empty or the new value is smaller than the head's data
            if (head == nullptr || head->data >= value) {
                newNode->next = head;
                head = newNode;
            } else {
                Node* current = head;
                // Traverse to find the proper position
                while (current->next != nullptr && current->next->data < value) {
                    current = current->next;
                }
                newNode->next = current->next;
                current->next = newNode;
            }
            cout << value << " inserted in sorted list.\n";
        }
    
        // Function to display the sorted linked list
        void display() {
            Node* temp = head;
            while (temp != nullptr) {
                cout << temp->data << " -> ";
                temp = temp->next;
            }
            cout << "nullptr\n";
        }
    };
    
    int main() {
        SortedLinkedList list;
        list.insertSorted(30);
        list.insertSorted(10);
        list.insertSorted(20);
        list.insertSorted(40);
        list.display();
        return 0;
    }
    

    Time Complexity of Insertion:

    • Best Case: O(1)O(1)O(1) if the element is inserted at the beginning.
    • Worst Case: O(n)O(n)O(n) if the element is inserted at the end (requires traversing the entire list).

    Advantages:

    • The list is always sorted, so searching for elements can be more efficient in specific use cases.

    ← Previous topic 20
    Linked List and Its Various Types
    Next topic 22 →
    Searching an Unsorted Array

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    2mremaining
    410words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Data Structures