Skip to content

Instantly share code, notes, and snippets.

@SamoraMachel
Last active May 5, 2021 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamoraMachel/517a1ef86a772aa2f4de9e918cd246f7 to your computer and use it in GitHub Desktop.
Save SamoraMachel/517a1ef86a772aa2f4de9e918cd246f7 to your computer and use it in GitHub Desktop.
Singly Linked List
#ifndef LIST_H_
#define LIST_H_
#include <cstddef>
#include <iostream>
using namespace std;
template<class T> class LinkedList;
// ---------------------------- DECLARATION SECTION --------------------------------------
// Node class
// since this is a singly linked list
// a node contains only a value and a next Node pointer
template<class T>
class Node {
protected:
T value;
Node<T>* next;
public:
Node();
Node(T data);
friend class LinkedList<T>;
};
// for the linked list, we shall maintain
// the head pointer and the current node pointer
// this will enable up to add, remove and iterate
// over data
template<class T>
class LinkedList {
private:
Node<T>* head;
Node<T>* current;
public:
int length;
LinkedList(T data);
LinkedList();
void add_front(T data);
void add_front(Node<T>* node);
void add_back(T data);
void add_back(Node<T>* node);
bool find_node(T data);
bool remove_node(T data);
void printList();
};
// ---------------------------- END OF DECLARATION SECITON --------------------------------------
// ---------------------------- IMPLEMENTATOIN SECTION --------------------------------------
// -------------- NODE Implementations
template<class T>
Node<T>::Node() {
value = NULL;
next = NULL;
}
template<class T>
Node<T>::Node(T data) {
value = data;
next = NULL;
}
//--------------- LINKED LIST Implementation
template<class T>
LinkedList<T>::LinkedList() {
head = NULL;
current = NULL;
length = 0;
}
template<class T>
LinkedList<T>::LinkedList(T data) {
Node<T> newNode = new Node<T>(data);
head = &newNode;
current = &newNode;
length = 0;
}
template<class T>
void LinkedList<T>::add_back(Node<T>* node) {
if(head == NULL) {
head = node;
current = node;
} else {
current->next = node;
current = node;
}
length++;
}
template<class T>
void LinkedList<T>::add_front(Node<T>* node) {
if (head == NULL) {
head = node;
current = node;
} else {
node->next = head;
head = node;
}
length++;
}
template<class T>
void LinkedList<T>::add_back(T data) {
Node<T>* newNode = new Node<T>(data);
this->add_back(newNode);
}
template<class T>
void LinkedList<T>::add_front(T data) {
Node<T>* newNode = new Node<T>(data);
this->add_front(newNode);
}
template<class T>
void LinkedList<T>::printList() {
if( head != NULL) {
Node<T>* n_iterator = head;
cout << "[";
while(n_iterator != NULL) {
if(n_iterator == current)
cout << n_iterator->value;
else
cout << n_iterator->value << ", ";
n_iterator = n_iterator->next;
}
cout << "]" <<endl;
} else {
cout << "The current list is empty";
}
}
template<class T>
bool LinkedList<T>::find_node(T data) {
Node<T> * n_iterator = head;
while(n_iterator != NULL) {
if(n_iterator->value == data)
return true;
n_iterator = n_iterator->next;
}
return false;
}
template<class T>
bool LinkedList<T>::remove_node(T data) {
if(this->find_node(data)) {
Node<T> * previous = NULL;
Node<T> * n_iterator = head;
while(n_iterator != NULL) {
if(head->value == data) {
head = n_iterator->next;
return true;
} else if (n_iterator->value == data){
previous->next = n_iterator->next;
if(previous->next == NULL) {
current = previous;
}
return true;
}
previous = n_iterator;
n_iterator = n_iterator->next;
}
return false;
}
}
// ---------------------------- END OF IMPLEMENTATION SECTION --------------------------------------
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment