Question

In: Computer Science

TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...

TITLE

Updating Accounts Using Doubly Linked List

TOPICS

Doubly Linked List

DESCRIPTION

General

Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list.

The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account id, first name, last name and the current balance. The transaction file will contain updates. Each update will consist of an account id, first name, last name and a plus or minus balance update. A plus update value will indicates a deposit and minus a withdrawal.

Building List

At the beginning, the program will input account data from the master file and build a doubly linked list. Each node in the list will contain data relating to one account and the whole list will be kept sorted by account id.

For building the list, the program will input account information from the master file one account at a time.  It will create a node containing the account data and add it to the list at the appropriate position in the list so that the list will remain sorted in ascending order by account id.

Updating List

After the list is built as described above, the program will read updates from the Update file and update the list accordingly. It will input one account update at a time and update the list using this update before inputting the next update.

For moving from account to account during updates, it will use a global cursor. For each update, it will determine  whether the target account is in the forward or backward direction from the current position. It will then move the cursor in the appropriate direction using either the forward or backward links of the doubly linked list. It will move the cursor till it reaches the target account or an account past the target account (in the case that the target account does not exist).

Depending upon the account id provided in the update, the program will do one of the following:

·       If the account specified in the update is not found in the list, it will create a new account, initialize its fields with the values in the update and insert it in the list at appropriate position so that the list will remain sorted in ascending order by account id.

·       If the account specified in the update is found in the list, it will update its current balance according to the plus or minus value specified in the update (i.e. it will add a plus value and subtract a minus value).

·       On completing the update, if an account balance becomes 0 or negative, the program will delete that account.

Logging Update

Before starting any updates, the program will log the following to the log file:

·       It will log the contents of the whole doubly linked list.

Then for each update, it will log the following information to the log file:

·       Before the update, it will log a line containing the contents of the update to be performed.

·       After the update, it will log the contents of the whole doubly linked list.

For logging the contents of an update, it will output one line of text containing account id, account name and the update value.

For logging the contents of the whole doubly linked list, it will output one line for each account containing account id, account name and account balance.

For the contents of a sample log file, see the Sample Log File section. It contains the partial contents of a sample log file.

New Master File

When all updates are completed, the program will save the contents of the updated linked list to a new master file. (Do not save the link values).

IMPLEMENTATION

Implement the account list using a Circular Doubly Linked List with a Dummy Node.

Account Id

Use a long for account id.

Circular Doubly Linked List With A Dummy Node

Implement the list for keeping the account as a Circular Doubly Linked List with a Dummy Node.

In a Circular Dummy Node implementation of a doubly linked list, the head points to a dummy node. The dummy node looks like any other node but its data fields do not contain any useful values. It’s forward link points to the first node in the list and back link points to the last node in the list. Initially, in an empty list, both its forward and back links points to itself (i.e. to the dummy node).

For doing a sorted insertion in such a list, do the following. Point the insertion pointer to the first real node. Move the insertion pointer forward as needed, till you reach the Point Of Insertion Node. (The Point Of Insertion Node is that node before which you intend to insert the new node) After the insertion pointer is at the Point of Insertion Node, insert the new node just before it. In performing the insertion, only the links in the Point Of Insertion node and the links in the node that is one before the Point Of Insertion node will change. Links in other nodes will not change.

Because of the presence of a dummy node and the circular nature of the list, in all insert cases, there will always exist a node (either a real or dummy node ) before the Point of Insertion node. It will be the links of this node and that of the Point of Insertion node that will change. Therefore, the code for inserting a new node will be the same in all cases including: empty list, head insertion, mid insertion and end insertion. For each of these cases, the point of insertion node and the node prior to the Point of Insertion Node are listed below:

·       For an empty list, both point of insertion node and the node prior to the point Of insertion node are the same namely the dummy node.

·       For a head insertion, the point of insertion node is the first node and the node prior to the point of insertion node is the dummy node.

·       For a mid insertion, the point of insertion node is a real node before which the new node is being inserted and the node prior to the point of insertion node is another real node after which the new node is being inserted.

·       For end insertion, the point of insertion node is the dummy node and the node prior to the point of insertion node is a real node after which the new node is being inserted.

In all of the above case, both the point of insertion node and the node prior to the point of insertion node are identical in format and have the same format as all other nodes. Therefore, the code for inserting a node in all of the above cases is the same.

