这种写法最后会是一个 data 为 0 的结点,但遍历和析构时用 data 为 0 判断结束而导致缺少对 data 为 0 结点的操作,改正如下:
#include
usingnamespace std;
struct node
{
//链表的节点
int data;//数据
int num;//节点编号
struct node *next;//指向下一个节点
};
int main()
{
struct node *head/*头节点*/,*p,*q;
head = NULL;
p = NULL;
q =new node;
q->next= NULL;
q->num =1;
int a =-1;
cout <<"请输入第1个数字:";
cin >> a;
q->data = a;
head = q;
while(a !=0)
{
p = q;
q =new node;
q->next= NULL;
p->next= q;
q->num = p->num +1;
cout <<"请输入第"<< q->num <<"个数字:";
cin >> a;
q->data = a;
}
//前面都是输入,这以下都是输出
q = head;
p = NULL;
while(1)
{
printf("%d %d\n", q->num, q->data);
if(q->data ==0)
break;
q = q->next;
}
//释放内存
q = head;
p = q;
while(1)
{
p = q->next;
delete[]q;
q = p;
if(!q)
break;
}
return0;
}