Head, current, and tail are still list items. They should be pointers. Otherwise how do you represent an empty list?
Remove_head(), remove_current() and remove_tail() shouldn't need parameters since the name implies exactly which item will be removed.
Line 149: You have the right idea here. You need to link ptail->nextaddr to something, but what? Also, what if the list is empty?
Display() prints a header for each item. It should print the header once and then print the individual items. Also the header is padded to 30 characters but the items are padded to 20 in some fields.
A more object-oriented way to handle this would be to have a display() method in the Teletype class and then use it inside your display() function. An even better way would be overload the << operator so it can take a TeleType object as the second parameter, but you might not have learned about overloaded operators so I'll skip that.
note that cin.ignore() will ignore only one character. You probably want to ignore up to the next newline. This problem occurs in several places.
Here is a version that will work if you fill in the code with the "INSERT CODE HERE" comments.
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
|
// 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;
};
TeleType *phead = nullptr;
TeleType *ptail = nullptr;
TeleType *pcurrent=nullptr;
void populate(TeleType *); // prompt for values and populate it
void display(); // print the list
void remove(TeleType *); // remove and delete the item
void make_list();
void push_front(TeleType *); // insert item at front of list
void push_back(TeleType *); // insert item at tail of list
TeleType *findRec(const string &);
void remove_head() {
remove(phead);
};
void remove_tail() {
remove(ptail);
}
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(); // display the structures
bool done(false);
do {
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;
cout << "7 Quit" << endl;
cin >> i;
if (!cin) break;
cin.ignore(1000000, '\n');
cout << "i is " << i << endl;
switch (i) {
case 1:
if (pcurrent == phead) pcurrent = nullptr;
remove_head();
display();
break;
case 2:
display();
break;
case 3:
if (pcurrent == ptail) pcurrent = nullptr;
remove_tail();
display();
break;
case 4:
{
TeleType *rec = new TeleType;
populate(rec);
push_back(rec);
display();
break;
}
case 5:
cout << "What are you looking for? " << endl;
getline(cin, search_term);
pcurrent = findRec(search_term);
cout << "The address matching is " << pcurrent << endl;
break;
case 6:
display();
break;
case 7:
done = true;
break;
}
} while (!done);
// system("pause");
return 0;
}
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;
cin.ignore(1000000, '\n');
for (int i = 0; i < number_of_records; i++) {
TeleType *rec = new TeleType;
populate(rec);
push_back(rec);
}
}
// 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);
cout << "Enter the phone number: ";
getline(cin, record->phoneNo);
}
// Display the list
void
display()
{
cout << endl << setiosflags(ios::left);
cout << setw(30) << "Names"
<< setw(20) << "Phone number"
<< setw(20) << "Previous mem addr"
<< setw(20) << "next mem addr";
for (TeleType *p = phead; p; p = p->nextaddr) {
cout << '\n';
cout << setw(30) << p->name
<< setw(20) << p->phoneNo
<< setw(20) << p->prevaddr
<< setw(20) << p->nextaddr;
}
cout << '\n';
}
void
push_back(TeleType * record)
{
// INSERT CODE HERE
}
void push_front(TeleType *record)
{
// INSERT CODE HERE
}
// Remove and destroy the record, which must be in the list
void
remove(TeleType * record)
{
// INSERT CODE HERE
}
TeleType *
findRec(const string &term)
{
for (TeleType *rec = phead; rec; rec = rec->nextaddr) {
if (term == rec->name) {
return rec;
}
}
return nullptr;
}
|