Why do I only output 2 items from my linked list despite creating multiple structures?

Hello,

I am writing a simple program that works as a telephone directory but in a doubly linked list format. I am unsure why I am only able to display 2 items after calling my make list function and showing no data members. But when I call my insert function everything works fine!


The list will now be made
How many records would you like?
2
Enter a name:
1
Enter the phone number: 1
Enter a name:
2
Enter the phone number: 2

The list consists of the following records:

Names     Phone number                  Previous mem addr             next mem addr                                     00000000  013D4370
Names     Phone number                  Previous mem addr             next mem addr                                     013D4370  00000000
Which record would you like to remove?
1 if you want to remove the head
2 for the middle
3 for the tail
4 to insert a new record
5 to find a record
6 Display records
4

Enter a name:
3
3
Enter the phone number: 3

Names     Phone number                  Previous mem addr             next mem addr                                     00000000  013D4370
Names     Phone number                  Previous mem addr             next mem addr                                     013D4370  008C4318
Names     Phone number                  Previous mem addr             next mem addr                 3   


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
// Exercise13.5_4revised.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <list>

using namespace std;

struct TeleType
{
	string name;
	string phoneNo;
	TeleType *nextaddr;
	TeleType *prevaddr;
};

void populate(TeleType *); // function prototype needed by main()
void make_head_tail(TeleType *);
void display(TeleType *); // function prototype needed by main()
void remove_head(TeleType *); //function prototype for main()
void remove_current(TeleType *); //function prototype for main()
void remove_tail(TeleType *); //function prototype for main()
void make_list();
void insert(TeleType *);
void push_front(TeleType *);
void push_back(TeleType *);



TeleType *findRec(string, TeleType *);

TeleType head, current, tail;

TeleType *phead = &head;
TeleType *pcurrent = &current;
TeleType *ptail = &tail;

int main()
{
	int i;
	string search_term;

	cout << "The list will now be made" << endl;

	make_list();

	cout << "\nThe list consists of the following records:\n";

	display(phead); // display the structures

	cout	<< "Which record would you like to remove?" << endl;
	cout	<< "1 if you want to remove the head" << endl;
	cout	<< "2 for the middle " << endl;
	cout	<< "3 for the tail" << endl;
	cout	<< "4 to insert a new record "<< endl;
	cout	<< "5 to find a record" << endl;
	cout	<< "6 Display records" << endl;

	cin >> i;
	cin.ignore();

	cout << endl;

	switch (i)
	{
	case 1:
		remove_head(phead);
		display(phead);
		break;
	case 2:
		remove_current(pcurrent);
		display(phead);
		break;
	case 3:
		remove_tail(pcurrent);
		display(phead);
		break;
	case 4:
		insert(ptail);
		display(phead);
		break;
	case 5:
		cout << "What are you looking for? " << endl;
		getline(cin, search_term);
		cin.ignore();
		cout << "The address matching is " << findRec(search_term, phead) << endl;
		break;
	case 6:
		display(phead);
		break;
	}

	system("pause");

	return 0;
}

void make_head_tail(TeleType *record)
{
	populate(phead);
	head.prevaddr = nullptr;
	head.nextaddr = ptail;

	tail.prevaddr = phead;
	tail.nextaddr = nullptr;
}

void make_list()
{
	//Setting the head of the linked list
	//assign head to a new structure
	//Call populate function to fill in data
	//set nextaddr and prevaddr pointers to null
	//assign tail with head

	int number_of_records = 1;
	cout << "How many records would you like?" << endl;
	cin >> number_of_records;

	//populate head with data
	//assign prev addr for head as a null
	//assign next addr for head as tail;

	make_head_tail(phead);

	for (int i = 0; i < number_of_records - 1; i++)
	{
		insert(ptail);
		//populate(ptail); //call populate function to fill in data members of the structure.
		push_back(pcurrent);
		tail = current;
	}
	//set up a for loop to make records
		//make a new node assigned to current structure
		//call populate function to fill in data members
		//assign a pointer to to current's nextaddr as NULl
		//assign pointer prevaddr of current as the pointer of head.
		//assign tail to current
	
	return;
}

// input a name and phone number
void populate(TeleType *record) // record is a pointer to a
{ // structure of type TeleType

	cout << "Enter a name: " << endl;
	getline(cin, record->name);
	cin.ignore();

	cout << "Enter the phone number: ";
	getline(cin, record->phoneNo);
	cin.ignore();

	return;
}

void insert(TeleType *record)
{
	TeleType *pcurrent = new TeleType; //create a new structure, return an address and assign it to a ptr
											 //new record.
	record->nextaddr = pcurrent; //change the nextaddr pointer to point to the new record

	populate(pcurrent); //populate the members of the new record.

	pcurrent->nextaddr = NULL; //assign new null pointer
	pcurrent->prevaddr = record; //assign new pointer of new record to point to the the old tail structure.

	return;
}

void push_back(TeleType *record)
{
	record->nextaddr = NULL;
	record->prevaddr = ptail;
	ptail->nextaddr = pcurrent;
	tail = current;
	//assign the next addr pointer as null
	//assign the prevaddr pointer to point to the tail
	//assign the pointer of tail for next addr to to point to current.
	//assign tail as current.
}

void display(TeleType *contents) // contents is a pointer to a
{ // structure of type TeleType

	while (contents != NULL) // display until end of linked list
	{
		cout << endl << setiosflags(ios::left)
			<< setw(10) << "Names" << setw(30) << "Phone number" << setw(30) << "Previous mem addr" << setw(30) << "next mem addr"
			<< setw(10) << contents->name
			<< setw(10) << contents->phoneNo
			<< setw(10) << contents->prevaddr
			<< setw(10) << contents->nextaddr;
		contents = contents->nextaddr;
	}
	cout << endl;

	return;
}

void remove_head(TeleType *record)
{
	return;
}

void remove_current(TeleType *record)
{
	return;
}

void remove_tail(TeleType *record)
{

	return;
}

TeleType *findRec(string term, TeleType *contents)
{
	TeleType *pointer_to_address = phead;

	while (contents != NULL)
	{
		if (term == contents->name)
		{
			pointer_to_address = contents;
			break;
		}
		else
		{
			cout << "match not found" << endl;
		}
		contents = contents->nextaddr;
	}

	return pointer_to_address;
}


The problem is on line 169. Instead of setting pcurrent->nextaddr to NULL it needs to be set to record->nextaddr.
Registered users can post here. Sign in or register to post.