请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2,函数void Insert(node*q)使程序能够完成如下功能:从键盘输入一行字符,调用该函数建立反序的无头结点的单链表,然后输出整个链表。
注意:请勿修改主函数main和其他函数中的任何内容,只需在画线处编写适当代码,也不能删除或移动//************found************。
//源程序proj2.cpp
#include<iostream>
using namespace std;
struct node
char data;
node*link:
*head; //链表首指针
void Insert(node*q) //将节点插入链表首部
//************found************
______;
head=q;
int main()
char ch;
node *p;
head=NULL:
cout<<"Please input the string"<<endl;
while((ch=cin.get())!=’\n’)
//************found************
______;//用new为节点p动态分配存储空间
p->data=ch;
//************found************
______; //在链表首部插入该节点
p=head;
while(p!=NULL)
cout<<p->data;
p=p->link;
cout<<endl;
return 0:
参考答案:1)node*head;
2)p=new node();
3)p->link=head; head=p;
解析:1)后面有head=q;,显然有个变量头指针没定义,所以在该行应该定义一个头指针head。
2)为指针p分配内存空间。
3)将新结点插入到链表中,链表的指针顺序不能搞错,应该是先将p->link指向head指向的地址,然后将head指针指向p。