Simple Program Is Skipping Statements

Hi all, I am trying to write a simple program that reverses the contents of a singly linked list of integers and I would like to be able to see the list before and after the reversal, however, the only output that I get is from the introduction function and then it goes back to a prompt. I am using Linux on Windows 10 Subsystem. I would think that I would also see the cout statement just before the for loop, however, even this is skipped. Any thoughts of what I might be doing wrong here? Thank you for all replies and help.

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
#include <iostream>
#include <cstdlib>
#include "Programming_Project_1_Interface_Node.h"

typedef Node* NodePtr;

void introduction();

void headInsert(int value, NodePtr& listHead);

void reverse(NodePtr& listHead);

int main()
{
    using namespace std;
    int i;         //Loop counter to set up the linked list.
    NodePtr head;  //Head node of the linked list to be reversed
    NodePtr iter;  //Iterator over the linked list

    introduction();

    //Set up the linked list
    for(i = 1; i <= 5; i++)
        headInsert(i, head);

     cout << "List before reversal:";
     for(iter = head; iter != NULL; iter = iter->getLink())
         cout << " " << iter->getData();
     cout << endl;
 
     return (0);
}

void introduction()
{
    using namespace std;
    cout << "\nThis is a program that reverses a singly linked list of integers.\n";
}

void headInsert(int value, NodePtr& listHead)
{
    NodePtr temp = new Node(value, NULL);
    temp->setLink(listHead);
    listHead = temp;
}
 
void reverse(NodePtr& listHead)
{
    //TODO
}
Last edited on
You need to initialize head before using it.
Could you elaborate as to why I need to initialize head before use? I updated my code to reflect this and just before the 5 headInsert() function calls, I set head equal to NULL. This did work for me. However, why would the program skip the cout statement "List before reversal" when head is not initialized. It seems that even had I not correctly set up the linked list, the program should still show all of the cout statements? Right?
Last edited on
If you don't initialize head, the link of the last node (the one that is inserted first) might not end up as a null pointer as you want. This could cause problems later when you try to iterate through the list because instead of finding a null pointer and stop, it tries to continue using an invalid pointer that is not pointing to any valid Node object. This is likely to cause the program to crash but it's not guaranteed to. It's what's called undefined behaviour.

cout is buffered, meaning things might not be outputted right away. Forcing everything written so far to be outputted is called flushing and can be done using std::endl or std::flush. It also happens before using cin and when the program terminates successfully.

If the program crashes things that has not been flushed are usually lost and will not be outputted, so if your program crashes on line 27-28 it's perfectly possible that what you have on line 26 will not appear on the screen, but I would expect the crash to generate some kind of error message.
Last edited on
Thank you very much for your explanation. It also clears up some other questions I had about the output that I was getting from valgrind.
Topic archived. No new replies allowed.