【发布时间】:2012-03-24 20:53:47
【问题描述】:
我在 C++ 程序中创建了一个链接列表,该程序将节点写入二进制文件并将它们读回链接列表。然而,当返回列表时,程序只连续显示一个节点(连续循环)。
代码如下:
#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
链表头文件:
#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
主驱动:
#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
【问题讨论】:
-
到目前为止你尝试过什么?例如,您是否在调试器中运行过它?如果您表明自己确实陷入困境,人们将更有可能提供帮助,因为您已经付出了自己的努力。
-
只看写逻辑这是行不通的:写构成对象的字节,特别是如果包含指针,如果有什么好处的话,对你没有多大帮助。通过快速浏览代码,很明显您可以删除许多不相关的垃圾:将程序简化为显示问题的最小示例,典型的ky 导致我看到问题 mysrlf。
标签: c++ linked-list binaryfiles