help me understand linked list

How do you implement this example code? The comments say what to do, but I'm having problems with the whole node and linked list thing. Please help. I'm a visual learner and need to see.

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//start filename: main.h

#ifndef MAIN_H
#define MAIN_H

#include "p07.h"
#include "WordTracker.h"
#include <string>
#include <iostream>
#include <fstream>

using namespace std;
using namespace P07;

// Tests the WordTracker class. This function is implemented and should
// be used as is.
void TestWordTrackerClass();

// Connects the file stream to the filename and returns true is
// stream is opened successfully, false otherwise.
bool ConnectInputFile(ifstream& fin, const string& filename);

// Reads the file and loads the WordTracker object will all the words.
// It first calls ConnectInputFile() to establish the connection. Catches
// a bad_alloc Exception if any attempt to add a word fails, displays the
// error message in the exception object and exits the application.
void LoadWordTrackerFromFile(WordTracker& wt, const string& filename);

#endif
//end filename:main.h

//filename: main.cpp
#include "main.h"

int main() {
	// Testing the WordTracker class.
	// TestWordTrackerClass();

	WordTracker wt;
	LoadWordTrackerFromFile(wt, "words.txt");

	cout << "Listing contents of words.txt:"
		 << endl
		 << "------------------------------"
		 << endl;
	wt.ListAllWords();
	cout << endl << endl;

	return 0;
}// end main()


void TestWordTrackerClass() {
	WordTracker wt;

	try {
		cout << "Original empty WordTracker:"
			 << endl 
			 << "-------------------------------------------"
			 << endl;
		wt.ListAllWords();

		// Add a few words to the tracker.
		wt.AddWord("Hello");
		wt.AddWord("Hi");
		wt.AddWord("The");
		wt.AddWord("A");
		wt.AddWord("Zoo");

		cout << endl
			 << "After adding 5 words to the tracker:"
			 << endl
			 << "-------------------------------------------"
			 << endl;
		wt.ListAllWords();

		// Get the count of the H list.
		cout << endl
			 << "List of H list ("
			 << wt.GetCountOfWordsStartingWith('H') 
			 << "):"
			 << endl
			 << "-------------------------------------------"
			 << endl;
		wt.ListWordsStartingWith('h');
	
		// Remove the Z list.
		cout << endl
			 << "After removing Z list:"
			 << endl 
			 << "-------------------------------------------"
			 << endl;
		wt.RemoveWordsStartingWith('Z');
		wt.ListAllWords();

		// Make a duplicate copy.
		WordTracker duplicate(wt);
		
		duplicate.AddWord("Duplicate");
		cout << endl
			 << "Duplicate list:"
			 << endl 
			 << "-------------------------------------------"
			 << endl;
		duplicate.ListAllWords();
		cout << endl
			 << "Original list:"
			 << endl;
		wt.ListAllWords();

		// Assign original to duplicate.
		duplicate = wt;

		duplicate.AddWord("Duplicate");
		cout << endl
			 << "Duplicate list after assignment:"
			 << endl 
			 << "-------------------------------------------"
			 << endl;
		duplicate.ListAllWords();
		cout << endl
			 << "Original list:"
			 << endl;
		wt.ListAllWords();
	} catch (bad_alloc ba) {
		cerr << ba.what()
			 << endl;
		exit(1);
	}// end try
	cout << endl << endl;	 
}// end TestWordTrackerClass()



// Tests the WordTracker class. This function is implemented and should
// be used as is.
void TestWordTrackerClass()
;

// Connects the file stream to the filename and returns true is
// stream is opened successfully, false otherwise.
bool ConnectInputFile(ifstream& fin, const string& filename)
{
}

// Reads the file and loads the WordTracker object will all the words.
// It first calls ConnectInputFile() to establish the connection. Catches
// a bad_alloc Exception if any attempt to add a word fails, displays the
// error message in the exception object and exits the application.
void LoadWordTrackerFromFile(WordTracker& wt, const string& filename)
{
}
//end filename: main.cpp

//start filename: WordTracker.h

#ifndef WORDTRACKER_H
#define WORDTRACKER_H

