This follows from a post I made here. I made changes according to the answer I accepted in the following link. I tried to make all the changes necessary that I could. I just want to see if there are any other further adjustments that I need to have for this generic single linked list.
Here is my header file:
#ifndef LinkedList_hpp
#define LinkedList_hpp
#include <iostream>
template<class T>
class SingleLinkedList {
private:
template<class S>
struct Node {
S data;
Node<S>* next;
};
Node<T>* head;
Node<T>* tail;
public:
SingleLinkedList() : head(nullptr), tail(nullptr) {}
SingleLinkedList(SingleLinkedList const& value) : head(nullptr), tail(nullptr) {
for(Node<T>* loop = value->head; loop != nullptr; loop = loop->next) {
createNode(loop->data);
}
}
SingleLinkedList& operator=(SingleLinkedList const& rhs) { SingleLinkedList copy(rhs);}
void swap(SingleLinkedList& other) noexcept {
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
}
~SingleLinkedList(){
Node<T>* nodePtr = head;
while(nodePtr != nullptr) {
Node<T>* nextNode = nodePtr->next;
nodePtr = nextNode;
delete nodePtr;
}
}
void createNode(const T&& theData) {
Node<T>* temp = new Node<T>;
temp->data = std::move(theData);
temp->next = nullptr;
if(head == nullptr) {
head = temp;
tail = temp;
temp = nullptr;
}
else {
tail->next = temp;
tail = temp;
}
}
void display(std::ostream& str = std::cout) {
Node<T>* temp = head;
while(temp != nullptr) {
str << temp->data << "\t";
temp = temp->next;
}
delete temp;
}
void insert_start(const T& theData) {
Node<T>* temp = new Node<T>;
temp->data = theData;
temp->next = head;
head = temp;
delete temp;
}
void insert_position(int pos, const T& theData) {
Node<T>* previous = new Node<T>;
Node<T>* current = new Node<T>;
Node<T>* temp = new Node<T>;
current = head;
for(int i = 1; i < pos; i++) {
previous = current;
current = current->next;
}
temp->data = theData;
previous->next = temp;
temp->next = current;
}
void delete_first() {
Node<T>* temp = head;
head = head->next;
delete temp;
}
void delete_last() {
Node<T>* previous = nullptr;
Node<T>* current = nullptr;
current = head;
while(current->next != nullptr) {
previous = current;
current = current->next;
}
tail = previous;
previous->next = nullptr;
delete current;
}
void delete_position(int pos) {
Node<T>* previous = new Node<T>;
Node<T>* current = new Node<T>;
current = head;
for(int i = 1; i < pos; i++) {
previous = current;
current = current->next;
}
previous->next = current->next;
}
bool search(const T& x) {
struct Node<T>* current = head;
while (current != NULL) {
if (current->data == x)
return true;
current = current->next;
}
return false;
}
friend std::ostream& operator<<(std::ostream& str, SingleLinkedList& data) {
data.display(str);
return str;
}
};
#endif /* LinkedList_hpp */
Here is the main.cpp file that tests this header file:
#include <iostream>
#include "LinkedList.hpp"
int main(int argc, const char * argv[]) {
SingleLinkedList<int> obj;
obj.createNode(2);
obj.createNode(4);
obj.createNode(6);
obj.createNode(8);
obj.createNode(10);
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"---------------Displaying All nodes---------------";
std::cout<<"\n--------------------------------------------------\n";
std::cout << obj << std::endl;
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"-----------------Inserting At End-----------------";
std::cout<<"\n--------------------------------------------------\n";
obj.createNode(55);
std::cout << obj << std::endl;
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"----------------Inserting At Start----------------";
std::cout<<"\n--------------------------------------------------\n";
obj.insert_start(50);
std::cout << obj << std::endl;
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"-------------Inserting At Particular--------------";
std::cout<<"\n--------------------------------------------------\n";
obj.insert_position(5,60);
std::cout << obj << std::endl;
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"----------------Deleting At Start-----------------";
std::cout<<"\n--------------------------------------------------\n";
obj.delete_first();
std::cout << obj << std::endl;
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"----------------Deleting At End-----------------";
std::cout<<"\n--------------------------------------------------\n";
obj.delete_last();
std::cout << obj << std::endl;
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"--------------Deleting At Particular--------------";
std::cout<<"\n--------------------------------------------------\n";
obj.delete_position(4);
std::cout << obj << std::endl;
std::cout << std::endl;
obj.search(8) ? std::cout << "Yes" << std::endl : std::cout << "No" << std::endl;;
return 0;
}