我正在尝试将链接列表写入二进制文件,然后在程序启动时将其读回。
我为此编写了以下代码:
class Node
{
private:
int pos;
int data;
Node* next;
Node* prev;
friend class Linklist;
public:
Node(int d):data(d),pos(-1),next(NULL),prev(NULL)
{}
};
#include <new>
#include <sstream>
#include<iostream>
#include"linklist.h"
bool Linklist::insert(int data, bool updateDisk)
{
if(isExist(data))
{
std::cout<<"Tried to insert duplicate data";
return false;
}
Node *temp = new (std::nothrow) Node(data);
if(temp == NULL)
{
return false;
}
if(tail == NULL)
{
tail = temp;
head = temp;
}
else
{
tail->next = temp;
temp->prev = tail;
tail = temp;
}
if(updateDisk)
{
tail->pos = nextPosition++;
updateAdditionOnDisk(tail);
}
}
bool Linklist::insert(int data, int location)
{
if(isExist(data))
{
return false;
}
Node *temp = new (std::nothrow) Node(data);
if(temp == NULL)
{
return false;
}
Node *it = head;
while(it != NULL)
{
if(it->pos > location)
{
break;
}
it = it->next;
}
if(it)
{
temp->prev = it->prev;
temp->next = it;
it->prev->next = temp;
it->prev = temp;
}
//tail->position = updateAdditionOnDisk(data, nextAvailablePos);
}
bool Linklist::erase(int data)
{
if(tail == NULL)
return false;
Node *temp = head;
while(temp != NULL)
{
if(temp->data == data)
{
//nextAvailablePos = updateDeletionOnDisk(temp->position, nextAvailablePos);
if(temp == head)
{
if(head->next)
{
head = head->next;
head->prev = NULL;
delete temp;
}
else
{
delete head;
head = NULL;
tail = NULL;
}
return true;
}
else if(temp == tail)
{
if(head == tail)
{
delete head;
head = NULL;
tail = NULL;
}
else
{
tail = tail->prev;
tail->next = NULL;
delete temp;
}
return true;
}
else
{
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
return true;
}
}
temp = temp->next;
}
return false;
}
bool Linklist::isExist(int data)
{
Node *temp = head;
while(temp != NULL)
{
if(temp->data == data)
{
return true;
}
temp = temp->next;
}
return false;
}
void Linklist::display( )
{
Node *temp = head;
while(temp != NULL)
{
std::cout<<temp->data;
if(temp->next)
{
std::cout<<"-->";
}
temp = temp->next;
}
}
int Linklist::updateAdditionOnDisk(Node *node)
{
oFile.seekp (0, std::ios::beg);
oFile.write( (char*)&nextPosition, sizeof(int) );
oFile.flush();
int count = 0,pos = 0;
bool inserted = false;
Node n(-1);
iFile.seekg (0, std::ios::beg);
iFile.read((char*)&pos, sizeof(int));
while(!iFile.eof())
{
std::cout<<"iFile is good";
if(n.pos == -1)
{
oFile.seekp(sizeof(int) + (sizeof(Node) * count) , std::ios::beg);
oFile.write( (char*)node, sizeof(Node) );
oFile.flush();
inserted = true;
break;
}
count++;
}
if(!inserted)
{
oFile.seekp(sizeof(int), std::ios::beg);
oFile.write( (char*)node, sizeof(Node) );
oFile.flush();
}
}
int Linklist::updateDeletionOnDisk(int data)
{
int temp = nextPosition + 1;
oFile.seekp (0, std::ios::beg);
oFile.write( (char*)&nextPosition, sizeof(int) );
oFile.flush();
int count = 0,pos = 0;
bool inserted = false;
Node n(-1);
n.pos = -1;
iFile.seekg (0, std::ios::beg);
iFile.read((char*)&pos, sizeof(int));
while(!iFile.eof())
{
std::cout<<"iFile is good";
iFile.read((char*)&n, sizeof(int));
if(n.data == data)
{
n.pos = -1;
oFile.seekp(sizeof(int) + (sizeof(Node) * count) , std::ios::beg);
oFile.write( (char*)&n, sizeof(Node) );
oFile.flush();
break;
}
count++;
}
}
void Linklist::createListFromFile ()
{
Node n(-1);
iFile.seekg(0, std::ios::beg);
if(!iFile.eof())
{
iFile.read((char*)&nextPosition, sizeof(int));
while(!iFile.eof())
{
iFile.read((char*)&n, sizeof(Node));
}
}
}
Linklist::~Linklist()
{
while(head)
{
Node * temp = head;
head = head->next;
delete temp;
}
}
int main ()
{
char choice;
int data;
Linklist l;
while (1)
{
std::cout << "\n\nSelect Opration to performed on LinkList"<<std::endl;
std::cout << "1 Insert "<<std::endl;
std::cout << "2 Delete "<<std::endl;
std::cout << "3 IsExist "<<std::endl;
std::cout << "4 Display "<<std::endl;
}
}
Linklist::~Linklist()
{
while(head)
{
Node * temp = head;
head = head->next;
delete temp;
}
}
但代码给了我垃圾输出。
有人可以指出我在代码中可能存在的错误。
谢谢