Header Linked List
⭐Types of Header Linked List
1) Grounded Header Linked List:
CODE:
#include <iostream>struct Node {int data;Node* next;};// Function to create a new nodeNode* createNode(int value) {Node* newNode = new Node;newNode->data = value;newNode->next = nullptr;return newNode;}// Function to create the header nodeNode* createHeaderNode() {return createNode(0);}// Function to insert at the beginning (right after header)void insertAtBeginning(Node* header, int value) {Node* newNode = createNode(value);newNode->next = header->next;header->next = newNode;}// Function to insert at the endvoid insertAtEnd(Node* header, int value) {Node* newNode = createNode(value);Node* current = header;while (current->next != nullptr) {current = current->next;}current->next = newNode;}// Function to insert after a given valuevoid insertAfterValue(Node* header, int afterValue, int newValue) {Node* current = header->next;while (current != nullptr && current->data != afterValue) {current = current->next;}if (current != nullptr) {Node* newNode = createNode(newValue);newNode->next = current->next;current->next = newNode;}else {std::cout << "Value " << afterValue << " not found in the list." << std::endl;}}// Function to delete from the beginningvoid deleteFromBeginning(Node* header) {if (header->next != nullptr) {Node* temp = header->next;header->next = temp->next;delete temp;}else {std::cout << "List is empty. Cannot delete." << std::endl;}}// Function to delete from the endvoid deleteFromEnd(Node* header) {if (header->next == nullptr) {std::cout << "List is empty. Cannot delete." << std::endl;return;}Node* current = header;while (current->next->next != nullptr) {current = current->next;}delete current->next;current->next = nullptr;}// Function to delete a specific valuevoid deleteValue(Node* header, int value) {Node* current = header;while (current->next != nullptr && current->next->data != value) {current = current->next;}if (current->next != nullptr) {Node* temp = current->next;current->next = temp->next;delete temp;}else {std::cout << "Value " << value << " not found in the list." << std::endl;}}// Function to print the listvoid printList(Node* header) {Node* current = header->next;while (current != nullptr) {std::cout << current->data << " ";current = current->next;}std::cout << std::endl;}// Function to free the memory used by the listvoid freeList(Node* header) {Node* current = header;while (current != nullptr) {Node* temp = current;current = current->next;delete temp;}}int main() {Node* header = createHeaderNode();// InsertionsinsertAtBeginning(header, 1);insertAtEnd(header, 3);insertAfterValue(header, 1, 2);std::cout << "After insertions: ";printList(header); // Expected: 1 2 3// DeletionsdeleteFromBeginning(header);std::cout << "After deleting from beginning: ";printList(header); // Expected: 2 3insertAtBeginning(header, 1);deleteFromEnd(header);std::cout << "After deleting from end: ";printList(header); // Expected: 1 2insertAtEnd(header, 3);deleteValue(header, 2);std::cout << "After deleting value 2: ";printList(header); // Expected: 1 3freeList(header);return 0;}
OUTPUT:
After insertions: 1 2 3
After deleting from beginning: 2 3
After deleting from end: 1 2
After deleting value 2: 1 3
2) Circular Header Linked List:
CODE:
#include <iostream>struct Node {int data;Node* next;};// Function to create a new nodeNode* createNode(int value) {Node* newNode = new Node;newNode->data = value;newNode->next = nullptr;return newNode;}// Function to create the header nodeNode* createHeaderNode() {Node* header = createNode(0);header->next = header;return header;}// Function to insert at the beginning (right after header)void insertAtBeginning(Node* header, int value) {Node* newNode = createNode(value);newNode->next = header->next;header->next = newNode;}// Function to insert at the endvoid insertAtEnd(Node* header, int value) {Node* newNode = createNode(value);Node* current = header;while (current->next != header) {current = current->next;}newNode->next = header;current->next = newNode;}// Function to insert after a given valuevoid insertAfterValue(Node* header, int afterValue, int newValue) {Node* current = header->next;while (current != header && current->data != afterValue) {current = current->next;}if (current != header) {Node* newNode = createNode(newValue);newNode->next = current->next;current->next = newNode;}else {std::cout << "Value " << afterValue << " not found in the list." << std::endl;}}// Function to delete from the beginningvoid deleteFromBeginning(Node* header) {if (header->next != header) {Node* temp = header->next;header->next = temp->next;delete temp;}else {std::cout << "List is empty. Cannot delete." << std::endl;}}// Function to delete from the endvoid deleteFromEnd(Node* header) {if (header->next == header) {std::cout << "List is empty. Cannot delete." << std::endl;return;}Node* current = header;while (current->next->next != header) {current = current->next;}Node* temp = current->next;current->next = header;delete temp;}// Function to delete a specific valuevoid deleteValue(Node* header, int value) {if (header->next == header) {std::cout << "List is empty. Cannot delete." << std::endl;return;}Node* current = header;while (current->next != header && current->next->data != value) {current = current->next;}if (current->next != header) {Node* temp = current->next;current->next = temp->next;delete temp;}else {std::cout << "Value " << value << " not found in the list." << std::endl;}}// Function to print the listvoid printList(Node* header) {if (header->next == header) {std::cout << "List is empty." << std::endl;return;}Node* current = header->next;while (current != header) {std::cout << current->data << " ";current = current->next;}std::cout << std::endl;}// Function to free the memory used by the listvoid freeList(Node* header) {if (header->next == header) {delete header;return;}Node* current = header->next;while (current != header) {Node* temp = current;current = current->next;delete temp;}delete header;}int main() {Node* header = createHeaderNode();// InsertionsinsertAtBeginning(header, 3);insertAtBeginning(header, 2);insertAtBeginning(header, 1);std::cout << "After insertions at beginning: ";printList(header); // Expected: 1 2 3insertAtEnd(header, 4);insertAtEnd(header, 5);std::cout << "After insertions at end: ";printList(header); // Expected: 1 2 3 4 5insertAfterValue(header, 3, 6);std::cout << "After inserting 6 after 3: ";printList(header); // Expected: 1 2 3 6 4 5// DeletionsdeleteFromBeginning(header);std::cout << "After deleting from beginning: ";printList(header); // Expected: 2 3 6 4 5deleteFromEnd(header);std::cout << "After deleting from end: ";printList(header); // Expected: 2 3 6 4deleteValue(header, 6);std::cout << "After deleting value 6: ";printList(header); // Expected: 2 3 4freeList(header);return 0;}
OUTPUT:
After insertions at beginning: 1 2 3
After insertions at end: 1 2 3 4 5
After inserting 6 after 3: 1 2 3 6 4 5
After deleting from beginning: 2 3 6 4 5
After deleting from end: 2 3 6 4
After deleting value 6: 2 3 4
π¨Thanks for visiting classpdfindia✨
Welcome to a hub for πNerds and knowledge seekers! Here, you'll find everything you need to stay updated on education, notes, books, and daily trends.
π Bookmark our site to stay connected and never miss an update!
π Have suggestions or need more content? Drop a comment below, and let us know what topics you'd like to see next! Your support means the world to us. π