Help with Linked Lists - C++

I'm trying to build a banking program with the capability to create new accounts (savings or checking), process a deposit, process a withdrawal, or print one or all of the accounts at this "bank". I am doing this utilizing two classes, one for 'Checking' and one for 'Savings'.

I've more or less written all of the code needed but I could use some help utilizing linked lists in a few spots that are commented within my 'main' function where I am needing to search through my classes and then send the proper 'Savings' or 'Checking' variable to my class functions. I've had issues implementing linked lists for classes and my textbook along with online sources have not helped me understand proper usage of them.

Thanks in advance for any assistance.

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
#include <iostream>

using namespace std;

class Savings
{
    private:
        string FName;
        string LName;
        string Address;
        int SSN;
        double TotBalance;

    public:
        Savings SavingsCreate(Savings *a);
        Savings SavingsDep(Savings *b);
        Savings SavingsWit(Savings *c);
        Savings SavingsPrint(Savings *e);
};

Savings Savings::SavingsCreate(Savings *a)
{
    cout << endl;
    cout << "Enter your first name: ";
    cin >> a->FName;
    cout << endl << endl;
    cout << "Enter your last name: ";
    cin >> a->LName;
    cout << endl << endl;
    cout << "Enter your street address: ";
    cin >> a->Address;
    cout << endl << endl;
    cout << "Please enter your Soc. Sec. # (123456789 Format): ";
    cin >> a->SSN;
    cout << endl << endl;
}

Savings Savings::SavingsDep(Savings *b)
{
    double deposit;

    cout << endl;
    cout << "Deposit Amount: ";
    cin >> deposit;

    b->TotBalance += deposit;

    cout << endl;
    cout << "Current Balance: $" << b->TotBalance;
    cout << endl;
}

Savings Savings::SavingsWit(Savings *c)
{
    double withdraw;

    cout << endl;
    cout << "Withdraw Amount: ";
    cin >> withdraw;

    c->TotBalance -= withdraw;

    cout << endl;
    cout << "Current Balance: $" << c->TotBalance;
    cout << endl;
}

Savings Savings::SavingsPrint(Savings *e)
{
   cout << endl;
    cout << "Name: " << e->FName << " " << e->LName << endl;
    cout << "Address: " << e->Address << endl;
    cout << "Soc. Sec. #: " << e->SSN << endl;
    cout << "Account Type: Savings\n";
    cout << "Interest Rate: 1%\n";
    cout << "Total Balance: $" << e->TotBalance << endl;
    cout << "Interest Gained: $" << e->TotBalance * .01 << endl;
    cout << "Total Balance with Interest Gained: $" << e->TotBalance + (e->TotBalance * .01) << endl;
    cout << endl;
}

class Checking
{
    private:
        string FName;
        string LName;
        string Address;
        int SSN;
        double TotBalance;

    public:
        Checking CheckingCreate(Checking *a);
        Checking CheckingDep(Checking *b);
        Checking CheckingWit(Checking *c);
        Checking CheckingPrint(Checking *e);
};

Checking Checking::CheckingCreate(Checking *a)
{
    cout << endl;
    cout << "Enter your first name: ";
    cin >> a->FName;
    cout << endl << endl;
    cout << "Enter your last name: ";
    cin >> a->LName;
    cout << endl << endl;
    cout << "Enter your street address: ";
    cin >> a->Address;
    cout << endl << endl;
    cout << "Please enter your Social Security Number (123456789 Format): ";
    cin >> a->SSN;
    cout << endl << endl;
}

Checking Checking::CheckingDep(Checking *b)
{
    double deposit;

    cout << endl;
    cout << "Deposit Amount: ";
    cin >> deposit;

    b->TotBalance += deposit;

    cout << endl;
    cout << "Current Balance: $" << b->TotBalance;
    cout << endl;
}

Checking Checking::CheckingWit(Checking *c)
{
    double withdraw;

    cout << endl;
    cout << "Withdraw Amount: ";
    cin >> withdraw;

    c->TotBalance -= withdraw;

    cout << endl;
    cout << "Current Balance: $" << c->TotBalance;
    cout << endl;
}

