C++ Linked List Sort a List as It's Being Created?

This is part of the header

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
class LinkedList 
{ 
public: 
struct ListNode 
{ 
public: 
ListNode( const string& theString, ListNode* pNext = NULL ) 
{ 
word = theString; 
pNextNode = pNext; 
} 

string word; 
ListNode* pNextNode; 
}; 

public: 
LinkedList();//default constructor, written 
~LinkedList();//destructor, written 

//Given a word, place it in alphabetical position, in the Linked List
void addWord( const string& theWord );//create new node w/ string, add to linked list, help 
//Pulls words from a file one by one and passes them to addWord
void addWords( const string& filename );//Not written, but will call addWord in a loop 
//remove the first instance of a word in the linked list
void removeWord( const string& theWord );//Not written, plan to do myself 
//remove every instance of a given word in the linked list
void removeAllInstancesOfWord( const string& theWord );//Not written, plan to do myself 
//Remove all nodes and cleanup after them
void clear();// Written 

void printReport( ostream& out ) const; 

private: 
ListNode* mpHead; 

void removeEnd();//Written 
};


this is the function I'm having trouble with

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
void LinkedList::addWord( const string& theWord )
{
	ListNode* pCurr = mpHead;
	ListNode* pPrev = NULL;

	while(pCurr->pNextNode != NULL)
	{
		if(pCurr->word > theWord)
		{
			if(!pCurr->pNextNode)
			{
				ListNode* node = new ListNode(theWord);
					
				pCurr->pNextNode = node;
			}
			else
			{
				pCurr = pCurr->pNextNode;


			}
		}
		
		pPrev = pCurr;
		pCurr = pCurr->pNextNode;
	}
}


This is the cpp that I've been working on

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "LinkedList.h"
#include <fstream>
#include <sstream>
#include <iostream>

using namespace std;

LinkedList::LinkedList()
{
	mpHead = NULL;
}

LinkedList::~LinkedList()
{
	clear();
}

//add a word to the list in the proper position alphabetically
void LinkedList::addWord( const string& theWord )
{
	ListNode* pCurr = mpHead;

	if(mpHead)
	{	
		while(pCurr->pNextNode != NULL)
			pCurr = pCurr->pNextNode;

		ListNode* newNode = pCurr->pNextNode;

		pCurr->pNextNode = newNode;
		newNode->word = theWord;
	}
	else
		mpHead->word = theWord;

	cout << "right before sortList";

	//sortList();
}

//add all words in the file referred to by filename - add them alphabetically
void LinkedList::addWords( const string& filename )
{
	ifstream inFile;
	string temp, temp2;
	ListNode* pCurr = mpHead;

	while(!inFile.eof())
	{
		inFile >> temp;

		if(!mpHead)
			pCurr->word = temp;

		while(pCurr->pNextNode != NULL)
			pCurr = pCurr->pNextNode;

		ListNode* newNode = pCurr->pNextNode;
		newNode->word = temp;
	}

	sortList();
}

//remove the first instance of theWord in the list (and delete it)
void LinkedList::removeWord( const string& theWord )
{
	ListNode* pCurr = mpHead;
	ListNode* pPrev = NULL;

	if(!pCurr)
	{
		pCurr = mpHead->pNextNode;
	}
	else
	
	while( pCurr != NULL )
	{
		if(pCurr->word == theWord)
		{
			pPrev->pNextNode = pCurr->pNextNode;

			if(!pCurr->pNextNode)
			{
				pPrev->pNextNode = NULL;

				delete pCurr;
			}

			break;
		}

		pPrev = pCurr;
		pCurr = pCurr->pNextNode;
	}
}

//remove all instances of theWord in the list (and delete them)
void LinkedList::removeAllInstancesOfWord( const string& theWord )
{
	bool found = false;
	ListNode* pCurr = mpHead;


	while( pCurr != NULL )
	{
		if(pCurr->word == theWord)
			removeWord(theWord);
		
		pCurr = pCurr->pNextNode;
	}
}

//remove all words from the list (and delete them)
void LinkedList::clear()
{
	while(mpHead)
	{
		removeEnd();
	}
}

void LinkedList::printReport( ostream& out ) const
{
	ListNode* pCurrent = mpHead;
	out << "List contains: ";
	while( pCurrent != NULL )
	{
		out << pCurrent->word << " ";
		pCurrent = pCurrent->pNextNode;
	}

	out << " EOL\n";
}

void LinkedList::removeEnd()
{
	if ( mpHead )
    {
       ListNode* pCurr = mpHead;
       ListNode* pPrevPtr = NULL;

       while ( pCurr->pNextNode != NULL)
       {
          pPrevPtr = pCurr;
          pCurr = pCurr->pNextNode;
       }

       delete pCurr;

       if ( pPrevPtr ) 
		   pPrevPtr->pNextNode = NULL;

       else 
		   mpHead = pPrevPtr; 
    }
}

void LinkedList::sortList()
{
	bool swap = true,
		 end = false;

	string temp = "";

	ListNode* pCurr = mpHead;

	while(swap)
	{
		while(!end)
		{
			if(pCurr->word < pCurr->pNextNode->word)
			{
				temp = pCurr->word;
				pCurr->word = pCurr->pNextNode->word;
				pCurr->pNextNode->word = temp;
			}
			else if(!pCurr->pNextNode)
				end = true;

			if(temp == "")
				swap = false;

			temp = "";
		}
	}
}
Last edited on
What exactly is the issue you're having?
The fact that I get runtime errors when I call it
Did you try to use a debugger to determine the state of the program and what line it crashes on?
Please explain (add a header comment) to the addword function so that one can understand the logic behind it and provide you with the right fix.

> 3 ListNode* pCurr = mpHead;
Is mpHead always non-NULL here?

> 1 void LinkedList::addWord( const string& theWord )
Is the function complete? I don't think so. For example:

> 8 if(pCurr->word > theWord)
What happens if "theWord" is greater than any of the existing words in the list? Function addWord does not insert(modify) the list?
@ kannanmj

mpHead is only null when the list is first created, that is until the first word is added

addWord is not complete by any means and is the function I need help with

23 if(mpHead)
. .
33 else
34 mpHead->word = theWord;

At line 34, mpHead is NULL and it is being used to access a member of the object (non-existing) it is pointing to. This will lead to run-time error.
Topic archived. No new replies allowed.