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

Click For Summary

Discussion Overview

The discussion focuses on the design of a linked list class in C++, addressing challenges and proposing improvements. It encompasses technical explanations and design considerations related to data structure implementation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a basic implementation of a linked list class but identifies issues with requiring a key in the constructor and challenges with inserting a key before the first node.
  • Another participant suggests using the C++ standard library's list class as an alternative design.
  • A third participant proposes a two-class design, one for nodes and another for the list, and questions how to represent an empty list if a key is mandatory in the constructor.
  • This participant also recommends creating a new list to insert a key before the first node instead of modifying the existing one.
  • Another participant advises on naming conventions, suggesting that using different names for the private data and constructor parameters can improve code clarity and maintainability.

Areas of Agreement / Disagreement

Participants express differing views on the best design approach for the linked list class, with no consensus reached on a single solution. Various suggestions and critiques are presented without agreement on a definitive design.

Contextual Notes

Participants highlight limitations in the current design, such as the inability to represent an empty list and the potential confusion caused by naming conventions. These issues remain unresolved within the discussion.

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:

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · 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
89
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K