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

Click For 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:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
6K
Replies
16
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
89
Views
6K