Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Implemented remove and insertAt method for linked list class. (#766)
Browse files Browse the repository at this point in the history
* Implemented InsertAt method for linked list.

* Implemented remove method.
Added method to get count of elements in the list.
Added a method to clear the list.
Added a destructor which calls clear.
  • Loading branch information
sparkskapil authored Nov 3, 2020
1 parent 745e344 commit 6a69a49
Showing 1 changed file with 115 additions and 9 deletions.
124 changes: 115 additions & 9 deletions C++/Data Structure/Linked List/linkedList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ struct node {
};

class Linked_list {

private:
node *head, *tail;
unsigned int size;

public:
Linked_list() {

head = NULL;
tail = NULL;
size = 0;
}

void insert(int n) {
Expand All @@ -35,6 +37,75 @@ class Linked_list {
tail->next =tmp;
tail = tail->next;
}
size++;
}

unsigned int count() const {
return size;
}

bool insertAt(unsigned int index, int value) {
if(index > size)
return false;

size++;
// Insert at the end of the list
if(index == size){
insert(value);
return true;
}

node * tmp = new node;
tmp -> data = value;
tmp -> next = NULL;

// Insert at the begining of the list
if(index == 0){
tmp -> next = head;
head = tmp;
return true;
}

// Insert in between list
unsigned int current_index = 0;
node *temp = head;
while(temp){
if(current_index == index - 1){
tmp -> next = temp -> next;
temp -> next = tmp;
return true;;
}
temp = temp -> next;
current_index++;
}
return true;
}

bool remove(unsigned int index) {
if(index >= size){
return false;
}
node * prev = NULL;
node * curr = head;
unsigned int curr_index = 0;
while(curr){
if(curr_index == index){
if(prev == NULL){
head = head -> next;
}
else {
prev -> next = curr -> next;
}
delete curr;
curr = NULL;
size--;
return true;
}
prev = curr;
curr = curr->next;
curr_index++;
}
return false;
}

void display() {
Expand All @@ -45,17 +116,52 @@ class Linked_list {
tmp = tmp -> next;
}
}

void clear(){
while(head){
node * curr = head;
head = head -> next;
delete curr;
curr = NULL;
--size;
}
head = NULL;
}

~Linked_list(){
// This is a destructor for Linked list class.
// This will be called when a stack allocated object of this class will go out of scope.
// Or when a heap allocated object of this class is deleted.
// This will release all allocated memory.
clear();
}
};

int main() {

Linked_list a;
a.insert(1);
a.insert(2);
a.insert(3);
a.insert(4);
a.insert(5);
cout << "The linked list is: " << endl;
a.display();
Linked_list list;
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.insert(5);

cout << "\nThe linked list is: " << endl;
list.display();
cout << "\nSize of the list: " << list.count();

list.insertAt(1, 5);
cout<< "\nThe linked list after insertAt(2,5)" << endl;
list.display();
cout << "\nSize of the list: " << list.count();

list.remove(1);
cout<< "\nThe linked list after remove(1)" << endl;
list.display();
cout << "\nSize of the list: " << list.count();

list.clear();
cout << "\nSize of the list after clear(): " << list.count() << endl;

return 0;
}

0 comments on commit 6a69a49

Please sign in to comment.