I created a Linked list in a C++ program that writes the nodes to a binary file and read them back to the linked list. However when displaying back the list the program continuously displays only one node (continuous loop).
Here's the code:
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account{
private:
double balance;
unsigned int accountNumber;
Account *next;
public:
Account(){
balance=0.0;
accountNumber=0;
next=NULL;
}// primary constructor
Account(unsigned int acc, double bal){
accountNumber=acc;
balance=bal;
next=NULL;
}// primary constructor
Account::~Account(){
//delete next;
}// destructor
void setBalance(double b){
balance=b;
}// end of setBalance
double getBalance(){
return balance;
}
void setAcc(unsigned int acc){
accountNumber=acc;
}
unsigned int getAccNum(){
return accountNumber;
}
// links
void setNextAccount(Account *nAcc){
next=nAcc;
//delete nAcc;
}// Mutator for next
Account *getNextAccount(){
//Account *temp=next;
return next;
}
};
#endif
Linked list header file:
#ifndef ACCOUNTLIST_H
#define ACCOUNTLIST_H
class AccountList{
private :
Account *head;
//Account * temp;
//Account *current;
public:
AccountList(){
head=NULL;
//temp=NULL;
//current=NULL;
//current=NULL;
}// end of default constructor
AccountList::~AccountList(){
/*Account *temp;
while (head!= NULL)
{
temp = head;
head = head->getNextAccount();
delete temp;
} */
delete head;
//delete current;
}// destructor for list
void addNode(Account *h){
//Account *temp;
Account *current;
//temp=h;
//temp->setNextAccount(NULL);
if(head==NULL){
head=h;
}
else{
current=head;
while((current->getNextAccount())!=NULL){
current= current->getNextAccount();
}
current->setNextAccount(h);
}
//delete current;
}// mutator for head
void displayAll(){
Account *temp=head;
while ((temp!=NULL) && (temp->getAccNum()!=0)){
cout << "Account number: " <<temp->getAccNum() << " has a balnce of: " << temp->getBalance() <<endl;
temp=temp->getNextAccount();
}
delete temp;
}
void displayNode(int id){
Account *temp= head;
while (temp !=NULL){
if (temp->getAccNum()==id){
//temp->display();
cout << "Account Number : " << temp->getAccNum() <<"has a balance of " << temp->getBalance() <<endl;
delete temp;
return;
}
temp= temp->getNextAccount();// gets next node in the list
}
cout << "Employer was not found" << endl;
}// end of displayNode
Account* getHead(){
return head;
}
void removeAll(){
Account *temp;
while (head!=NULL){
temp=head;
head= head->getNextAccount();
delete temp;
}
}// end of method removeAll
};
#endif
main driver:
#include <iostream>
#include <fstream>
#include "Account.h"
#Include "AccountList"
//#include <cstdlib>
#include <stdlib.h>
using namespace std;
void main(){
Account *acc1=new Account(1, 546.34); // create object and initialize attributes
Account *acc2=new Account(2,7896.34);
Account *acc3=new Account();
AccountList *list1= new AccountList();
AccountList *list2= new AccountList();
// add nodes to linked list
list1->addNode(acc1);
list1->addNode(acc2);
//cout <<"Hello"<<endl;
// file operation
ofstream outAccount("account.dat", ios::out|ios::binary);
// checks if ofstream could open file
if(!outAccount){
cerr<<"File could not be open" << endl;
exit(1);
}
acc3=list1->getHead();
while( acc3!=NULL){
outAccount.write(reinterpret_cast < const char*>(&acc3), sizeof(Account));
acc3=acc3->getNextAccount();
//outAccount.write(reinterpret_cast < const char*>(&acc2), `sizeof(Account));`
}
//cout <<"Hello"<<endl;
outAccount.close();
// read and display contents of file
ifstream inAccount("account.dat", ios::in|ios::binary);
if(!inAccount){
cerr <<"File could not be openned for reading from file" << endl;
exit(1);
}
//Account *accTemp=new Account();
while(inAccount && !inAccount.eof()){
inAccount.read(reinterpret_cast < char* >(&acc3), sizeof(Account));
list2->addNode(acc3);
//cout <<"Account Number : " << acc3->getAccNum()<< "has a balance of: " `<< acc3->getBalance() <<endl;`
}
inAccount.close();
cout <<"Hello"<<endl;
list2->displayAll();
system("PAUSE");
system("PAUSE");
}// end of main