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
|
#include <iostream>
#include <string>
using namespace std;
class Node //node
{
private:
string firstName;
string lastName;
string address;
string phoneNum;
Node *next;
Node *prev;
friend class DubLinkedList; //so that class DubLinkedList can use variables of class Node
};
class DubLinkedList //doubly linked list
{
private:
Node *head;
Node *tail;
Node *curr;
public:
DubLinkedList(); //constructor
void data(); //function that stores peoples' data
void search_list(); //search the list for node based on last name
};
DubLinkedList::DubLinkedList()
: head(NULL) { }
void DubLinkedList::data()
{
Node *t = new Node;
head = t;
t->firstName = "John";
t->lastName = "Smith";
t->address = "45 Xiao Chan Street, Hightown, Alabama";
t->phoneNum = "2019993456";
Node *u = new Node;
u->firstName = "Samuel";
u->lastName = "Adams";
u->address = "636 Uptown Boogie Ave, Pastorville, California";
u->phoneNum = "5543423948";
u->prev = t;
t->next = u;
Node *v = new Node;
v->firstName = "Frederick";
v->lastName = "Jones";
v->address = "1 Yes Street, Notown, New Mexico";
v->phoneNum = "2329994850";
v->prev = u;
u->next = v;
Node *w = new Node;
w->firstName = "Rigatony";
w->lastName = "Piece-Ah";
w->address = "444 Hallelujah Ave, HolyLand, Nevada";
w->phoneNum = "3442339485";
w->prev = v;
v->next = w;
Node *x = new Node;
x->firstName = "Especial";
x->lastName = "Basura";
x->address = "234 El Nombre Strada, Isla Della Fantasia, Guadalupe";
x->phoneNum = "4859483048";
x->prev = w;
w->next = x;
x->next = NULL;
}
void DubLinkedList::search_list() //function to search node
{
string lName;
cout << "Enter one of these last names.(Smith, Adams, Jones, Piece-Ah, Basura\n";
cout << "to display the data for that person.\n";
cin >> lName;
curr = head;
while (curr->next != NULL) //error Thread 1: EXC_BAD_ACCESS (code=1, address=0x60)
{
curr = curr->next;
if (curr->lastName == lName)
{
cout << "A match was found.\n";
cout << curr->lastName << endl;
cout << curr->firstName << endl;
cout << curr->address << endl;
cout << curr->phoneNum << endl;
}
else
{
cout << "Nothing was found.\n";
}
}
}
int main()
{
DubLinkedList person;
person.search_list();
return 0;
}
|