Implementation of Singly Link List using Classes of OOP in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
#include <iostream> using namespace std; // Node class to represent // a node of the linked list. class Node { public: int data; Node* next; // Default constructor Node() { data = 0; next = NULL; } // Parameterised Constructor Node(int data) { this->data = data; this->next = NULL; } }; // Linked list class to // implement a linked list. class Singly_Linked_list { Node* head; public: // Default constructor Singly_Linked_list() { head = NULL; } // Function to add a // node at the end of the // linked list. void Insert_Node(int); // Function to print the // linked list. void printList(); // Function to delete the // node at given position void Remove_Node(int); }; // Function to delete the // node at given position void Singly_Linked_list::Remove_Node(int nodeOffset) { Node *Temporary1 = head, *Temporary2 = NULL; int ListLen = 0; if (head == NULL) { cout
<< "List empty." << endl; return; } // Find length of the linked-list. while (Temporary1 != NULL) { Temporary1 = Temporary1->next; ListLen++; } // Check if the position to be // deleted is less than the length // of the linked list. if (ListLen < nodeOffset) { cout << "Index out of range" << endl; return; } // Declare Temporary1 Temporary1 = head; // Deleting the head. if (nodeOffset == 1) { // Update head head = head->next; delete Temporary1; return; } // Traverse the list to // find the node to be deleted. while (nodeOffset-- > 1) { // Update Temporary2 Temporary2
= Temporary1; // Update Temporary1 Temporary1 = Temporary1->next; } // Change the next pointer // of the previous node. Temporary2->next = Temporary1->next; // Delete the node delete Temporary1; } // Function to add a new node. void Singly_Linked_list::Insert_Node(int data) { // Create the new Node. Node* newNode = new Node(data); // Assign to head if (head == NULL) { head = newNode; return; } // Traverse till end of list Node* Temporary = head; while (Temporary->next != NULL) { // Update Temporary Temporary = Temporary->next; } // add at the last. Temporary->next = newNode; } // Function to print the // nodes of the linked list. void Singly_Linked_list::printList() { Node* Temporary = head; // Check for empty list. if (head == NULL) { cout << "List empty" << endl; return; } // Traverse the list. while (Temporary != NULL) { cout << Temporary->data << " "; Temporary = Temporary->next; } } // Driver Code int main() { Singly_Linked_list list; // adding nodes list.Insert_Node(7); list.Insert_Node(8); list.Insert_Node(1); list.Insert_Node(3); cout << "The Elements in the singly link list: "; // Print the list list.printList(); cout << endl; // Delete node at position 2. list.Remove_Node(3); cout << "The Elements in the singly link list:"; list.printList(); cout << endl; return 0; } |
Output
The Elements in the singly link list:
7 8 1 3
The Elements in the singly link list
7 8 3