Checking Checking::CheckingPrint(Checking *e)
{
    cout << endl;
    cout << "Name: " << e->FName << " " << e->LName << endl;
    cout << "Address: " << e->Address << endl;
    cout << "Soc. Sec. #: " << e->SSN << endl;
    cout << "Account Type: Checking\n";
    cout << "Interest Rate: 0%\n";
    cout << "Total Balance: " << e->TotBalance << endl;
    cout << endl;
}
Last edited on
code continued.

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
int main()
{
    int choice, choice_1, choice_2, choice_3, choice_4;
    int SSN_temp;

    do
    {
        cout << "---------------------------------------------\n";
        cout << "Welcome to the First National Bank\n";
        cout << "Account transaction system\n";
        cout << "---------------------------------------------\n";
        cout << "Please choose from the following menu:\n";
        cout << "       1) Create Account\n";
        cout << "       2) Deposit\n";
        cout << "       3) Withdraw\n";
        cout << "       4) Print Account(s)\n";
        cout << endl;
        cout << "       0) Exit Program\n";
        cout << endl;
        cout << ":";
        cin >> choice;

        switch (choice)
        {
            case 1:
                {
                    cout << endl;
                    cout << "Create a Savings or Checking Account?: \n";
                    cout << "       1) Savings Account - 1% Interest Rate\n";
                    cout << "       2) Checking Account - 0% Interest Rate\n";
                    cout << endl;
                    cout << ":";
                    cin >> choice_1;

                    //Search through linked list for checking or savings classes until NULL is found
                    // and create new variable for 'Savings' or 'Checking' class.

                    switch (choice_1)
                    {
                        case 1:
                            {
                                //"new 'Savings'".SavingsCreate(Savings &"new");
                                break;
                            }
                        case 2:
                            {
                                //"new 'Checking'".CheckingCreate(Checking &"new");
                                break;
                            }
                    }
                    break;
                }
            case 2:
                {
                    cout << endl;
                    cout << "Deposting to Savings or Checking Account?:\n";
                    cout << "       1) Savings Account\n";
                    cout << "       2) Checking Account\n";
                    cout << endl;
                    cout << ":";
                    cin >> choice_2;

                    switch (choice_2)
                    {
                        case 1:
                            {
                                cout << "Please enter your soc. sec. #:\n";
                                cout << endl;
                                cout << ":";
                                cin >> SSN_temp;

                                //Search through linked list for 'Savings' containing matching SSN

                                //"current 'Savings'".SavingsDep(Savings &"current");

                                break;
                            }
                        case 2:
                            {
                                cout << "Please enter your soc. sec. #:\n";
                                cout << endl;
                                cout << ":";
                                cin >> SSN_temp;

                                //Search through linked list for 'Checking' containing matching SSN

                                //"current 'Checking'".CheckingDep(Checking &"current");

                                break;
                            }
                    }
                    break;
                }
            case 3:
                {
                    cout << endl;
                    cout << "Withdrawing from Savings or Checking Account?:\n";
                    cout << "       1) Savings Account\n";
                    cout << "       2) Checking Account\n";
                    cout << endl;
                    cout << ":";
                    cin >> choice_3;

                    switch (choice_3)
                    {
                        case 1:
                            {
                                cout << "Please enter your soc. sec. #:\n";
                                cout << endl;
                                cout << ":";
                                cin >> SSN_temp;

                                //Search through linked list for 'Savings' containing matching SSN

                                //"current 'Savings'".SavingsWit(Savings &"current");

                                break;
                            }
                        case 2:
                            {
                                cout << "Please enter your soc. sec. #:\n";
                                cout << endl;
                                cout << ":";
                                cin >> SSN_temp;

                                //Search through linked list for 'Checking' containing matching SSN

                                //"current 'Checking'".CheckingWit(Checking &"current");

                                break;
                            }
                    }
                    break;
                }

            case 4:
                {
                    cout << endl;
                    cout << "Print Savings Acc., Checking Acc., or all:\n";
                    cout << "       1) Savings Account\n";
                    cout << "       2) Checking Account\n";
                    cout << "       3) All\n";
                    cout << endl;
                    cout << ":";
                    cin >> choice_4;

                    switch (choice_4)
                    {
                        case 1:
                            {
                                cout << "Please enter your soc. sec. #:\n";
                                cout << endl;
                                cout << ":";
                                cin >> SSN_temp;

                                //Search through linked list for 'Savings' containing matching SSN

                                //"current 'Savings'".SavingsPrint(Savings &"current");

                                break;
                            }
                        case 2:
                            {
                                cout << "Please enter your soc. sec. #:\n";
                                cout << endl;
                                cout << ":";
                                cin >> SSN_temp;

                                //Search through linked list for 'Checking' containing matching SSN

                                //"current 'Checking'".CheckingPrint(Checking &"current");

                                break;
                            }
                        case 3:
                            {
                                cout << "Savings Accounts:\n";

                                //Run through linked list for all 'Savings'

                                //"current 'Savings'".SavingsPrint(Savings &"current");

                                cout << endl;
                                cout << "Checking Accounts:\n";

                                //Run through linked list for all 'Checking'

                                //"current 'Checking'".CheckingPrint(Checking &"current");

                                break;
                            }
                    }
                }
        }
    } while (choice != 0);
}
Here is a link to some code I wrote to help some one else with a similar thing. It isn't complete, and some of the return types aren't right because this is for an assignment.

http://www.cplusplus.com/forum/beginner/76482/4/#msg412946


The main idea with my code is to have 2 classes, 1 for the Account itself, and a Bank class that deals with Account objects.

It is different from your situation because it does not handle withdrawals & deposits. However it shouldn't be hard to implement that

I don't think you should have classes for different types of accounts, the account type should just be a member variable.

Your classes are circular because they have members that are of the same type. Objects should be created in main, not inside the class.

The other thing to understand about classes is the concept of encapsulation. The functionality of a class should be contained in the class. You will see in my code how little there is in main().

You are welcome to use my code as a starting point, IMO that might be better than continuing with your code. I am sorry to pour cold water all over your creation, but don't worry - it won't be the first time that it has happened to anyone.

Hope all goes well, I look forward to helping out further if needed.
Last edited on
Thanks, will check this out.
Are you using a generic std::list or implementing your own templated linked list? The std::list can do the trick but it will waste memory because it's actually implemented as a doubly linked list.

A much more efficient approach may be to use a std::map where a key/value pair would eliminate the need for traversing the list to find the matching Savings data. You could use the SSN as the key to access the Savings data and go from O(n) to O(1) access times. Just a suggestion.
Topic archived. No new replies allowed.