Doubly Linked List Program

I had to make a program using a doubly linked list that holds the data of 6 people. The problem I am having is on line 84 there is an error that says bad access and I don't know why. Function search_list() searches for the last name that matches the user input. If the user types in "Adams", then all of the data associated with "Adams" will be printed out.

I don't know what's wrong. I made the curr ptr point to the head so that it will search from the beginning until just before NULL. I think part is right.

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;
}
You have defined a function to store data, but you aren't using it.
Then you should check the condition of the while loop at line 84 and the positioning of the line 86 to get the behaviour you expect.
Last edited on
I thought I was using the data here:
 
 if (curr->lastName == lName)


Nevermind I see what you mean. I didn't call it lol
Here is the fixed code:

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 && lName != curr->lastName)
    {
        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.data();                         //Fack
    person.search_list();
    return 0;
}
Last edited on
Great!
Topic archived. No new replies allowed.