Doubly Linked List Class

Create a class to encapsulate circular doubly linked list with a dummy node and to provide the following.

Global Cursor

Provide a global cursor (pointer) that points to a working account. This cursor will be used during updates.

Building Initial List

Provide a method for building a linked list while reading from the master file. The algorithm for this method is provided later below.

Updating List

Provide a method for performing updates while reading from the transaction file. For doing an update, move the global cursor forwards or backwards as needed from account to account till you either reach the target node or a node past that node. Then carry out the required update (update, insert or delete). The algorithm for this method is provided later below

TESTING

Test Run 1

Use the following file contents for a test run.

Master File Contents (master.txt)

27183 Teresa Wong 1234.56

12345 Jeff Lee 211.22

31456 Jack Smith 1200.00

14142 James Bond 1500.00

31623 Norris Hunt 1500.00

10203 Mindy Ho 2000.00

20103 Ed Sullivan 3000.00

30102 Ray Baldwin 3824.36

30201 Susan Woo 9646.75

22345 Norma Patel 2496.24

Transaction File Contents (tran.txt)

31623 Norris Hunt -1500.00

20301 Joe Hammil +500.00

31416 Becky Wu +100.00

10203 Mindy Ho -2000.00

14142 James Bond +1500.00

27183 Teresa Wong -1234.56

10101 Judy Malik +1000.00

31416 Becky Wu  +100.00

32123 John Doe +900.00

10101 Judy Malik -200.00

22222 Joanne Doe +2750.02

SAMPLE LOG FILE

For a sample, the contents of the log file at the end of completing the first and the second update are presented below:

List At Start:

10203 Mindy Ho 2000

12345 Jeff Lee 211.22

14142 James Bond 1500

20103 Ed Sullivan 3000

22345 Norma Patel 2496.24

27183 Teresa Wong 1234.56

30102 Ray Baldwin 3824.36

30201 Susan Woo 9646.75

31456 Jack Smith 1200

31623 Norris Hunt 1500

Update #1

31623 Norris Hunt -1500

List After Update #1:

10203 Mindy Ho 2000

12345 Jeff Lee 211.22

14142 James Bond 1500

20103 Ed Sullivan 3000

22345 Norma Patel 2496.24

27183 Teresa Wong 1234.56

30102 Ray Baldwin 3824.36

30201 Susan Woo 9646.75

31456 Jack Smith 1200

Update #2

20301 Joe Hammil +500.00

List After Update #2:

10203 Mindy Ho 2000

12345 Jeff Lee 211.22

14142 James Bond 1500

20103 Ed Sullivan 3000

20301 Joe Hammil +500.00

22345 Norma Patel 2496.24

27183 Teresa Wong 1234.56

30102 Ray Baldwin 3824.36

30201 Susan Woo 9646.75

31456 Jack Smith 1200

SUBMIT

Submit the following:

·       Source code

·       Contents of file log.txt

·       Contents of the updated new master file.

Below is the hint

#include <iostream>
#include <string>
using namespace std;

struct NodeType;
typedef NodeType * NodePtr;
struct RecType
{
        long id;
        string fname;
        string lname;
        double amount;
};

struct NodeType
{
        long id;
        string fname;
        string lname;
        double amount;
        NodePtr flink;
        NodePtr blink;

};
class AccountList
{
private:
        NodePtr head;
        NodePtr cursor;
public:
        AccountList ( );
        void addAccountSorted (RecType rec);
        void updateAccount (RecType rec);
        void display(ofstream &lfstream);
};

