Difficulty with an AddressBook Sorted Linked List

Hi everyone,

I am having trouble with several parts of this program. The gist is that I need to create an address book using a record structure and a sorted linked list. The problem is that I am struggling to understand how to execute the following:

- In the main function, how to locate a specific record to delete/change data elements.
- In the member functions, how to code the enum RelationType ComparedTo() function, so that it compares the parameter to the nodes in the linked list.

Thank you for all of your help.

-precc1390

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
struct NodeType;
enum RelationType {LESS, EQUAL, GREATER};

class SortedType
{
public:
	SortedType();
	~SortedType();
	bool IsFull() const;
	int GetLength() const;
	void MakeEmpty() const;
	NodeType GetItem(NodeType item, bool& found);
	void PutItem(NodeType* item);
	void DeleteItem(NodeType* item);
	void ResetList();
	NodeType GetNextItem();
	void displayAnniversary();
	void displayBirthday();
	RelationType ComparedTo(NodeType* item);

private:
	NodeType* listData;
	int length;
	NodeType* currentPos;
};

typedef struct NodeType
{
	string name;
	string address;
	string dateOfBirth;
	string anniversaryDate;
	NodeType* next;
};

NodeType* record;


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
#include "stdafx.h"
#include "SortedType.h"
#include "AddressBook.h"

SortedType::SortedType()
{
	length = 0;
	listData = NULL;
}

SortedType::~SortedType()
{
	NodeType* tempPtr;

	while (listData != NULL)
	{
		tempPtr = listData;
		listData = listData->next;
		delete tempPtr;
	}
}

bool SortedType::IsFull() const
{
	NodeType* location;

	try
	{
		location = new NodeType;
		delete location;
		return false;
	}

	catch (bad_alloc exception)
	{

		return true;
	}
}

int SortedType::GetLength() const
{
	return length;
}

void SortedType::MakeEmpty() const
{
	NodeType* tempPtr;

	while (listData != NULL)
	{
		tempPtr = listData;
		listData = listData->next;
		delete tempPtr;
	}
	length = 0;
}

NodeType SortedType::GetItem(NodeType item, bool & found)
{
	bool moreToSearch;
	NodeType* location;

	location = listData;
	found = false;
	moreToSearch = (location != NULL);

	while (moreToSearch && !found)
	{
		switch (item.ComparedTo(location->record))
		{
			case GREATER:	location = location->next;
							moreToSearch = (location != NULL);
							break;
			case EQUAL:		found = true;
							item = location->record;
							break;
			case LESS:		moreToSearch = false;
							break;
		}
	}

	return item;
}

void SortedType::PutItem(NodeType* item)
{
	NodeType* newNode;
	NodeType* predLoc;
	NodeType* location;
	bool moreToSearch;

	location = listData;
	predLoc = NULL;
	moreToSearch = (location != NULL);

	while (moreToSearch)
	{
		switch (item.ComparedTo(location->name))
		{
		case GREATER:	predLoc = location;
						location = location->next;
						moreToSearch = (location != NULL);
						break;
		case LESS:		moreToSearch = false;
						break;
		}
	}
}

void SortedType::DeleteItem(NodeType* item)
{
	NodeType* location = listData;
	NodeType* tempLocation;

	if (item.ComparedTo(listData->name) == EQUAL)
	{
		tempLocation = location;
		listData = listData->next;
	}

	else
	{
		while ((item.ComparedTo(location->next)->name) != EQUAL)
			location = location->next;
			tempLocation = location->next;
			location->next = (location->next)->next;
	}

	delete tempLocation;
	length--;
}

void SortedType::ResetList()
{
	currentPos = NULL;
}

NodeType SortedType::GetNextItem()
{
	NodeType* item;

	if (currentPos == NULL)
		currentPos = listData;
	item = currentPos->name;
	currentPos = currentPos->next;
	return item;
}

void SortedType::displayAnniversary()
{
	cout << record.anniversaryDate << endl;
	cout << record.address << endl;
	cout << endl;

	cout << "Dear" << record.name << "," << endl;
	cout << endl;

	cout << "Wishing you and your partner a Happy Anniversary! Have a great day!" << endl;
}

