Skip to content

Instantly share code, notes, and snippets.

@iKrishneel
Last active February 22, 2018 06:26
Show Gist options
  • Save iKrishneel/200cc0f462226f130000362108fc4a30 to your computer and use it in GitHub Desktop.
Save iKrishneel/200cc0f462226f130000362108fc4a30 to your computer and use it in GitHub Desktop.
singly linked list tutorial
cmake_minimum_required(VERSION 2.8)
project(linked_list)
add_executable(main linked_list.cpp)
target_link_libraries(main)
#include <iostream>
template<class T>
struct Node {
T data;
Node<T> *next;
};
template<class T>
class List {
private:
Node<T> *head;
Node<T> *tail;
public:
List();
void createNode(T value);
void printList();
void insertNode(int position, T value);
void deleteNode(int position);
void reverseList();
int num_nodes_;
};
template<class T>
List<T>::List() : num_nodes_(0) {
head = NULL;
tail = NULL;
}
template<class T>
void List<T>::createNode(T value) {
Node<T> *temp = new Node<T>;
temp->data = value;
temp->next = NULL; //! becomes the last node
if (this->head == NULL) {
this->head = temp;
this->tail = temp;
temp = NULL;
} else {
this->tail->next = temp;
this->tail = temp;
}
this->num_nodes_++;
}
template<class T>
void List<T>::insertNode(int position, T value) {
//! if list is empty
if (this->head == NULL && position != 0) {
std::cout << "ERR: cannot insert node in empty list" << "\n";
return;
} else if (this->head == NULL && position == 0) {
this->createNode(value);
return;
}
Node<T> *temp = new Node<T>;
temp->data = value;
//! insert at the head
if (this->head != NULL && position == 0) {
temp->next = head;
this->head = temp;
this->num_nodes_++;
return;
}
//! check if position is at end
int icount = this->num_nodes_;
if (icount == position) {
std::cout << "inserting at the tail" << "\n";
temp->next = NULL;
this->tail->next = temp;
this->tail = temp;
this->num_nodes_++;
} else if (position > icount) {
std::cout << "ERR: the list is smaller then position" << "\n";
} else {
std::cout << "inserting in the middle" << "\n";
Node<T> *n = new Node<T>;
n = this->head;
icount = 0;
while (icount != position && n != NULL) {
if (icount == position - 1) {
Node<T> *p = new Node<T>;
p = n->next;
n->next = temp;
temp->next = p;
}
n = n->next;
icount++;
}
this->num_nodes_++;
}
}
template<class T>
void List<T>::deleteNode(int position) {
if (this->head == NULL) {
std::cout << "cannot delete empty list" << "\n";
return;
}
//! count the list elements
int icount = this->num_nodes_;
if (this->head != NULL && position == 0) {
std::cout << "deleting the HEAD node" << "\n";
Node<T> *temp = new Node<T>;
temp = this->head;
this->head = head->next;
delete temp;
this->num_nodes_--;
} else if (this->head != NULL & position == icount) {
std::cout << "deleting the TAIL node" << "\n";
Node<T> *current = new Node<T>;
current = this->head;
while (current->next != NULL) {
if (current->next->next == NULL) {
this->tail = current;
current->next = NULL;
delete current->next;
this->num_nodes_--;
break;
}
current = current->next;
}
return;
//! old code
/*
Node<T> *temp = new Node<T>;
temp = this->head;
int i = 0;
while (temp != NULL && i != icount) {
if (i == icount - 2) {
this->tail = temp;
temp->next = NULL;
delete temp->next;
} else {
temp = temp->next;
}
i++;
}
this->num_nodes_--;
*/
} else if (this->head != NULL && position > 0 && position < icount) {
std::cout << "Deleting at " << position << "\n";
Node<T> * current = new Node<T>;
current = this->head;
int k = 0;
while (current != NULL) {
if (k++ == position - 1) {
Node<T> *temp = new Node<T>;
temp = current->next;
current->next = current->next->next;
delete temp;
break;
}
current = current->next;
}
return;
//! old code
/*
int i = 0;
Node<T> *temp = new Node<T>;
temp = this->head;
while (temp != NULL && i != position) {
if (i == position - 1) {
Node<T> *n = new Node<T>;
n = temp->next;
temp->next = temp->next->next;
n->next = NULL;
delete n;
}
temp = temp->next;
i++;
}
this->num_nodes_--;
*/
}
}
template<class T>
void List<T>::reverseList() {
if (this->head == NULL) {
std::cout << "cannot reverse an empty list" << "\n";
return;
}
int icount = this->num_nodes_;
Node<T> *temp = new Node<T>;
temp = this->head;
Node<T> *rev_temp = new Node<T>;
rev_temp = NULL;
Node<T> *prev = new Node<T>;
prev = NULL;
while (temp != NULL) {
Node<T> *next = temp->next;
if (next == NULL) {
rev_temp = temp;
}
std::cout << "-------------------" << "\n";
this->head = prev;
this->printList();
temp->next = prev;
prev = temp;
temp = next;
}
this->head = rev_temp;
this->printList();
}
template<class T>
void List<T>::printList() {
Node<T> *temp = new Node<T>;
temp = this->head;
int icount = 0;
while (temp != NULL) {
std::cout << temp->data << "\t";
temp = temp->next;
icount++;
}
std::cout << "\nTotal Node: " << icount << "\n\n";
}
void testLinkedList() {
List<int> linked_list;
linked_list.createNode(25);
linked_list.createNode(50);
linked_list.createNode(90);
linked_list.createNode(40);
linked_list.printList();
linked_list.insertNode(4, 55);
linked_list.printList();
linked_list.insertNode(0, 50);
linked_list.printList();
linked_list.insertNode(4, 60);
linked_list.printList();
linked_list.deleteNode(0);
linked_list.printList();
linked_list.deleteNode(6);
linked_list.printList();
linked_list.deleteNode(3);
linked_list.printList();
}
int main(int argc, char *argv[]) {
testLinkedList();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment