首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

一点疑惑,大家帮忙看下。该如何解决

2012-03-26 
一点疑惑,大家帮忙看下。刚学C++不久,钱能那本书里面的关于链表的。。。#includeiostreamusingnamespacestd

一点疑惑,大家帮忙看下。
刚学C++不久,钱能那本书里面的关于链表的。。。

#include   <iostream>
using   namespace   std;

struct   Student
{
long   number;
float   score;
Student   *next;
};

Student   *head;

Student   *Create()
{
Student   *pS;
Student   *pEnd;
pS   =   new   Student;
cin   > >   pS-> number   > > pS-> score;
head   =   NULL;
pEnd   =   pS;
while   (pS-> number   !=   0)
{
if   (head   ==   NULL)
head   =   pS;
else
pEnd-> next   =   pS;

pEnd   =   pS;
pS   =   new   Student;
cin   > > pS-> number   > > pS-> score;
}
pEnd-> next   =   NULL;
delete   pS;
return   head;
}

void   ShowList(Student   *head)
{
cout   < < "now   the   items   of   list   are "   < <endl;
while   (head)
{
cout   < <head-> number   < < ", "   < <head-> score   < <endl;
head   =   head-> next;
}
}

void   main()
{
ShowList(Create());
}

[解决办法]
是的。

但是你需要明白,
在最上面给的程序中,没有删除链表的函数,
它是预先分配一个节点 pS,
然后输入数据。

但是由于是预先分配的,
所以在输入结束的时候,
需要把最后一次多余分配的那个节点删除,
(先分配了pS,然后发现数据不输入了,这个节点不用了,可以 delete)

热点排行