Removing the head node of a LinkedList efficiently.

Hello all, I'm quite a novice at C++ and have been working on this assignment. I am able to insert integers into the linklist I've made (where the skeleton of the Link and LinkList classes were provided by my professor) and obtain the size of the list with no problem. But now in my removeFirst() function I get an erorr in Xcode that outputs "lldb" which I'm assuming is a translation for "Hey buddy you're trying to access something that is NULL." I also need to return the data that my head node contains. From my implementation of the other methods logically I would think "Oh, so before I delete my node I'll just do node->data into an int variable to return" but that's where the NULL lldb error arises. I'm quite puzzled on how to approach this. I have a book that does LinkedLists in a bit different manner that I can totally follow but my professor wants it this way instead and it's confusing.

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
#include <iostream>
using namespace std;

class Link
{
public:
    int data;
    Link *next;
    Link(int d, Link *n)
    {
        data = d;
        next = n;
    }
};

class LinkList
{
public:
    LinkList()
    {
        first = NULL;
        length = 0;
    }
    void insertFirst(int data);
    void printListz();
    int removeFirst();
    int getSize();
private:
    Link *first;
    int length;
};

void LinkList::insertFirst(int data)
{
    Link  *temp = new Link(data, first); //(I can just use first = new Link(data, first);
    first = temp; // inserts new Link into the LinkList
    length++; // maintain length of list for future purposes
}


int LinkList::removeFirst()
{
    int deletionz;
    Link *del=NULL;
    del = first;
    deletionz = del->data;
    first = del->next;
    delete del;
    length--;
    return deletionz;
}

int LinkList::getSize()
{
    return length; // returns length of LinkList from previous 2 methods
}

void LinkList::printListz()
{
    while(first !=NULL)
    {
        cout << first->data << " ";
        first = first->next;
    }
}

int main()
{
    LinkList something;
    something.insertFirst(3); something.insertFirst(4); something.insertFirst(5);
    cout << "Size of LL is: " << something.getSize() << " units." << endl;
    cout << "Contents of LL from top to bottom order is: ";
    something.printListz(); cout << endl;
    something.removeFirst();
    cout << "Size of LL is now: " << something.getSize() << " units.\n";
    something.printListz(); cout << endl;
    
}
Last edited on
Who wrote the implementation of LinkList::printListz() ?
The problem is that you're not setting LinkList::first to the new head, so when you try to use the list again, LinkList::first points to a node that has already been deleted.
keskiverto I did is there something erroneous going about it in that way? It works fine (at least to me) in the output. helios, so then that seems to be a problem with my insertfirst method then....thank you both for your advice.
Last edited on
"works fine"? As in, "this will print the list three times":
1
2
3
something.printListz(); cout << endl;
something.printListz(); cout << endl;
something.printListz(); cout << endl;

Does it? No.
Topic archived. No new replies allowed.