patterncppMinor
Make LinkedList in C++ more clean
Viewed 0 times
cleanmakemorelinkedlist
Problem
I have been working on a linked list for some while now and started out with this.
Is my code clean? Or is there some way to make it better and more readable?
The reason for this question is for me to be a better programmer. I would love to get one step further for making more clean code.
my class:
my main:
my functions:
```
void Node::addValueLeft(int nVal)
{
Node *newNode = new Node;
if(!head)
{
newNode->nX = nVal;
newNode->next = NULL;
head = newNode;
}
else
{
newNode->nX = nVal;
newNode->next = head;
head = newNode;
}
}
void Node::printList()
{
if(!head) //list is empty - nothing to print
{
//cout nX next;
}
}
}
void Node::addValueRight(int nVal)
{
Node *tempNode1 = new Node;
if(!head) //List is empty
{
tempNode1 -> nX = nVal;
tempNode1 -> next = head;
head = tempNode1;
}
else
{
tempNode1 = head;
while(tempNode1 -> next != NULL) //Go to last Node
{
tempNode1 = tempNode1->next;
}
Node *tempNode2 = new Node; //create temp node
Is my code clean? Or is there some way to make it better and more readable?
The reason for this question is for me to be a better programmer. I would love to get one step further for making more clean code.
my class:
using namespace std;
class Node
{
public:
int nX;
Node *next;
//constructor
Node(){ head = NULL; } //Initialising head to NULL
void addValueLeft(int nVal);
void addValueRight(int nVal);
void deleteValueLeft();
void deleteValueRight();
void printList();
private:
Node *head; //a pointer to the first Node
};my main:
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
//creating a new object
Node list;
list.addValueLeft(5);
list.addValueLeft(6);
list.addVaueRight(8);
list.deleteValueRight();
list.printList();
return 0;
}my functions:
```
void Node::addValueLeft(int nVal)
{
Node *newNode = new Node;
if(!head)
{
newNode->nX = nVal;
newNode->next = NULL;
head = newNode;
}
else
{
newNode->nX = nVal;
newNode->next = head;
head = newNode;
}
}
void Node::printList()
{
if(!head) //list is empty - nothing to print
{
//cout nX next;
}
}
}
void Node::addValueRight(int nVal)
{
Node *tempNode1 = new Node;
if(!head) //List is empty
{
tempNode1 -> nX = nVal;
tempNode1 -> next = head;
head = tempNode1;
}
else
{
tempNode1 = head;
while(tempNode1 -> next != NULL) //Go to last Node
{
tempNode1 = tempNode1->next;
}
Node *tempNode2 = new Node; //create temp node
Solution
Here's some feedback (not an exhaustive list).
Please stop doing this. when you add
Notes:
The only ways lines (1) and (2) are acceptable, are:
Client code example:
Otherwise, nX and next should be private data, with getters to read the values and setters to validate and set the values.
Line (3) is a bad implementation for a constructor, because it leaves a new instance in a partially-initialized state (it should initialize nX, next and head, explicitly and in this specific order).
Better implementation:
Note regarding class API design:
You are implementing both a node (for an element of the list) and a list of nodes. You should have two classes instead of one, and the
Eventually, you should consider using node as an internal structure of the list:
using namespace std;Please stop doing this. when you add
using namespace directives in header files, any file including that header will have potential namespace clashes (which is the problem namespaces are trying to prevent).class Node
{
public:
int nX; // (1)
Node *next; // (2)
Node(){ head = NULL; } // (3)Notes:
The only ways lines (1) and (2) are acceptable, are:
- if instances of your class are valid regardless what you place in nX and next.
Client code example:
Node n;
n.nX = 1001; // is this valid?
n.next = n; // is this valid?- if Node class is moved from the global namespace to the private section of another class. That way, the outer class will protect the node's data.
Otherwise, nX and next should be private data, with getters to read the values and setters to validate and set the values.
Line (3) is a bad implementation for a constructor, because it leaves a new instance in a partially-initialized state (it should initialize nX, next and head, explicitly and in this specific order).
Better implementation:
explicit Node(int value = 0): nX(value), next(nullptr), head(nullptr) {}Note regarding class API design:
You are implementing both a node (for an element of the list) and a list of nodes. You should have two classes instead of one, and the
head pointer should be a member of the list, not a node:class Node {
int nX; Node* next;
public:
// ... rest of node interface here
};
class List {
Node *head;
public:
List() {}
const std::size_t size() const;
void add_value_end(int value);
// ... rest of interface here
};Eventually, you should consider using node as an internal structure of the list:
class List {
class Node { /* same as above */ };
Node *head;
public:
// same as above
};Code Snippets
using namespace std;class Node
{
public:
int nX; // (1)
Node *next; // (2)
Node(){ head = NULL; } // (3)Node n;
n.nX = 1001; // is this valid?
n.next = n; // is this valid?explicit Node(int value = 0): nX(value), next(nullptr), head(nullptr) {}class Node {
int nX; Node* next;
public:
// ... rest of node interface here
};
class List {
Node *head;
public:
List() {}
const std::size_t size() const;
void add_value_end(int value);
// ... rest of interface here
};Context
StackExchange Code Review Q#43595, answer score: 8
Revisions (0)
No revisions yet.