Linked List C++
I am new to data structures and I have to implement my own data structures for a project. I have made this working Singly Linked List data structure in C++. I would like to ask everyone who is more experienced than me in this matter if my implementation is optimized enough, and if my memory usage and allocation works fine (because I think that my memory might be broken). Any other suggestions and fixes are welcome.
#pragma once
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <limits>
using namespace std;
template <class T>
struct node
{
T data;
node<T> *next;
};
template <class T>
class SLL
{
private:
node<T> *head, *tail;
public:
SLL() // Constructor
{
head = nullptr;
tail = nullptr;
cout << "Constructor Called" << '\n';
}
~SLL() // Destructor
{
int index = 1;
node<T>* tempo;
while(head != nullptr)
{
tempo = head->next;
delete head;
head = tempo;
cout << "Destructor Called "<< "Destroying node number "<< index << '\n';
index++;
}
}
void createnode(T value)
{
node<T> *temp = new node<T>;
temp->data = value;
temp->next = nullptr;
if(head == nullptr)
{
head = temp;
tail = temp;
temp = nullptr;
}
else
{
tail->next = temp;
tail = temp;
}
}
void display()
{
node<T> *temp = new node<T>;
temp = head;
while(temp != nullptr)
{
cout << temp->data <<" ";
temp = temp->next;
}
cout << '\n';
}
void insert_start(T value)
{
node<T> *temp = new node<T>;
temp->data = value;
temp->next = head;
head = temp;
}
void insert_position(int pos, T value)
{
node<T> *pre = new node<T>;
node<T> *cur = new node<T>;
node<T> *temp = new node<T>;
cur = head;
for(int i=1; i<pos; i++)
{
pre = cur;
cur = cur->next;
}
temp->data = value;
pre->next = temp;
temp->next = cur;
}
void delete_first()
{
node<T> *temp = new node<T>;
temp = head;
head = head->next;
delete temp;
}
void delete_last()
{
node<T> *current = new node<T>;
node<T> *previous = new node<T>;
current = head;
while(current->next != nullptr)
{
previous = current;
current = current->next;
}
tail = previous;
previous->next = nullptr;
delete current;
}
void delete_position(T pos)
{
node<T> *current = new node<T>;
node<T> *previous = new node<T>;
current = head;
for(int i=1; i<pos; i++)
{
previous = current;
current = current->next;
}
previous->next = current->next;
}
int length() const{
node<T> *current = head;
int len = 0;
while(current){
len++;
current = current->next;
}
cout << "The lists length is: " << len << '\n';
return len;
}
T find(int k)
{
node<T> *current = new node<T>;
if(k < 1){
cout << "Invalid Index point" << '\n';
throw std::out_of_range ("Invalid Index point");
}
if(k > length()){
cout << "Index Out of Bounds" << '\n';
throw std::out_of_range ("Invalid Index point");
}
current = head;
int index = 1;
while (index < k && current){
current = current->next;
index++;
}
if(current){
cout << "In index point: " << k << " || " << " The value is: "<< current->data << '\n';
return current->data;
}
}
int search(const T& x) const{
node<T> *current = new node<T>;
current = head;
int index = 1;
while (current && current->data != x){
current = current->next;
index++;
}
if(current){
cout << "The value: " << x << " || " << "Was found in index point: " << index << '\n';
return index;
}
return 0;
}
bool isempty(){
if(head == nullptr){
cout << "The list is empty" << '\n';
return true;
}
cout << "The list is NOT empty" << '\n';
return false;
}
T const findbig(){
T max;
node<T> *current = new node<T>;
node<T> *nn = new node<T>;
nn = head->next;
current = head;
max = std::numeric_limits<T>::min();
int index = 1;
while(current){
if(current->data > max){
max = current->data;
}
current = current->next;
index++;
}
cout << "The BIGGEST value found in the list is: " << max << '\n';
return max;
}
T const findsmall(){
T min;
node<T> *current = new node<T>;
current = head;
min = std::numeric_limits<T>::max();
int index = 1;
while(current){
if(current->data < min){
min = current->data;
}
current = current->next;
index++;
}
cout << "The LOWEST value found in the list is: " << min << '\n';
return min;
}
};
#endif