填空:以下程序的功能为:从键盘输入1个字符串,调用函数建立反序的链表,然后输出整个链表。补充完善程序,以实现其功能。
#include <stdio.h>
#include <malloc.h>
struct node
{ char data;
struct node *link;
} *head;
Ins(struct node ___________)
{
if(head==NULL)
{ q->link==NULL;
head=q;
}
else
{ q->link=__________;
head=q;
}
}
main()
{ char ch;
struct node *p;
head=NULL;
while((ch=getchar())!='\n')
{
p=________________;
p->data=ch;
Ins(____________);
}
p=head;
while(p!=NULL)
{
printf("%c",p->data);
________________;
;
}
}
#include <stdio.h>
#include <malloc.h>
struct node
{
char data;
struct node *link;
} * head;
void Ins(struct node *q)
{
if (head == NULL)
{
q->link = NULL;
head = q;
}
else
{
q->link = head;
head = q;
}
}
int main()
{
char ch;
struct node *p;
head = NULL;
while ((ch = getchar()) != '\n')
{
p = (struct node *)malloc(sizeof(struct node));
p->data = ch;
Ins(p);
}
p = head;
while (p != NULL)
{
printf("%c", p->data);
p = p->link;
}
return 0;
}
供参考:
#include <stdio.h>
#include <malloc.h>
struct node
{ char data;
struct node *link;
} *head;
Ins(struct node *q) //___________)
{
if(head==NULL)
{ q->link=NULL; // q->link==NULL;
head=q;
}
else
{ q->link=head;//__________;
head=q;
}
}
main()
{
char ch;
struct node *p;
head=NULL;
while((ch=getchar())!='\n')
{
p=(struct node*)malloc(sizeof(struct node));//________________;
p->data=ch;
Ins(p); //(____________);
}
p=head;
while(p!=NULL)
{
printf("%c",p->data);
p = p->link; //________________;
//;
}
}