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