-4

I'm taking a data structures course using C++, and we're supposed to implement a simple single linked-list structure.

I have all the other methods figured out, but when I check if the list is empty when it actually is, the program crashes, causing a segmentation fault.

When a list is empty, my linked list structure is supposed to look like:

HEAD -> NULL

Whereas if it is not empty, it should look like:

HEAD -> ...... -> NULL

My isListEmpty() looks like:

bool singleList::isListEmpty(void) {
    return (head->next == NULL);
}

I'm pretty sure the core is dumped when I try to access a null pointer, but I'm not sure where. Any suggestions on where I should look?

Thanks.

--Edited--

Sorry, I wasn't clear enough.

I'm pretty sure head itself is NOT a null pointer, because while checking if my code worked, I worked with something like:

list.list_insert_front(guy1);
list.list_insert_front(guy2);
list.list_remove(guy1);
list.list_remove(guy2);
list.isListEmpty(); //This line causes segmentation fault.
WannabeArchitect
  • 1,058
  • 2
  • 11
  • 22
  • 5
    Have you considered the possibility that `head` might be `NULL`? Anyway, either learn to use a debugger, or provide us with a [mcve]. – YSC Oct 06 '17 at 15:01
  • Are you sure `head` isn't `nullptr` for an empty list? This implies that the default constructor (which presumably constructs an empty list) creates a `head`. Perhaps you meant to check that `head` is `nullptr` for an empty list. – François Andrieux Oct 06 '17 at 15:01
  • Your insert and remove function calls don't ensure that `head` is not null, you may be adding or removing links wrong inside the functions. Change your function to `head == null` to test it. – Confuzing Oct 06 '17 at 15:15

3 Answers3

0

Most of the times, in an empty list head is null, not it'snext.

Look twice at the initalization and decide if you need to check head or it'snext for nullity.

Neo
  • 3,534
  • 2
  • 20
  • 32
  • Thanks for the answer. But we were told to implement it this way so that an element called "head" would always exist, and it would be pointing to the first element of the linked list. – WannabeArchitect Oct 06 '17 at 15:17
  • @정진하 you can have a `head` that is there always, but if there is no first element then `head` is `NULL` – 463035818_is_not_an_ai Oct 06 '17 at 15:20
  • @tobi303 Wow I was so stupid thanks. I'm still learning to use gdb, I guess that should help me ask these questions less. Thanks a lot! – WannabeArchitect Oct 06 '17 at 15:25
0

Head is the pointer which keeps the track of first node of the list. If list is empty then head should point to the null.

When you are trying to access head->next, indirectly you are checking if next of the first node (second node) exists or not.

To find if list is empty or not you need to check if head is NULL.

bool singleList::isListEmpty(void) {
    return (head == NULL);
}
nyemul
  • 71
  • 5
  • Head doesn't have to be an item in the list and can simply be used to keep track of the list, depends on how it is programmed. Although I do agree he needs to check if head is null. – Confuzing Oct 06 '17 at 15:56
  • Yes, head is just an anchor point. In a simplest implementation it is just a pointer for tracking. One can also implement a head node, which is never NULL and holds meta data about the list (like - size, no of nodes, access times, etc) and pointer to first data node. – nyemul Oct 06 '17 at 16:21
0

As far as I understand, even if you're supposed have a separate head node, it will be null every time the list is empty.

It will only point to the first node of the list if the list is not empty.

example:

LIST: A->B->C->...->Z->null In this case your head will point to A. (Rather your HEAD will be A. e.g. head = A)

LIST: null ( head = null)

GK___7
  • 1
  • 2
  • If the head node is separate then usually it is not supposed to be null, at least in my experience. It can be a separate class with data about the list or a unused list item that simply points to the start of the list with next. Null implies that it is simply a pointer to the first item and not a separate head node. – Confuzing Oct 06 '17 at 17:39