I am inexperienced in C++. I was able to get my LinkedList
to be fully functional. I was hoping to get some feedback as to whether or not I followed bad practices and if there are any risks associated with my code.
LinkedList.h
#pragma once
#include <string>
#include "Node.h"
class LinkedList
{
private:
Node *head;
int count;
public:
LinkedList();
~LinkedList();
int get_count();
void AddNode(int);
std::string GetList();
bool Contains(int);
void Remove(int);
int GetValue(int);
};
LinkedList.cpp
#include <iostream>
#include "LinkedList.h"
LinkedList::LinkedList()
{
head = new Node(0);
}
LinkedList::~LinkedList()
{
}
int LinkedList::get_count(){
return count;
}
void LinkedList::AddNode(int _value){
Node *node = new Node(_value);
Node *previousNode = head;
while (previousNode->next_node && previousNode->next_node->get_value() <= node->get_value()){
previousNode = previousNode->next_node;
}
if (previousNode->next_node){
node->next_node = previousNode->next_node;
}
previousNode->next_node = node;
count++;
}
std::string LinkedList::GetList(){
std::string list;
Node *currentNode = head;
while (currentNode->next_node){
currentNode = currentNode->next_node;
list += std::to_string(currentNode->get_value()) + " ";
}
return list;
}
bool LinkedList::Contains(int number){
Node *currentNode = head;
while (currentNode->next_node){
currentNode = currentNode->next_node;
if (currentNode->get_value() == number){
return true;
}
if (currentNode->get_value() > number){
return false;
}
}
return false;
}
void LinkedList::Remove(int number){
Node *previousNode = head;
Node *currentNode = head;
while (currentNode->next_node){
previousNode = currentNode;
currentNode = currentNode->next_node;
if (currentNode->get_value() == number){
previousNode->next_node = currentNode->next_node;
delete currentNode;
count--;
return;
}
}
}
int LinkedList::GetValue(int index){
Node *currentNode = head;
int currentIndex = -1;
while (currentNode->next_node){
currentNode = currentNode->next_node;
currentIndex++;
if (index == currentIndex){
return currentNode->get_value();
}
}
return -1;
}
Node.h
#pragma once
class Node
{
private:
int value;
public:
Node(int);
~Node();
Node *next_node;
int get_value();
void set_value(int);
};
Node.cpp
#include "Node.h"
Node::Node(int _value)
{
value = _value;
}
Node::~Node()
{
}
int Node::get_value(){
return value;
}
void Node::set_value(int _value){
value = _value;
}