linked list in c++

help in adding new data to list and storing to a file

Page 1 of 1

6 Replies - 809 Views - Last Post: 24 February 2010 - 12:16 PM Rate Topic: -----

#1 pascal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-February 07

linked list in c++

Posted 24 February 2010 - 08:12 AM

i understand how input the data as follows:
#include <iostream>
class Account
{
private:
int accountNum; //account number
float accountBal; //balance in the account
Account *nextAccount; //points to next account in list

public:
Account();
Account(int, float);
~Account();
int getAccountNum();
void setAccountNum(int);
float getAccountBal();
void setAccountBal(float);
Account * getNextAccount();
void setNextAccount(Account *);
};

#include "Account.h"
#include <iostream>
using std::cout;
Account::Account() //default constructor
{
accountNum = 0;
accountBal = 0.0f;
nextAccount = NULL;
}
Account::Account(int an, float ab) //primary constructor
{
accountNum = an;
accountBal = ab;
nextAccount = NULL;
cout << "[" << accountNum << "," << accountBal << "] -> ";
}
Account::~Account() //destructor
{
delete nextAccount;
cout << "\nDeleting node [" << getAccountNum() << ","
<< getAccountBal() << "]";
}
int Account::getAccountNum() //accessor for accountNum
{
return accountNum;
}
void Account::setAccountNum(int an) //mutator for accountNum
{
accountNum = an;
}

float Account::getAccountBal() //accessor for accountBal
{
return accountBal;
}
void Account::setAccountBal(float ab) //mutator for accountBal
{
accountBal = ab;
}
Account * Account::getNextAccount() //accessor for nextAccount pointer
{
return nextAccount;
}
void Account::setNextAccount(Account *na) //mutator for nextAccount pointer
{
nextAccount = na;
}

#include <iostream>
#include "Account.h"
int main()
{
Account *head = NULL; //create an empty list - head points to NULL
Account *temp;
Account *current;
temp = new Account(1000, 50000.00); //create 1st node
head = temp; //let head point to 1st node
temp = new Account(1001, 90500.00); //create 2nd node
head->setNextAccount(temp); //place 2nd node at end of list
temp = new Account(1002, 30700.00); //create 3rd node
current = head; //need to place it at end of list...
//...so traverse list until...
//... nextAccount == NULL
while( current->getNextAccount() != NULL)
{
current = current->getNextAccount();
} current->setNextAccount(temp);
delete head; //remove the list from memory (recursively)
return 0;
}




what i want help with is how to get the users input to add to the list and create and store it to a file.

thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: linked list in c++

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: linked list in c++

Posted 24 February 2010 - 08:32 AM

How far have you got with the problem?
Was This Post Helpful? 0
  • +
  • -

#3 neptunusmaris  Icon User is offline

  • New D.I.C Head

Reputation: -18
  • View blog
  • Posts: 42
  • Joined: 31-July 09

Re: linked list in c++

Posted 24 February 2010 - 08:35 AM

View Postpascal, on 24 February 2010 - 07:12 AM, said:

i understand how input the data as follows:
#include <iostream>
class Account
{
private:
int accountNum; //account number
float accountBal; //balance in the account
Account *nextAccount; //points to next account in list

public:
Account();
Account(int, float);
~Account();
int getAccountNum();
void setAccountNum(int);
float getAccountBal();
void setAccountBal(float);
Account * getNextAccount();
void setNextAccount(Account *);
};

#include "Account.h"
#include <iostream>
using std::cout;
Account::Account() //default constructor
{
accountNum = 0;
accountBal = 0.0f;
nextAccount = NULL;
}
Account::Account(int an, float ab) //primary constructor
{
accountNum = an;
accountBal = ab;
nextAccount = NULL;
cout << "[" << accountNum << "," << accountBal << "] -> ";
}
Account::~Account() //destructor
{
delete nextAccount;
cout << "\nDeleting node [" << getAccountNum() << ","
<< getAccountBal() << "]";
}
int Account::getAccountNum() //accessor for accountNum
{
return accountNum;
}
void Account::setAccountNum(int an) //mutator for accountNum
{
accountNum = an;
}

float Account::getAccountBal() //accessor for accountBal
{
return accountBal;
}
void Account::setAccountBal(float ab) //mutator for accountBal
{
accountBal = ab;
}
Account * Account::getNextAccount() //accessor for nextAccount pointer
{
return nextAccount;
}
void Account::setNextAccount(Account *na) //mutator for nextAccount pointer
{
nextAccount = na;
}

#include <iostream>
#include "Account.h"
int main()
{
Account *head = NULL; //create an empty list - head points to NULL
Account *temp;
Account *current;
temp = new Account(1000, 50000.00); //create 1st node
head = temp; //let head point to 1st node
temp = new Account(1001, 90500.00); //create 2nd node
head->setNextAccount(temp); //place 2nd node at end of list
temp = new Account(1002, 30700.00); //create 3rd node
current = head; //need to place it at end of list...
//...so traverse list until...
//... nextAccount == NULL
while( current->getNextAccount() != NULL)
{
current = current->getNextAccount();
} current->setNextAccount(temp);
delete head; //remove the list from memory (recursively)
return 0;
}




what i want help with is how to get the users input to add to the list and create and store it to a file.

thanks in advance.


First, if you are using C++ try using some of the built in types that can help you with linked list functionality:

Specifically for linked list situations..
http://cplusplus.com...rence/stl/list/

A wiki page for the concept:
http://en.wikipedia....iki/Linked_list

And a book that explains the 3 types of link lists(singly, doubly, circularly) well:
http://oreilly.com/c...ithms+with+C%2c

Hope that helps...

This post has been edited by neptunusmaris: 24 February 2010 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 pascal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-February 07

Re: linked list in c++

Posted 24 February 2010 - 11:55 AM

View PostMartyn.Rae, on 24 February 2010 - 07:32 AM, said:

How far have you got with the problem?


i understand how to add info using c eg:
void add_node_at_end()
  {  node *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new node;
     cout << "Please enter the name of the person: ";
     cin >> temp->name;
     cout << "Please enter the age of the person : ";
     cin >> temp->age;
     cout << "Please enter the height of the person : ";
     cin >> temp->height;
     temp->nxt = NULL;

     // Set up link to this node
     if (start_ptr == NULL)
       { start_ptr = temp;
	 current = start_ptr;
       }
     else
       { temp2 = start_ptr;
         // We know this is not NULL - list not empty!
         while (temp2->nxt != NULL)
           {  temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
  }



but now they are doing with c++ and that's the problem also saving the added information from the user to a file.
Was This Post Helpful? 0
  • +
  • -

#5 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: linked list in c++

Posted 24 February 2010 - 12:00 PM

You say:

Quote

... but now they are doing with c++ ...


Who is doing it with C++?

This post has been edited by Martyn.Rae: 24 February 2010 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: linked list in c++

Posted 24 February 2010 - 12:04 PM

You may find this helpful
Was This Post Helpful? 0
  • +
  • -

#7 pascal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-February 07

Re: linked list in c++

Posted 24 February 2010 - 12:16 PM

View PostMartyn.Rae, on 24 February 2010 - 11:00 AM, said:

You say:

Quote

... but now they are doing with c++ ...


Who is doing it with C++?


my school the title is data structures using c++
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1