Dec 11, 2017 at 4:13am Dec 11, 2017 at 4:13am UTC
I'm supposed to have a doubly linked. Each node should store a person's first and last names, along with their age. Initialize the list with 5 nodes. It should allow for search with name. It should allow for a name to be removed.
There's a slew of errors that I'm not sure how to correct. I'd just like to get rid of the errors so I can finish it but I'm not sure how. A bunch of identifiers are undefined and "Getline" doesn't match argument list. I'm so confused so any guidance at all is greatly appreciated!
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
#include <iostream>
#include <string>
using namespace std;
typedef string Elem;
class DNode {
private :
Elem elem;
DNode* prev;
DNode* next;
friend class DLinkedList;
};
class DLinkedList { // doubly linked list
public :
DLinkedList();
~DLinkedList();
bool empty() const ;
const Elem& front() const ;
const Elem& back() const ;
void addFront(const Elem& name);
void addBack(const Elem& name);
void removeFront();
void removeBack();
void displayViaAge();
void displayViaName();
private :
DNode* header;
DNode* trailer;
protected :
void add(DNode* v, const Elem& name);
void remove(DNode* v);
};
DLinkedList::DLinkedList() {
header = new DNode;
trailer = new DNode;
header->next = trailer;
trailer->prev = header;
}
void DLinkedList::add(DNode* v, const Elem& name) {
DNode* u = new DNode; u->elem = name;
u->next = v;
u->prev = v->prev;
u->prev->next = u;
v->prev = u;
}
void DLinkedList::addFront(const Elem& name)
{
add(header->next, name);
}
void DLinkedList::addBack(const Elem& name)
{
add(trailer, name);
}
void DLinkedList::remove(DNode* v) {
DNode* u = v->prev;
DNode* w = v->next;
u->next = w;
w->prev = u;
delete v;
}
void DLinkedList::removeFront()
{
remove(header->next);
}
void DLinkedList::removeBack()
{
remove(trailer->prev);
}
bool DLinkedList::empty() const
{
return (header->next == trailer);
}
const Elem& DLinkedList::front() const
{
return header->next->elem;
}
const Elem& DLinkedList::back() const
{
return trailer->prev->elem;
}
void DLinkedList::displayViaAge() {
DNode*temp = header;
while (temp != trailer)
{
if (howold = age)
cout << temp->elem << endl;
temp = temp->next;
}
cout << temp->elem << endl;
DLinkedList::~DLinkedList() {
while (!empty()) removeFront();
delete header;
delete trailer;
}
void DLinkedList::add(DNode* v, const Elem& name) {
DNode* u = new DNode; u->elem = name;
u->next = v;
u->prev = v->prev;
u->prev->next = u;
v->prev = u;
}
void DLinkedList::addFront(const Elem& name)
{
add(header->next, name);
}
void DLinkedList::addBack(const Elem& name)
{
add(trailer, name);
}
void DLinkedList::remove(DNode* v) {
DNode* u = v->prev;
DNode* w = v->next;
u->next = w;
w->prev = u;
delete v;
}
void DLinkedList::removeFront()
{
remove(header->next);
}
void DLinkedList::removeBack()
{
remove(trailer->prev);
}
bool DLinkedList::empty() const
{
return (header->next == trailer);
}
const Elem& DLinkedList::front() const
{
return header->next->elem;
}
const Elem& DLinkedList::back() const
{
return trailer->prev->elem;
}
void DLinkedList::displayViaAge() {
DNode*temp = header;
while (temp != trailer)
{
if (howold = age)
cout << temp->elem << endl;
temp = temp->next;
}
cout << temp->elem << endl;
}
int main() {
char input = 'z' ;
string entry;
int age;
DLinkedList person;
person.addFront("person one" );
person.add("person two" );
person.add("person three" );
person.add("person four" );
person.add("person five" );
cout << "What would you like to do?" << endl;
cout << "Enter 'A' to: Add someone" << endl;
cout << "Enter 'B' to: Remove someone" << endl;
cout << "Enter 'C' to: Search for someone via age" << endl;
cout << "Enter 'D' to: Search for someone via name" << endl;
cout << "Enter 'E' to: Quit" << endl;
while (input != 'f' ) {
cin >> input;
cout << endl;
char input = 'z' ;
while ((input != 'a' ) && (input != 'b' ) && (input != 'c' ) && (input != 'd' ) && (input != 'e' )) {
cout << "Not an option" << endl;
cin >> input;
}
if ((input == 'a' )) {
cout << "Please enter name: " ;
cin.ignore();
getline(cin, entry);
cout << "Please enter age: " ;
cin >> age;
person.addFront(entry);
}
if ((input == 'b' )) {
cout << "Who do you want to delete: " ;
cin.ignore();
getline(cin, entry);
person.removeFront();
}
if ((input == 'c' )) {
cout << "What is the age of the person?: " ;
person.displayViaAge();
}
if ((input == 'd' )) {
cout << "What is the name of the person?: " ;
cin.ignore();
getline(cin, entry);
person.addFront(entry);
}
if ((input == 'e' )) {
return 0;
}
cout << endl;
}
}
Last edited on Dec 11, 2017 at 4:14am Dec 11, 2017 at 4:14am UTC
Dec 11, 2017 at 9:22am Dec 11, 2017 at 9:22am UTC
Last edited on Dec 11, 2017 at 10:30am Dec 11, 2017 at 10:30am UTC
Dec 11, 2017 at 12:05pm Dec 11, 2017 at 12:05pm UTC
@lastchance,
how did you found it ?
Any special tools to compare the codes ?