Problem in Linked List

Hello Everyone!

Here is what I am working on:
Design your own linked list class to hold a series of integers. Your module main should use a query-controlled loop to prompt the user to enter the actual integer values to insert into the list. The class should have member functions for appending, inserting, and deleting nodes (assume that the list will be arranged in ascending order). It should also have a constructor that initializes the head pointer to NULL and a destructor to destroy the list. Finally, the code should print out the contents of the integers in the list using a member function that traverses the list.

I am having a problem with it though...I can enter one value into the list but when I try and enter another value, the program just halts and the .exe file stops working. Any help that you could provide would be great! Thanks! Here is what I have so far.

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
137
138
139
140
141
142
143
144
145
#include <iostream>
using namespace std;

struct ListNode {
	float value;
	ListNode *next;
};
ListNode *head;

class LinkedList {
public:
	int insertNode(float num);
	int appendNode(float num);
	void deleteNode(float num);
	void destroyList();
	void displayList();
	LinkedList(void) {head = NULL;}
	~LinkedList(void) {destroyList();}
};

int LinkedList::appendNode(float num)
{
	ListNode *newNode, *nodePtr = head;
	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}
	newNode->value = num;
	newNode->next = NULL;
	if(head == NULL) {
		cout << "List was empty - " << newNode->value;
		cout << " is part of list's first node.\n";
		head = newNode;
	}
	else {
		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;
		nodePtr->next = newNode;
	}
	return 0;
}

int LinkedList::insertNode(float num)
{
	struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;

	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}
	newNode->value = num;
	newNode->next = NULL;
	if(head==NULL) {
		cout << "List was empty - " << newNode->value;
		cout << " is part of list's first node.\n";
		head = newNode;
	}
	else {
		while((nodePtr != NULL) && (nodePtr->value < num)) {
			prevNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		if(prevNodePtr==NULL)
			newNode->next = head;
		else 
			newNode->next = nodePtr; prevNodePtr->next = newNode;
	}
	return 0;
}

void LinkedList::deleteNode(float num)
{
	struct ListNode *nodePtr = head, *prevNodePtr = NULL;

	if(head==NULL) {
		cout << "The list was empty!\n";
		return;
	}
	if(head->value == num) {
		head = nodePtr->next;
		delete [] nodePtr;
	}
	else {
		while((nodePtr!= NULL)&&(nodePtr->value != num)) {
			prevNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		if(nodePtr==NULL) 
			cout << "The value " << num << " is not in this list!\n";
		else {
			prevNodePtr->next = nodePtr->next;
			delete [] nodePtr;
		}
	}
}

void LinkedList::destroyList()
{
	struct ListNode *nodePtr = head, *nextNode = nodePtr;

	if(head==NULL) {
		cout << "The list is empty!\n";
		return;
	}
	while (nodePtr != NULL) {
		nextNode = nodePtr->next;
		delete [] nodePtr;
		nodePtr = nextNode;
	}
}

void LinkedList::displayList()
{
	struct ListNode *nodePtr = head;
	if (nodePtr == NULL)
		cout << "The list is empty!\n";
	else {
		while (nodePtr != NULL) {
			cout << nodePtr->value << endl;
			nodePtr = nodePtr->next;
		}
	}
}
	

int main()
{
	int num;
	char answer, choice;
	LinkedList temp;
	do {
		cout << "Please enter a value to put in the list --> ";
		cin >> num;
		temp.insertNode(num);
		cout << endl;
		cout << "Would you like to put another value into your list? ";
		cin >> answer;
	} while(toupper(answer)=='Y');

	cout << "I will now display your list!\n";
	cout << endl;
	temp.displayList();
}
Some simple questions:
1 - Why is the head pointer outside of the LinkList class?
2 - Whay do appendNode and insertNode functions return an int value (which doesn't seem to mean anything)


There is a problem here:

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
int LinkedList::insertNode(float num)
{
	struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;

	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}
	newNode->value = num;
	newNode->next = NULL;
	if(head==NULL) {
		cout << "List was empty - " << newNode->value;
		cout << " is part of list's first node.\n";
		head = newNode;
	}
	else {
		while((nodePtr != NULL) && (nodePtr->value < num)) {
			prevNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
                
		if(prevNodePtr==NULL)
			newNode->next = head;
                //This if statement is not complete. What is head supposed to point to afterwards.
		
              else 
			newNode->next = nodePtr; prevNodePtr->next = newNode;
	}
	return 0;
}
Last edited on
Head is supposed to point to newNode. Can't believe I forgot that but thanks for pointing it out. Thanks for the help!
Topic archived. No new replies allowed.