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

Linked List Full Code

This document provides a comprehensive guide to linked list operations in C++, including creating, printing, inserting, deleting, reversing, searching, counting nodes, and deleting the entire list. It includes code snippets for each operation, demonstrating how to manipulate linked lists effectively. The document serves as a practical reference for programmers looking to implement linked lists in their C++ applications.
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)
10 views3 pages

Linked List Full Code

This document provides a comprehensive guide to linked list operations in C++, including creating, printing, inserting, deleting, reversing, searching, counting nodes, and deleting the entire list. It includes code snippets for each operation, demonstrating how to manipulate linked lists effectively. The document serves as a practical reference for programmers looking to implement linked lists in their C++ applications.
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 List Programs in C++ - Full Code

1. Create & Print a Linked List

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

Node* head = new Node{10, nullptr};


head->next = new Node{20, nullptr};
head->next->next = new Node{30, nullptr};

void printList(Node* head) {


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

2. Insert at Beginning

Node* insertAtBeginning(Node* head, int data) {


Node* newNode = new Node{data, head};
return newNode;
}

3. Insert at End

Node* insertAtEnd(Node* head, int data) {


Node* newNode = new Node{data, nullptr};
if (!head) return newNode;

Node* temp = head;


while (temp->next) temp = temp->next;
temp->next = newNode;
return head;
}

4. Insert at Given Position

Node* insertAtPosition(Node* head, int pos, int data) {


Node* newNode = new Node{data, nullptr};
if (pos == 1) {
newNode->next = head;
return newNode;
}
Node* temp = head;
for (int i = 1; temp && i < pos - 1; i++)
temp = temp->next;
if (!temp) return head;
newNode->next = temp->next;
temp->next = newNode;
return head;
}

5. Delete by Value

Node* deleteNode(Node* head, int key) {


if (!head) return head;
if (head->data == key) {
Node* temp = head;
head = head->next;
delete temp;
return head;
}
Node* temp = head;
while (temp->next && temp->next->data != key)
temp = temp->next;
if (temp->next) {
Node* del = temp->next;
temp->next = del->next;
delete del;
}
return head;
}

6. Delete at Position

Node* deleteAtPosition(Node* head, int pos) {


if (!head) return head;
if (pos == 1) {
Node* temp = head;
head = head->next;
delete temp;
return head;
}
Node* temp = head;
for (int i = 1; temp && i < pos - 1; i++)
temp = temp->next;
if (temp && temp->next) {
Node* del = temp->next;
temp->next = del->next;
delete del;
}
return head;
}

7. Reverse Linked List


Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* current = head;
Node* next = nullptr;
while (current) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}

8. Search an Element

int search(Node* head, int key) {


int pos = 1;
while (head) {
if (head->data == key) return pos;
head = head->next;
pos++;
}
return -1;
}

9. Count Nodes

int countNodes(Node* head) {


int count = 0;
while (head) {
count++;
head = head->next;
}
return count;
}

10. Delete Entire List

void deleteList(Node*& head) {


while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
}

You might also like