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.
/*
* 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"
usingnamespace 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
{
returnfalse;
}
// initialize node
node->x_node = *data;
node->next = NULL;
// check for empty list
if (list->head == NULL)
{
list->head = node;
node->next = NULL;
returntrue;
}
elseif (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
returntrue;
}
elseif (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;
returntrue;
}
else
{
list->current = list->head;
while (true)
{
if (list->current->x_node == node->x_node)
{
free(node);
returnfalse;
}
elseif (node->x_node < list->current->x_node)
{
node->next = list->current->next;
list->current->next = node;
returntrue;
}
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.
"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.