C/C++ How to Improve the Design of a Linked List Class in C++?

AI Thread Summary
The discussion revolves around designing a linked list class in C++. The initial class design presents issues, such as requiring a key in the constructor, which prevents the creation of an empty list. To address this, suggestions include creating a second constructor that allows for the initialization of a new list with a key while pointing to an existing list. Additionally, it's recommended to differentiate the parameter name from the class member variable to avoid confusion, enhancing code clarity. The conversation emphasizes the importance of maintainable code through careful naming conventions and suggests using two classes: one for individual nodes and another for the list itself, which can improve the overall design and functionality of the linked list implementation.
Avichal
Messages
294
Reaction score
0
I decided to make a library for some common data structures and I'm facing some design problems.
I wanted to implement linked list using classes in c++.

Here is the sample class:-
Code:
class Linked_List
{
private:
    int key;
    Linked_List* next;
public:
    Linked_List(int key)
    {
        this->key = key;
        this->next = NULL;
    }
    void insert(int key)
    {
    ...
    }
    void delete(int key)
    {
    ...
    }
}

I want the next pointer to point to another Linked_List class. Current class should store key and pointer to next class.
Problem with this design:
1) In constructor I have to give the first key. I can't have a class with no key.
2) When I need to insert a key before the first one, then it involves deleting the "this" pointer but that's not possible.

Any better design for a linked list class? (some standard implementation)?
 
Technology news on Phys.org
Best design I can think of:
Code:
#include <list>
 
I think most of the textbook-type designs I've seen for a linked list in C++ use two classes: one for the individual nodes (data and associated pointer(s)), and one for the list as a whole (containing a pointer to the first node, plus other useful data as desired).

Nevertheless, thinking about your proposed scheme a bit:

1) In constructor I have to give the first key. I can't have a class with no key.

If you don't have a first key, you have an empty list. How do you propose to represent an empty list?

2) When I need to insert a key before the first one, then it involves deleting the "this" pointer but that's not possible.

Create a new list and make it point to (the beginning of) the existing one. The existing list doesn't need to change.
Try writing a second constructor, which takes a key and a LinkedList as parameters, and constructs a new list whose first node contains the given key, and then points to the given LinkedList.
 
Last edited:
Avichal said:
Code:
class Linked_List
{
private:
    int key;
    Linked_List* next;
public:
    Linked_List(int key)
    {
        this->key = key;
        this->next = NULL;
    }
...
 }

I think it's confusing to have the same name 'key' for both the private data and the parameter of the constructor (and your other member functions). If you use different names, then you don't need to invoke the 'this' pointer.

Code:
class Linked_List
{
private:
    int key;
    Linked_List* next;
public:
    Linked_List(int newKey)
    {
        key = newKey;
        next = NULL;
    }
...
 }

A key (pun intended :-p) aspect of writing good, maintainable code is to choose names carefully so that they clearly indicate their purpose and relationships, and don't confuse the reader.
 
Last edited:
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top