check values if values in side node are null


I need to check and see if my value inside my node is '\0'. Every time I run the program I get stuck in my while loop when I add more than one character. How can I check the data inside my node?

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
 //adds character to the back of the circular linked list
void LinkedList::addBack(char data)
{
	if (head == NULL) {
		head = new ListNode(data);
		back = head;
		head->next = head;
	}
	else
	{
		ListNode *newNode = new ListNode(data);
		this->back = newNode;
		this->head->next = newNode;
		newNode->next = head;
	}
}

//removes the first character and sets to null
char LinkedList::removeFirst()
{
	char temp= '\0';
	// If the list is empty, do nothing
	if (head != NULL) {
		temp = head->data;	//temp is set to data in first node
		head->data = '\0';	//head is data is set to null leaving it empty
		head = head->next;	// head is set to next node
	}
	return temp;	//returned to display
}

void LinkedList::displayFirst()
{
	ListNode *nodePtr = head;   // Start at head of list

	while (nodePtr->next->data != '\0') {	//loops through displaying the first node character, until head is NULL again
		// Print the value in the first node
		cout << removeFirst();

		nodePtr = nodePtr->next; //moves onto the next node to display and remove
	}

}
Last edited on
closed account (48bpfSEw)
If you don't work with a debugger then you should put out your own debug-information:


1
2
3
4
5
6
7
8
9
10
11
12
13
char LinkedList::removeFirst() {

   cout << "debug: enter removeFirst(), head = " << (int) head << endl;

  ...

  for (...) 
    cout << "debug: temp: " << temp << endl;

  ...
  cout << "debug: leave removeFirst" << endl;
  }



the more data you have the more your chance grow to find the bug!


good luck!
Topic archived. No new replies allowed.