一点疑惑,大家帮忙看下。
刚学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)