|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | + |
| 5 | +//Test cases: |
| 6 | +//1 |
| 7 | +//5 |
| 8 | +//1 2 3 4 5 |
| 9 | +//Expected Output: |
| 10 | +//2 3 4 5 |
| 11 | +//input (index = 3) |
| 12 | +//2 3 4 |
| 13 | +//2 3 |
| 14 | + |
| 15 | + |
| 16 | +//Class for creation of a Node |
| 17 | +class Node{ |
| 18 | + public: |
| 19 | + int data; |
| 20 | + Node* next; |
| 21 | + Node(int data) |
| 22 | + { |
| 23 | + this->data = data; |
| 24 | + this->next = NULL; |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +//Class for implementation of CLL |
| 29 | +class CLL{ |
| 30 | + public: |
| 31 | + //to keep track of head of the LL |
| 32 | + Node* head; |
| 33 | + //to keep track of the last node of LL. |
| 34 | + Node* tail; |
| 35 | + CLL() |
| 36 | + { |
| 37 | + this -> head = NULL; |
| 38 | + this -> tail = NULL; |
| 39 | + } |
| 40 | + //Since this code is to demonstrate deletion, we will only use InsertAtEnd in the CLL |
| 41 | + void insertAtEnd(int data) |
| 42 | + { |
| 43 | + |
| 44 | + //Create new node |
| 45 | + Node* newNode = new Node(data); |
| 46 | + |
| 47 | + //If list is empty |
| 48 | + if(head == NULL) |
| 49 | + { |
| 50 | + head = newNode; |
| 51 | + tail = newNode; |
| 52 | + tail -> next = head; |
| 53 | + } |
| 54 | + |
| 55 | + //If there are elements already present in the list |
| 56 | + else |
| 57 | + { |
| 58 | + tail -> next = newNode; |
| 59 | + tail = newNode; |
| 60 | + tail -> next = head; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + //Delete node from the beginning |
| 66 | + void deleteFromBeg() |
| 67 | + { |
| 68 | + Node* temp = head; |
| 69 | + if(temp == NULL) |
| 70 | + { |
| 71 | + cout<<"LL is empty!"<<endl; |
| 72 | + return; |
| 73 | + } |
| 74 | + else if(temp->next == temp) |
| 75 | + { |
| 76 | + head = NULL; |
| 77 | + tail = NULL; |
| 78 | + free(temp); |
| 79 | + return; |
| 80 | + } |
| 81 | + head = head -> next; |
| 82 | + tail -> next = head; |
| 83 | + free(temp); |
| 84 | + } |
| 85 | + |
| 86 | + //Delete Node from middle given the index |
| 87 | + void deleteFromMid(int index) |
| 88 | + { |
| 89 | + if(index == 0) |
| 90 | + { |
| 91 | + deleteFromBeg(); |
| 92 | + return; |
| 93 | + } |
| 94 | + Node* temp = head; |
| 95 | + if(temp == NULL) |
| 96 | + { |
| 97 | + cout<<"LL is empty!"<<endl; |
| 98 | + return; |
| 99 | + } |
| 100 | + if(temp -> next == temp) |
| 101 | + { |
| 102 | + head = NULL; |
| 103 | + tail = NULL; |
| 104 | + free(temp); |
| 105 | + return; |
| 106 | + } |
| 107 | + int count = 0; |
| 108 | + |
| 109 | + //Traverse to the node to be deleted |
| 110 | + while(count != index - 1 && temp->next != tail) |
| 111 | + { |
| 112 | + temp = temp -> next; |
| 113 | + count++; |
| 114 | + } |
| 115 | + |
| 116 | + //If node to be deleted is the next node |
| 117 | + if(count == index - 1) |
| 118 | + { |
| 119 | + Node* temp1 = temp->next; |
| 120 | + temp->next = temp1->next; |
| 121 | + if(temp1 == tail) |
| 122 | + { |
| 123 | + tail = temp; |
| 124 | + } |
| 125 | + free(temp1); |
| 126 | + } |
| 127 | + |
| 128 | + //If last node has been reached but index has not been found |
| 129 | + if(temp->next == tail && count!=index-1) |
| 130 | + { |
| 131 | + cout<<"Index out of bounds!"<<endl; |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + //Delete last node |
| 138 | + void deleteFromEnd() |
| 139 | + { |
| 140 | + Node* temp = head; |
| 141 | + |
| 142 | + //If LL is empty |
| 143 | + if(temp == NULL) |
| 144 | + { |
| 145 | + cout<<"LL is empty"<<endl; |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + //If LL has just one element |
| 150 | + else if(temp -> next == temp) |
| 151 | + { |
| 152 | + head = NULL; |
| 153 | + tail = NULL; |
| 154 | + free(temp); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + //Otherwise |
| 159 | + while(temp -> next != tail) |
| 160 | + { |
| 161 | + temp = temp -> next; |
| 162 | + } |
| 163 | + Node* temp1 = temp->next; |
| 164 | + temp -> next = temp1 -> next; |
| 165 | + tail = temp; |
| 166 | + free(temp1); |
| 167 | + |
| 168 | + } |
| 169 | + //To display the CLL |
| 170 | + void display() |
| 171 | + { |
| 172 | + Node* temp = head; |
| 173 | + if(temp == NULL) |
| 174 | + { |
| 175 | + cout<<"Empty LL!"<<endl; |
| 176 | + return; |
| 177 | + } |
| 178 | + cout<<temp->data<<" "; |
| 179 | + temp = temp->next; |
| 180 | + while(temp != head) |
| 181 | + { |
| 182 | + cout<<temp->data<<" "; |
| 183 | + temp = temp->next; |
| 184 | + } |
| 185 | + cout<<endl; |
| 186 | + } |
| 187 | + |
| 188 | + }; |
| 189 | + int main() |
| 190 | + { |
| 191 | + int test_cases; |
| 192 | + cout<<"Enter number of test cases:"<<endl; |
| 193 | + cin>>test_cases; |
| 194 | + while(test_cases--) |
| 195 | + { |
| 196 | + cout<<"Enter number of elements:"<<endl; |
| 197 | + int n; |
| 198 | + cin>>n; |
| 199 | + int index; |
| 200 | + CLL* linkedList = new CLL(); |
| 201 | + cout<<"Enter elements: "<<endl; |
| 202 | + while(n--) |
| 203 | + { |
| 204 | + int data; |
| 205 | + cin>>data; |
| 206 | + |
| 207 | + linkedList->insertAtEnd(data); |
| 208 | + } |
| 209 | + cout<<"Current LL is:"<<endl; |
| 210 | + linkedList -> display(); |
| 211 | + cout<<"Deleting first node:"<<endl; |
| 212 | + linkedList -> deleteFromBeg(); |
| 213 | + linkedList -> display(); |
| 214 | + cout<<"Enter index to delete element from (0-based indexing):"<<endl; |
| 215 | + cin>>index; |
| 216 | + cout<<"Deleting node at index "<<index<<":"<<endl; |
| 217 | + linkedList -> deleteFromMid(index); |
| 218 | + linkedList -> display(); |
| 219 | + cout<<"Deleting last node:"<<endl; |
| 220 | + linkedList -> deleteFromEnd(); |
| 221 | + linkedList -> display(); |
| 222 | + } |
| 223 | + |
| 224 | + return 0; |
| 225 | + } |
0 commit comments