void SortedType::displayBirthday()
{
	cout << record.dateOfBirth << endl;
	cout << record.address << endl;
	cout << endl;

	cout << "Dear" << record.name << "," << endl;
	cout << endl;

	cout << "Wishing you a most happiest of birthdays! Have a wonderful day!" << endl;
}

RelationType SortedType::ComparedTo(NodeType * item)
{
	if (record < item)
		return LESS;
	else if (record > item)
		return GREATER;
	else return EQUAL;
}


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
#include "stdafx.h"
#include "SortedType.h"
#include "AddressBook.h"
#include <string>
#include <iostream>
using namespace std;

int main()
{	
	cout << "Welcome to the Message Greeting program!" << endl;
	cout << "This program creates auto-generated birthday and anniversary greetings, based upon information in an address book of your creation." << endl;
	cout << "Please select one of the following menu options to proceed:" << endl;
	cout << endl;

	menuOptions();
}

void menuOptions()
{
	SortedType list;

	int input;
	int change;
	string nameChange;
	int dateUpdate;
	bool finished = false;
	bool moreToDo = false;

	do
	{
		cout << "What would you like to do?" << endl;
		cout << endl;

		cout << "Enter 1 to add a new entry to the Address Book." << endl;
		cout << "Enter 2 to delete an entry from the Address Book." << endl;
		cout << "Enter 3 to change a name OR date in the Address Book." << endl;
		cout << "Enter 4 to generate a birthday greeting." << endl;
		cout << "Enter 5 to generate an anniversary greeting." << endl;
		cout << "Enter 6 to exit the program." << endl;

		cin >> input;

		switch (input)
		{
			case 1:		cout << "Please enter the first and last name of the new contact: " << endl;
						getline(cin, record->name);

						cout << "Please enter the person's street address now: " << endl;
						getline(cin, record->address);

						cout << "Please enter the person's date of birth in the following format: mm/dd/yyyy" << endl;
						cin >> record->dateOfBirth;

						cout << "Please enter the person's anniversary date in the following format: mm/dd/yyyy" << endl;
						cin >> record->anniversaryDate;

						list.PutItem(record);
						break;

			case 2:		cout << "Which entry are you looking to delete?" << endl;
						//cin >> list.DeleteItem(record); I don't know h
						list.DeleteItem(record);
						break;

			case 3:		
					do
					{
						cout << "What portion of the address record are you looking to change?" << endl;
						cout << "Enter 1 for name." << endl;
						cout << "Enter 2 for address." << endl;
						cout << "Enter 3 for date." << endl;

						cin >> change;

						if (change == 1)
						{
							cout << "Which record would you like to change the name for?" << endl;;
						}
						else if (change == 2)
						{
							cout << "Which record would you like to change the address for?" << endl;
						}
						else if (change == 3)
						{
							do
							{
								cout << "Which date field would you like to change: DOB or Anniversary?" << endl;
								cout << "Enter 4 for DOB." << endl;
								cout << "Enter 5 for anniversary." << endl;

								cin >> dateUpdate;

								if (dateUpdate == 4)
								{
									cout << "Which record would you like to change the DOB?" << endl;
								}

								else if (dateUpdate == 5)
								{
									cout << "Which record would you like to change the anniversary date?" << endl;
								}

								else
								{
									cout << "Sorry. That isn't a valid option. Please try again." << endl;
								}
							} while (moreToDo = false);
						}

						else
						{
							cout << "Sorry. That isn't a valid option. Please try again." << endl;
						}
					} while (finished = false);
					break;

			case 4:		list.displayBirthday();
						break;

			case 5:		list.displayAnniversary();
						break;

			default:	exitProgram();
		}
	} while (input != 6);
}

void exitProgram()
{
	cout << "Thank you very much for using the Message Greeting program!" << endl;
	cout << "We hope to see you again soon! Goodbye!" << endl;

	system("pause");
	EXIT_SUCCESS;
}
Topic archived. No new replies allowed.