0% found this document useful (0 votes)
9 views3 pages

Linked_Lists_Cpp_Notes

The document provides an overview of linked lists in C++, covering singly, doubly, and circular linked lists. It details operations such as insertion, deletion, and traversal, along with their time complexities. Code snippets for implementing these linked lists are included to illustrate the concepts.

Uploaded by

9iraj.jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Linked_Lists_Cpp_Notes

The document provides an overview of linked lists in C++, covering singly, doubly, and circular linked lists. It details operations such as insertion, deletion, and traversal, along with their time complexities. Code snippets for implementing these linked lists are included to illustrate the concepts.

Uploaded by

9iraj.jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Linked Lists in C++ - Notes (Singly, Doubly, Circular)

1. Singly Linked List

A singly linked list is a linear data structure where each element (node) points to the next.

Operations:

- Insertion at head, tail, or middle

- Deletion by value or position

- Traversal

Time Complexity:

- Search: O(n)

- Insertion (beginning): O(1)

- Insertion (end): O(n)

- Deletion: O(n)

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

void insertAtHead(Node*& head, int val) {


Node* newNode = new Node{val, head};
head = newNode;
}

void printList(Node* head) {


while (head) {
cout << head->data << " ";
head = head->next;
}
}

2. Doubly Linked List

Each node has two pointers: one pointing to the next node and one to the previous.

Operations:
Linked Lists in C++ - Notes (Singly, Doubly, Circular)

- Insertion and deletion from both ends

- Traversal in both directions

Time Complexity:

- Search: O(n)

- Insertion/Deletion: O(1) if pointer is known

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

void insertAtHead(Node*& head, int val) {


Node* newNode = new Node{val, nullptr, head};
if (head) head->prev = newNode;
head = newNode;
}

void printForward(Node* head) {


while (head) {
cout << head->data << " ";
head = head->next;
}
}

3. Circular Linked List

In a circular linked list, the last node points back to the head. Can be singly or doubly circular.

Operations:

- Insertion and deletion at head and tail

- Special handling to maintain the circular link

Time Complexity:

- Similar to singly/doubly, but circular logic must be handled


Linked Lists in C++ - Notes (Singly, Doubly, Circular)

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

void insertAtEnd(Node*& tail, int val) {


Node* newNode = new Node{val};
if (!tail) {
newNode->next = newNode;
tail = newNode;
return;
}
newNode->next = tail->next;
tail->next = newNode;
tail = newNode;
}

void printCircular(Node* tail) {


if (!tail) return;
Node* curr = tail->next;
do {
cout << curr->data << " ";
curr = curr->next;
} while (curr != tail->next);
}

You might also like