#include <string>
#include "p07.h"
using namespace std;


namespace P07 {

	class WordTracker {
	private:
		// The linked list's item type. Private since it is part of
		// the implementation.
		typedef string ItemType;

		// Struct: Node
		struct Node {
			ItemType item;
			Node* next;

			Node(const ItemType& item = ItemType(), Node* const next = NULL)
				: item(item), next(next)
			{}// end Node()
		};// end Node

		// Struct: WordList
		// Structure holds the word's initial character, a count of the nodes
		// on the list and the actual list of words.
		struct WordList {
			char startsWith;
			uint count;
			Node* head;

			WordList(const char startsWith = 0,
				     const uint count = 0,
					 Node* const head = NULL)
				: startsWith(startsWith), count(count), head(head)
			{}
		};// end WordList

		// Initializes receiving object with a duplicate of the 
		// parameter object.
		void CopyObject(const WordTracker& copyObject);

		// Releases all the memory allocated to the object and sets
		// each WordList element to 0, 0, NULL.
		// Calls ReleaseNodes() for each list in the array.
		void ReleaseObject();

		// Releases all the memory allocated for the nodes on the list.
		// Sets head to NULL.
		void ReleaseNodes(Node*& head);

		// Allocates and populates a Node. Throws a bad_alloc
		// exception if memory allocation fails.
		Node* CreateNode(const ItemType& item = ItemType(), 
			             Node* const next = NULL);

		// Hash function to map a character to an array index.
		// The hash is as follows:
		// uint arrayIndex = ch - 'A'.
		// Returns the arrayIndex that ch maps to. Converts the character
		// to uppercase first.
		uint Hash(const char ch) const;

		// Array of linked lists. Each element holds a linked list with all the
		// words that begin with the same letter. First element corresponds to
		// "A", and last element to "Z". A hashing algorithm will be used to
		// access each linked list.
		WordList words[26];

	public:
		// Initializes each array element to 0, 0, NULL.
		WordTracker();

		// Makes a duplicate of the parameter object. 
		// Calls CopyObject().
		WordTracker(const WordTracker& copyObject);

		// Releases all allocated memory.
		// Calls ReleaseObject();
		~WordTracker();

		// Assigns a duplicate of the parameter object. Checks against 
		// self-assignment first.
		// Calls CopyObject() and ReleaseObject().
		WordTracker operator =(const WordTracker& assignObject);

		// Returns the count of words(nodes) that start with the specified
		// character.
		// Calls Hash() to get the array index of the proper list.
		uint GetCountOfWordsStartingWith(const char startsWith) const;

		// Lists all the words that start with the specified character.
		// Calls Hash() to get the array index of the list to output.
		// Format of output:
		// <newline>
		// <initial character>:
		// <TAB5><word1>::<word2>...::<word10>
		// <TAB5><word11>...::<wordn>
		// <newline>
		void ListWordsStartingWith(const char startsWith) const;

		// Lists all the words from A to Z.
		// Calls ListWordsStartingWith().
		void ListAllWords() const;

		// Removes all the list of words that start with the specified 
		// character and sets the list's head to NULL.
		// Calls Hash() to get the array index of the list to remove and
		// ReleaseNodes() to release the actual list. Resets the WordList
		// members to 0,0,NULL to indicate that list has been removed.
		// Returns the number of words removed.
		uint RemoveWordsStartingWith(const char startWith);

		// Adds a word to the proper list. The word is added to the front
		// of the list.
		// Calls Hash() to get the array index of the list to add to.
		void AddWord(const string& word);

	}; // end WordTracker

}// end namespace P07

#endif

//end filename: WordTracker.h

//start filename: p07.h

#ifndef P07_H
#define P07_H

#include <iomanip>
using namespace std;

namespace P07 {
	typedef unsigned int uint;

	#define TAB(n) setw(n) << " "

}// end namespace
#endif

//end filename: p07.h



I have a file WordTracker.cpp with what I've done so far, but it won't let me include it cause it makes it too long.
Last edited on
Topic archived. No new replies allowed.