Linked list in a linked list question

Hello,

I want to implement a <<singly linked list in a singly linked list>> data structure. I coded my singly linked list but I find it difficult to make each node of the list behaving like a linked list itself. My node class code is given below (I have made everything public to avoid getters and setters):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class node {  // creates the nodes
public:
    int key;
    int data;
    node* next;
    node(){         /
        key = 0;
        data = 0;
        next = NULL;
    }
    node(int k, int d) {    
        key = k;
        data = d;
        next = NULL;
    }

};


and a second class to build the linked list by connecting and manipulationg the nodes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class singlylinkedlist {   
public:
    node* head;   
    singlylinkedlist() {   
        head = NULL;   
    }
    singlylinkedlist(node* n) {   
        head = n;    
    }
    node* nodeExists(int k) {  
        .
        .
        .
    }
    void appendnode(node* n) {  
        .
        .
        .
    }

    .
    .
    .
};



One thought of mine is to create one more class (class data) and set calss data in the class node (instead of int data),and to write a code similar to the code of the class singlylinkedlist but this time it is going to manipulate the class data similar to what class singlylinkedlist does for the class nodes, and therefore to create a list of data within each node.

Does this sound ok or is it nonsense? Any opinions are welcome..
Last edited on
You should use a template. Here's a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>

template <typename T>
struct Node
{
    T     data;
    Node* next;

    Node(const T& d, Node* n = nullptr) : data(d), next(n) { }
};

template <typename T>
class SinglyLinkedList
{
    Node<T>* head = nullptr;
    Node<T>* tail = nullptr;

public:
    ~SinglyLinkedList()
    {
        for (auto node = head; node; )
        {
            auto xnode = node;
            node = node->next;
            delete xnode;
        }
    }

    void append(const T& data)
    {
        auto node = new Node<T>(data);
        (tail ? tail->next : head) = node;
        tail = node;
    }
    
    friend std::ostream& operator<<(std::ostream& out, const SinglyLinkedList& list)
    {
        for (auto node = list.head; node; node = node->next)
            out << node->data << '\n';
        return out;
    }
};

int main()
{
    SinglyLinkedList<int> list[4];
    for (int n: {1, 2, 3, 4, 5})  list[0].append(n);
    for (int n: {6, 7, 8})        list[1].append(n);
    for (int n: {9, 10})          list[2].append(n);
    for (int n: {11, 12, 13, 14}) list[3].append(n);

    SinglyLinkedList<SinglyLinkedList<int>> list_of_lists;
    for (int i = 0; i < 4; ++i) list_of_lists.append(list[i]);

    std::cout << list_of_lists;
}

Deep copy if you have a list of lists?
Yes. I should have said "incomplete example" instead of just "simple example".
So:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    SinglyLinkedList() = default;

    SinglyLinkedList(const SinglyLinkedList& s)
    {
        for (auto node = s.head; node; node = node->next)
            append(node->data);
    }

    SinglyLinkedList(SinglyLinkedList&& s)
    {
        head = s.head;
        tail = s.tail;
        s.head = s.tail = nullptr;
    }

Last edited on
@dutch: Thanks a lot! It didn't cross my mind at all to use templates! I will do it this way. Thanks again!
Topic archived. No new replies allowed.