我对此真的很陌生,现在正在学习单链表。我正在写一些代码,但我真的很困惑。我正在尝试编写读取方法和写入方法。我有一个我无法更改的测试工具。我只想能够读取流并输出流,这样它就不会返回内存地址。
谁能以非常简单的方式解释并帮助我修复此代码?
void SLLIntStorage::Read(istream& r)
{
char c[13];
r >> c;
r >> NumberOfInts;
Node *node = new Node;
head = node; //start of linked list
for(int i = 0; i < NumberOfInts; i++) //this reads from the file and works
{
r >> node->data;
cout << node->data << endl;
node ->next = new Node; //creates a new node
node = node->next;
}
}
void SLLIntStorage::Write(ostream& w)
{
Node *node = new Node;
head = node;
for(int i = 0; i < NumberOfInts; i++)
{
w << node->data << endl;
//cout << i << endl;
}
}
并在头文件中
#pragma once
#include <iostream>
using namespace std;
struct Node
{
int data; //data in current node
Node *next; //link of address to next node
};
class SLLIntStorage
{
private:
Node *head;// start of linked list
//Node *tail;
Node current; //current node
public:
void setReadSort(bool);
void sortOwn();
void Read(istream&);
void Write(ostream&);
void add(int i);
void del();
bool _setRead;
int NumberOfInts;
SLLIntStorage(void);
~SLLIntStorage(void);
};
inline ostream& operator<< (ostream& out, SLLIntStorage& n)
{
n.Write(out);
return out;
}
inline istream& operator>> (istream& in, SLLIntStorage& s)
{
s.Read(in);
return in;
}
谢谢你!