Linked list

I am working on writing a linked list and I'm just going off the concept of it for practice.

Does this look okay? I'm doing this for the purpose of learning and testing my knowledge.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>

using namespace std;

class List{
public:
    List();
    void push_back(int value);
    void push_front(int value);
    void pop_back();
    void pop_front();
    void print();


private:
    struct node{
        int data;
        node * next;
    };

    node * head;
    node * current;
    node *temp;

};

List::List()
{
    head = 0;
    current = 0;
    temp = 0;
}

void List::push_back(int value)
{
    node *n = new node;
    n->data = value;
    n->next = 0;

    if(head == 0)
    {
        head = n;
    }
    else{
        current = head;
        while(current->next != 0)
        {
            current = current->next;
        }
        current->next = n;
    }
}

void List::print()
{
    if(head == 0)
    {
        cout << "List is empty!" << endl;
    }
    else{
    current = head;
    while(current->next != 0)
    {
        cout << current->data << " ";
        current = current->next;
    }
    cout << current->data << " ";
    }
}

int main()
{
    List object;
    object.print();
    object.push_back(7);
    object.push_back(8);
    object.push_back(9);
    object.print();

}
Last edited on
The push_back does the job. You'll want to add insertion and deletion methods and some more constructors for basic linked list functionality.

Some issues:
You're not deleting nodes. You should write a destructor which does this (should also do it in your future insertion method).
You're not handling the situation where new fails.
You should use nullptr instead of 0 in modern C++.
Indention is not right on lines 61-67.
Keep your brace style consistent.
Don't include all of the std namespace at the global level.

Those should keep you going for a bit. Once you get a little more comfortable you should try to generalize the list to use any data type, not just int.
Thanks for the feedback resident.

I do have one question though and how should I implement the destructor?

1
2
3
4
5
6
List::~List()
{

     //delete node; I know this won't compile.

}


After I finish this, I'll take a stab at using a template class to handle more data types.
Last edited on
The destructor's job should be to delete all the Nodes in the Linked List. For that, you'd have to iterate over the list, and save a pointer to the subsequent node before deleting the current one.
If you delete a node without saving its next pointer, you'll lose the rest of the list (and create a memory leak).

I created some videos for LinkedLists. Although they don't show how to delete a node (I might do that in the future), I still hope you'll find them useful.

https://www.youtube.com/watch?v=BrWXMfwmiQs&list=PLUTIHsaaB222qkK5_glKM1vzWS60O4VCi


Last edited on
Thanks little. I looked at them.

So I tried this as my destructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
List::~List()
{
    current = head;
    if(current->next == 0)
    {
        delete current;
    }
    else{
        while(current->next != 0)
        {
            temp = current->next;
            delete current;
            current = temp;
        }
        delete current;
    }

}


my only concern here is if I have to check if the list is empty initially? I'm assuming that if the current nodes next node is not pointing to 0 then the list is empty. I'm just not sure if my assumption here is correct.
Last edited on
Check if the head pointer points at a node or not. That's the way to determine if a list is empty or otherwise.

If you check the next pointer, then you're by default, assuming that there is a Node in the list with a next pointer to check.

So, to do that, do this:

1
2
3
4
if(current == nullptr) //Check if we are pointing at a head node or not.
{
    return; //If there is not even a head node, the list is empty and we return
}
Last edited on
Topic archived. No new replies allowed.