AccountList::AccountList ( )
{
        head = new NodeType;
        head->id = -1;
        head->fname = "";
        head->lname = "";
        head->amount= -999.999;
        head->flink=head;
        head->blink=head;
        cursor = head;

}
void AccountList::addAccountSorted(RecType rec)
{
        //Creat the new node and fill it in.
        NodePtr newPtr = new NodeType;
        newPtr->id = rec.id;
        newPtr->fname = rec.fname;
        newPtr->lname = rec.lname;
        newPtr->amount = rec.amount;
        newPtr->flink = NULL;
        newPtr->blink = NULL;
        //find the Node of point of insertion
        NodePtr cur, prev;
        for (cur=head->flink; cur!=head; cur=cur->flink)
        {
                if (rec.id < cur->id)
                        break;
        }
        //set prev
        prev = cur->blink;
        
        //update the two forward links
        newPtr->flink=prev->flink;
        prev->flink = newPtr;
        //update the two backward links
        newPtr->blink = cur->blink;
        cur->blink = newPtr;

}
void AccountList::updateAccount(RecType rec)
{
        //move the cursor forward if at dummy node
        if (cursor == head)
                cursor = cursor->flink;
        //cursor is at the target node. do not move it
        if (cursor->id == rec.id)
        {
                //update the account

                //if the account became zero or negative
                //delete the node and move the cursor forward
        }
        else if (cursor->id < rec.id)
        {
                while (cursor != head)
                {
                        if (cursor->id >= rec.id)
                                break;
                        cursor = cursor->flink;
                }
                if (cursor->id == rec.id)
                {
                        //update the account

                        //if the account became zero or negative
                        //delete the node and move the cursor forward.
                }
                else
                {
                        //insert the node prior to where cursor is.
                }
        }
        else
        {
                while (cursor != head )
                {
                        if (cursor->id <= rec.id)
                                break;
                        cursor = cursor->blink;
                }
                                
                if (cursor->id == rec.id)
                {
                        //update the account

                        //if the account became zero or negative
                        //delete the node and move the cursor forward.
                }
                else
                {
                        //first move the cursor forward by one 
                        //This will make it point to the point of insertion node.
                        //Then insert the node prior to where cursor is.
                }
        }
}

//This method receives an ofstream opened for the log file 
//as a reference parameter and uses it to write the contents
//of the doubly linked list to the log file.
//This method can be used while performing updates.
void AccountList::display(ofstream & lfout)
{
        for(NodePtr cur = head->flink; cur!=head; cur=cur->flink)
                lfout << cur->id << " " << cur->fname << " " << " " << cur->lname << " " 
                      << cur->amount << endl;

}
void main ( )
{

                ofstream lfout ("C:\\log.txt");
}

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

struct NodeType;

typedef NodeType *NodePtr;

struct RecType

{

        long id;

        string fname;

        string lname;

        double amount;
};

struct NodeType

{

        long id;

        string fname;

        string lname;

        double amount;

        NodePtr flink;

        NodePtr blink;
};

class AccountList

{

private:
        NodePtr head;

        NodePtr cursor;

public:
        AccountList();

        void addAccountSorted(RecType rec);

        void updateAccount(RecType rec);

        void display(ofstream &lfstream);
};

AccountList::AccountList()

{

        head = new NodeType;

        head->id = -1;

        head->fname = "";

        head->lname = "";

        head->amount = -999.999;

        head->flink = head;

        head->blink = head;

        cursor = head;
}

void AccountList::addAccountSorted(RecType rec)

{

        //Create the new node and fill it in.

        NodePtr newPtr = new NodeType;

        newPtr->id = rec.id;

        newPtr->fname = rec.fname;

        newPtr->lname = rec.lname;

        newPtr->amount = rec.amount;

        newPtr->flink = NULL;

        newPtr->blink = NULL;

        //find the Node of point of insertion

        NodePtr cur, prev;

        for (cur = head->flink; cur != head; cur = cur->flink)

        {

                if (rec.id < cur->id)

                        break;
        }

        //set prev

        prev = cur->blink;

        //update the two forward links

        newPtr->flink = prev->flink;

        prev->flink = newPtr;

        //update the two backward links

        newPtr->blink = cur->blink;

        cur->blink = newPtr;
}

void AccountList::updateAccount(RecType rec)

