Adding Node to Linked List at the Tail

Write your question here.
Why does the program hang when trying to add data to the tail? The program works if you add data to the head. See lines 83 to 95.


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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
*	Chapter 15 Problem 2
*
*	Write a program that adds elements to a linked list in sorted order, rather
*	than at the beginning.
*
*
*
*/

#include <iostream>
#include "c15p2.h"

using namespace std;
// Function Prototypes
bool addHead(LinkedList* list, int* data);
void displayLinkedList(LinkedList* list);
void initializeList(LinkedList* list);

int main()
{
	// Declare and initialize variables
	int x = 0;
	int choice = 0;
	LinkedList linkedList;
	initializeList(&linkedList);

	while (true)
	{
		// Print Menu
		cout << "Menu:" << endl <<
			'\t' << "0. Exit" << endl <<
			'\t' << "1. Add a node " << endl <<
			'\t' << "2. Display node(s) " << endl << endl;

		cout << "Select a number from the Menu: ";
		cin >> choice;

		switch (choice)
		{
		case 0:
			return 0;
		case 1:
			cout << "Please enter an integer: ";
			cin >> x;
			addHead(&linkedList, &x);
			break;
		case 2:
			displayLinkedList(&linkedList);
			break;
		}
	}
}




bool addHead(LinkedList* list, int* data)
{
	Node* node = (Node*)malloc(sizeof(Node));
	if (node == NULL)	// check if malloc functioned properly
	{
		return false;
	}

	// initialize node
	node->x_node = *data;
	node->next = NULL;

	// check for empty list
	if (list->head == NULL)
	{
		list->head = node;
		node->next = NULL;
		return true;
	}
	else if (node->x_node < list->head->x_node)  // check if number belongs at head of list
	{
		node->next = list->head;
		list->head = node;			// put it at head of list
		return true;
	}
	else if (node->x_node > list->current->x_node)	// check for insertion at tail
	{
		cout << "Before values of current: " << list->current << " " << list->current->x_node 
				<< " " << list->current->next << endl;
		cout << "Before values of node: " << node << " " << node->x_node << " " << node->next << endl;
		list->current->next = node;
//		list->tail = node;
//		node->next = NULL;
		cout << "After values of current: " << list->current << " " << list->current->x_node
				<< " " << list->current->next << endl;
		cout << "After values of node: " << node << " " << node->x_node << " " << node->next << endl;
		return true;
	}
	else
	{
		list->current = list->head;
		while (true)
		{
			if (list->current->x_node == node->x_node)
			{
				free(node);
				return false;
			}
			else if (node->x_node < list->current->x_node)
			{
				node->next = list->current->next;
				list->current->next = node;
				return true;
			}
		list->current = list->current->next;
		cout << "list->current: " << list->current << " list->current->next: " << list->current->next << endl;
		}
	}
}


void displayLinkedList(LinkedList* list)
{
	cout << endl << "Linked List \n";
	Node* current = list->head;
	while (current != NULL)
	{
		cout << current->x_node << endl;
		current = current->next;
	}
}


void initializeList(LinkedList* list)
{
	list->head = NULL;
	list->tail = NULL;
	list->current = NULL;
}



Menu:
        0. Exit
        1. Add a node
        2. Display node(s)

Select a number from the Menu: 1
Please enter an integer: 5
Menu:
        0. Exit
        1. Add a node
        2. Display node(s)

Select a number from the Menu: 1
Please enter an integer: 10
Press any key to continue . . .

From casual inspection I'd say the "current" member of a LinkedList is NULL when it shouldn't be. Although it's not exactly clear what current represents, it is clear that you expect it to be non-NULL on line 83 and also fairly clear that it will be NULL.
cire,

"current" is suppose to hold the address of the current node. Similarly, "head" is suppose to hold the address to the first node, and tail is suppose to hold the address of the last node.

Actually, I expected to find a NULL so that I could add the new node after the last node. I changed to code to the following and still it does not drop through the else if.

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
// check for empty list
	if (list->head == NULL)
	{
		list->head = node;
		list->tail = node;  //<- added statement
		cout << "list->head = " << list->head << " list->head->next = " << list->head->next << endl;
		cout << "list->head->x_node = " << list->head->x_node << endl;
		return true;
	}
	else if (node->x_node < list->head->x_node)  // check if number belongs at head of list
	{
		node->next = list->head;
		list->head = node;			// put it at head of list
		return true;
	}
	else if (node->x_node > list->tail->x_node)	// check for insertion at tail
	{
		cout << "Before values of tail: " << list->tail << " " << list->tail->x_node
			<< " " << list->tail->next << endl;
		cout << "Before values of node: " << node << " " << node->x_node << " " << node->next << endl;

		list->tail = node;
		node->next = NULL;

		cout << "After values of tail: " << list->tail << " " << list->tail->x_node
			<< " " << list->tail->next << endl;
		cout << "After values of node: " << node << " " << node->x_node << " " << node->next << endl;
		return true;
	}
In this code:

1
2
		list->tail = node;
		node->next = NULL;


You set tail to point to the new node, but you do not connect the new node to the list.

1
2
3
		list->tail->next = node;
		list->tail = node;
		// node->next = NULL;  node->next is already NULL.  


"current" is suppose to hold the address of the current node.
current should not be a member of LinkedList.
Last edited on
Thank you, Thank you, Thank you. It works. I spent hours trying to figure this thing out. Note that I did not make any changes to current.
Topic archived. No new replies allowed.