This document explains a simple implementation of a singly linked list data structure. The program provides various operations to manipulate the linked list.
typedef struct node {
int data; // Stores the actual value
node* link; // Points to the next node
} node;+----------+------+
| data | link |
+----------+------+
Creates a new linked list by repeatedly adding nodes.
Before: NULL
After: [1]→[2]→[3]→NULL
Adds a new node at the start of the list.
Before: [2]→[3]→NULL
After: [1]→[2]→[3]→NULL
Adds a new node at the end of the list.
Before: [1]→[2]→NULL
After: [1]→[2]→[3]→NULL
Inserts a node at a specific position.
Position 2:
Before: [1]→[3]→NULL
After: [1]→[2]→[3]→NULL
Removes a node from a specific position.
Delete at position 2:
Before: [1]→[2]→[3]→NULL
After: [1]→[3]→NULL
root: Always points to the first nodep: Used for traversalq: Used for new node creation
- Uses dynamic memory allocation with
newoperator - Each node is allocated separately on the heap
- Not handling empty list cases
- Not updating root when deleting first node
- Not properly maintaining links when inserting/deleting
| Operation | Time Complexity |
|---|---|
| Insert at Beginning | O(1) |
| Insert at End | O(n) |
| Insert at Position | O(n) |
| Delete at Position | O(n) |
| Display | O(n) |
QList myList;
myList.create(); // Create initial list
myList.insertAtBeg(5); // Insert 5 at beginning
myList.display(); // Show the listStep 1: Create new node
[New]
Step 2: Point new node to current root
[New]→[Old Root]→[...]→NULL
Step 3: Update root to new node
Root = [New]→[Old Root]→[...]→NULL
Step 1: Traverse to position-1
[1]→[2]→[3]→[4]→NULL
↑
p
Step 2: Update links
[1]→[2]→[4]→NULL
↑ ⤴
p
This implementation provides a foundation for understanding linked list operations and can be extended for more complex applications.