{

        //move the cursor forward if at dummy node

        if (cursor == head)

                cursor = cursor->flink;

        //cursor is at the target node. do not move it

        if (cursor->id == rec.id)

        {

                //update the account

                cursor->fname = rec.fname;

                cursor->lname = rec.lname;

                if (rec.amount > 0)

                {

                        cursor->amount += rec.amount;
                }

                else

                {

                        cursor->amount += rec.amount;
                }

                //if the account became zero or negative

                //delete the node and move the cursor forward

                if (cursor->amount <= 0)

                {

                        NodePtr temp = cursor;

                        cursor->blink->flink = cursor->flink;

                        cursor->flink->blink = cursor->blink;

                        cursor = cursor->flink;

                        delete (temp);
                }
        }

        else if (cursor->id < rec.id)

        {

                while (cursor != head)

                {

                        if (cursor->id >= rec.id)

                                break;

                        cursor = cursor->flink;
                }

                if (cursor->id == rec.id)

                {

                        //update the account

                        cursor->fname = rec.fname;

                        cursor->lname = rec.lname;

                        if (rec.amount > 0)

                        {

                                cursor->amount += rec.amount;
                        }

                        else

                        {

                                cursor->amount += rec.amount;
                        }

                        //if the account became zero or negative

                        //delete the node and move the cursor forward

                        if (cursor->amount <= 0)

                        {

                                NodePtr temp = cursor;

                                cursor->blink->flink = cursor->flink;

                                cursor->flink->blink = cursor->blink;

                                cursor = cursor->flink;

                                delete (temp);
                        }
                }

                else

                {

                        //insert the node prior to where cursor is.

                        NodePtr newPtr = new NodeType;

                        newPtr->id = rec.id;

                        newPtr->fname = rec.fname;

                        newPtr->lname = rec.lname;

                        newPtr->amount = rec.amount;

                        newPtr->flink = NULL;

                        newPtr->blink = NULL;

                        newPtr->blink = cursor->blink;

                        newPtr->flink = cursor;

                        cursor->blink->flink = newPtr;

                        cursor->flink->blink = newPtr;
                }
        }

        else

        {

                while (cursor != head)

                {

                        if (cursor->id <= rec.id)

                                break;

                        cursor = cursor->blink;
                }

                if (cursor->id == rec.id)

                {

                        cursor->fname = rec.fname;

                        cursor->lname = rec.lname;

                        if (rec.amount > 0)
                        {

                                cursor->amount += rec.amount;
                        }

                        else
                        {

                                cursor->amount += rec.amount;
                        }

                        //if the account became zero or negative

                        //delete the node and move the cursor forward

                        if (cursor->amount <= 0)

                        {

                                NodePtr temp = cursor;

                                cursor->blink->flink = cursor->flink;

                                cursor->flink->blink = cursor->blink;

                                cursor = cursor->flink;

                                delete (temp);
                        }
                }

                else

                {

                        NodePtr newPtr = new NodeType;

                        newPtr->id = rec.id;

                        newPtr->fname = rec.fname;

                        newPtr->lname = rec.lname;

                        newPtr->amount = rec.amount;

                        newPtr->flink = NULL;

                        newPtr->blink = NULL;

                        cursor = cursor->flink;

                        newPtr->blink = cursor->blink;

                        newPtr->flink = cursor;

                        cursor->blink->flink = newPtr;

                        cursor->flink->blink = newPtr;
                }
        }
}

void AccountList::display(ofstream &lfout)

{

        for (NodePtr cur = head->flink; cur != head; cur = cur->flink)

                lfout << cur->id << " " << cur->fname << " "
                      << " " << cur->lname << " "

                      << cur->amount << endl;
}

int main()
{

        RecType recType;

        AccountList accounts;

        ofstream lfout("log.txt");

        string master_file, transaction_file;

        cout << " Enter the master file :";

        cin >> master_file;

        cout << " Enter the transaction file :";

        cin >> transaction_file;

        ifstream master_fin(master_file.c_str());

        ifstream transaction_fin(transaction_file.c_str());

        if (master_fin.is_open())

        {

                while (!master_fin.eof())

                {

                        master_fin >> recType.id;

                        master_fin >> recType.fname;

                        master_fin >> recType.lname;

                        master_fin >> recType.amount;

                        accounts.addAccountSorted(recType);
                }

                accounts.display(lfout);

                if (transaction_fin.is_open())

                {

                        while (!transaction_fin.eof())

                        {

                                transaction_fin >> recType.id;

                                transaction_fin >> recType.fname;

                                transaction_fin >> recType.lname;

                                transaction_fin >> recType.amount;

                                accounts.updateAccount(recType);
                        }

                        accounts.display(lfout);
                }

                else
                {

                        cout << " Unable to open " << transaction_file;
                }
        }
        else

                cout << " Unable to open " << master_file;

        master_fin.close();

        transaction_fin.close();

        lfout.close();
}

Related Solutions

Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID,...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID, name and salary as data of each employee. Search a particular record on ID and display the previous and next records as well. Whichever ID it give, it should display all the records because of being circular. Code needed in Java.
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
so the assigment is is a data strucutre using c++ to make a doubly linked list...
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT