dereferencing NULL pointer in queue

My program crashes when the show function is called for the second time. I'm pretty sure its an issue of the NULL pointer being derefrenced. The problem is, I am not sure of a way around this without losing the first element of the array.

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  //main for Que Final

#include<iostream>
#include<string.h>
#include<conio.h>
#include<cassert>


using namespace std;

class Queue
{
	private:
		struct Node
		{
			string data;
			Node *next;
		};
	Node *head;
	Node *tail;
		
	public:
		Queue(){head = NULL; tail = NULL;};
		~Queue();
		void push(string);
		void pop();
		void show();
};


int main()
{ 
   char usrp;
   Queue myQ;
   string name;   
   
   do
   {
   	    system("CLS");
        cout << "\n\n";

   	    myQ.show();
        cout << endl;
   	    cout << "     (1) enQueue" << endl;
   	    cout << "     (2) deQueue" << endl;
   	    cout << "     (3) to quit" << endl;
   	    cout << "\n\n Enter: ";
   	    usrp = getch();

       if (usrp == '1')
       {
       		cout << "\n  Enter Name: ";
       		cin >> name;
       		if (name != "")
       			myQ.push(name);
	   }
       else if (usrp == '2')
          	myQ.pop();
      
   } while (usrp != '3');
}

Queue::~Queue()
{
	Node *walker = head;
	
	while(head)
	{
		walker = walker->next;
		delete head;
		head = walker;
	}
}

void Queue::push(string d)
{

	if(!head)
	{	
		Node *newNode= new Node;
		newNode -> next = NULL;
		newNode -> data = d;
		head = newNode;
		tail = newNode;
	}
	
	else
	{
		Node *newNode= new Node;
		tail -> next = newNode;
		tail = newNode;
		newNode -> data = d;
	}
}

void Queue::pop()
{
	Node *walker = head;
	
	if(head)
	{
		head = head -> next;
		delete walker;
		walker = head;
	}
}

void Queue::show()
{
	if(head)
	{
		Node *test;
		Node *walker = head;
		while(walker) 
			{	
				cout<< walker -> data << endl;
				
				walker = walker -> next;
			}
	}
Before you derefrence a pointer, you could check if it is a null pointer with something like:
if (myPointer != nullptr) /*do something with it*/ ;

That should help you avoid the problem and hopefully locate it so that you can prevent trying to use a nullpointer.
I may have phrased that wrong, but it is in the while loop of Queue::show(). I know exactly where it is happening, and i can avoid it by saying while(walker != tail) but it doesn't display the first element in the queue until another is added into it, then it doesn't display the second.

at some point next is pointing to NULL, and the pointer operator is dereferencing it. That's what im trying to avoid without the program crashing.
Instead of while(walker != tail), try while(walker != nullptr).

If the que is empty walker will instantly be null because head is null.
If there is/are item(s) in the que, walker will become null after the data of the last item has been output and walker was set to its next.

The show() function seems to work fine. I think the problem is that you forget to set the next pointer to null when you create a new node.
That is tested with the if(head) statement. if there Isn't a head it completely passes all of it. I'll give it a try though. Thank you.
Peter87, marvelous. That was exactly the problem. Thank you man. Much appreciation.
Topic archived. No new replies allowed.