新人菜鸟提问:关于C++中的链表问题
#include
#include
using namespace std;
struct node
{
int data;
node * next;
};
int main()
{
ifstream inFile("s.dat",ios::in);
node *head=NULL;
node *tail=NULL;
node *tmp;
int num;
tmp=new node;
inFile>>num;
tmp->next=NULL;
tmp->data=num;
head=tail=tmp;
while(inFile!=NULL)
{
tmp=new node;
inFile>>num;
tmp->data=num;
tmp->next=NULL;
tail->next=tmp;
tail=tmp;
}
inFile.close();
for(node *p_i=head;p_i!=NULL;p_i=p_i->next)
{
node *p_k=p_i;
for(node *p_j=p_i;p_j!=NULL;p_j=p_j->next)
if(p_j->datadata)
p_k=p_j;
int x=p_i->data;
p_i->data=p_k->data;
p_k->data=x;
}
ofstream outFile("d.dat",ios::out);
node *q=head;
while(q!=NULL)
{
outFile<data<<" ";
q=q->next;
}
outFile.close();
delete tmp;
return 0;
}
为什么文件中的最后一个数会被多读取一遍?
weixin_39538031
2017/07/17 08:58- 多谢!!!
- 希望大神帮忙解释一下
- 点赞
- 收藏
- 回答