Linked List Add

This is a discussion on Linked List Add within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I have created the following code and I can not figure out how to do one thing, add ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    20

    Linked List Add

    Hey guys, I have created the following code and I can not figure out how to do one thing, add users. I can do it manually, but I need to prompt the user for two new accounts and then add them to the list. Can someone please inform me of how I need to do this or show me, I would greatly appreciate it!

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    //Account class
    class Account {
    private:
    	class ListNode
    	{
    		friend class Account;
    		int value;
    		double valuetwo;
    		double valuethree;
    		ListNode *next;
    
    		//Constructor
    		ListNode(int value1, double value2, double value3, ListNode *next1 = NULL)
    		{
    			value = value1;
    			valuetwo = value2;
    			valuethree = value3;
    			next = next1;
    		}
    	};
    	ListNode *head;
    
    	int number;
    	double balance;
    	double rate;
    
    public:
    	Account()
    	{ head = NULL; }
    	~Account();
    	Account (int, double, double);
    	void appendNode(int, double, double);
    	void insertNode(int, double, double);
    	void deleteNode(int, double, double);
    	void displayList();
    
    
    	void setNumber(int);
        void setBalance(double);
        void setRate(double);
    
    	int getNumber();
        double getBalance();
        double getRate();
    	void account_details();
    	string string_account_details();
    };
    
    
    Account::Account(int number, double balance, double rate) {
                    Account::number = number;
                    Account::balance = balance;
    				Account::rate= rate;
    }
    void Account::setNumber(int number) {
        Account::number = number;
    }
    
    void Account::setBalance(double balance) {
        Account::balance = balance;
    }
    
    void Account::setRate(double rate) {
        Account::rate = rate;
    }
    
    int Account::getNumber() {
        return number;
    }
    double Account::getBalance() {
        return balance;
    }
    double Account::getRate() {
        return rate;
    }
    
    void Account::account_details() {
    	cout << "The current account number is " << number <<
    		" with a balance of $" << balance << " and an interest rate of " << rate << ".\n\n";
    }
    Code:
    #include "Account.h"
    #include <iostream>
    using namespace std;
    
    //Append node
    void Account::appendNode(int number, double balance, double rate)
    	{
    		if (head == NULL)
    			head = new ListNode(number, balance, rate);
    		else
    		{
    			//List is not empty
    			ListNode *nodePtr;
    			nodePtr = head;
    			
    			while (nodePtr->next != NULL)
    				nodePtr = nodePtr->next;
    
    			nodePtr->next = new ListNode(number, balance, rate);
    		}
    	}
    
    //Display list
    void Account::displayList()
    {
    	ListNode *nodePtr;
    
    	nodePtr = head;
    	while (nodePtr)
    	{
    		cout << "The account number is " << nodePtr->value << ", the balance of the account is $"<< 
    		nodePtr->valuetwo <<", and the account rate is " << nodePtr->valuethree <<"\n";
    		nodePtr = nodePtr->next;
    	}
    }
    
    //Insert Node Function
    void Account::insertNode(int number, double balance, double rate)
    {
    	ListNode *nodePtr, *previousNodePtr;
    
    	if ( head ==NULL || head ->value >= number || head ->valuetwo >= balance || head ->valuethree >= rate)
    	{
    		head = new ListNode(number, balance, rate, head);
    	}
    	else
    	{
    		previousNodePtr = head;
    		nodePtr = head->next;
    
    		while (nodePtr != NULL && nodePtr->value < number && nodePtr->valuetwo < balance && nodePtr->valuethree < rate)
    		{
    			previousNodePtr = nodePtr;
    			nodePtr = nodePtr->next;
    		}
    
    		previousNodePtr->next = new ListNode(number, balance, rate, nodePtr);
    	}
    }
    
    //DeleteNode Function
    void Account::deleteNode(int number, double balance, double rate)
    {
    	ListNode *nodePtr, *previousNodePtr;
    
    	if (!head)
    		return;
    
    	if ( head->value == number, head->valuetwo == balance, head->valuethree == rate)
    	{
    		nodePtr = head;
    		head = head->next;
    		delete nodePtr;
    	}
    	else
    	{
    		nodePtr = head;
    
    		while (nodePtr != NULL && nodePtr->value != number && nodePtr->valuetwo != balance && nodePtr->valuethree != rate)
    		{
    			previousNodePtr = nodePtr;
    			nodePtr = nodePtr->next;
    		}
    
    		if (nodePtr)
    		{
    			previousNodePtr->next = nodePtr->next;
    			delete nodePtr;
    		}
    	}
    }
    
    //Destructor
    Account::~Account()
    {
    	ListNode *nodePtr, *nextNodePtr;
    
    	nodePtr = head;
    	while (nodePtr!=NULL)
    	{
    		nextNodePtr = nodePtr->next;
    		delete nodePtr;
    		nodePtr = nextNodePtr;
    	}
    }
    
    int main() {
    
    	Account accountList;
    
    	int catchVal;
    	double catchVal2;
    	double catchVal3;
    	
    	//Get Account information
    	cout << "Getting the first account information \n";
    	accountList.appendNode(126455, 1000.54, .03);
    	
    	cout << "Getting the second account information \n";
    	accountList.appendNode(254774, 1500.44, .03);
    	
    	cout << "Getting the third account information \n";
    	accountList.appendNode(333411, 230.00, .02);
    	
    	cout << "Getting the fourth account information \n";
    	accountList.appendNode(491377, 1950.50, .03);
    	
    	cout << "Getting the fifth account information \n";
    	accountList.appendNode(677134, 5164.11, .04);
    	
    	cout << "Getting the sixth account information \n";
    	accountList.appendNode(765473, 2540.10, .04);
    
    	cout << "\nHere is the information in the list \n";
    	accountList.displayList();
    	cout << endl;
    
    	cout << "Adding 2 new items \n";
    	cout << "_____________________________________________________________ \n";
    
    		return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Instead of
    Code:
    accountList.appendNode(491377, 1950.50, .03);
    why not do
    Code:
    accountList.appendNode(variable_1, variable_2, variable_3);

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    #1
    Code:
    class Account {
    private:
        ...
        ListNode *head;
        ...
    public:
        Account()
        { head = NULL; }
        ...
        Account (int, double, double);
        ...
    };
    
    ...
    
    Account::Account(int number, double balance, double rate) {
        Account::number = number;
        Account::balance = balance;
        Account::rate= rate;
    }
    There's a potential problem there. If you create an account with the second (non-default) constructor, head never gets set to NULL. If you then try to do anything with head, you'll likely get a program crash. Both of these constructors should set head to NULL, not just one of them.




    #2
    Code:
    class Account {
        ...
        void displayList();
        ...
    
        int getNumber();
        double getBalance();
        double getRate();
        void account_details();
        ...
    };
    
    ...
    
    int Account::getNumber() {
        return number;
    }
    double Account::getBalance() {
        return balance;
    }
    double Account::getRate() {
        return rate;
    }
    
    void Account::account_details() {
        cout << "The current account number is " << number <<
    		" with a balance of $" << balance << " and an interest rate of " << rate << ".\n\n";
    }
    
    ...
    
    //Display list
    void Account::displayList()
    {
        ListNode *nodePtr;
    
        nodePtr = head;
        while (nodePtr)
        {
            cout << "The account number is " << nodePtr->value << ", the balance of the account is $"<< 
    		nodePtr->valuetwo <<", and the account rate is " << nodePtr->valuethree <<"\n";
            nodePtr = nodePtr->next;
        }
    }
    All these function should be declared const.




    #3
    Is that first code block meant to be the header for the Account class? If so why do you have the implementation code split up in the header and in other source file like you do? The code should probably be in its own implementation source file. And the header file should not contain a "using namespace std;" declaration. Most of those #includes don't belong in that header (it only looks like <string> is required within the header itself), some don't appear to be necessary at all based on the current code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using strcmp and strlen in a Linked List
    By matrixx333 in forum C Programming
    Replies: 4
    Last Post: 11-23-2009, 03:13 AM
  2. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 12:05 AM
  3. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21