问答题
已知一个单向链表结点的数据结构定义如下:
struct node
{
char data;
struct node *next;
};
函数struct node *cre (char *s)的功能是:根据s指向的字符串建立一个结点类型为struct node头指针为h的单向链表,使h链表中各结点的数据域分别存储s指向字符串中所有大写字母的编码,函数返回h链表首结点的地址.例如,若s指向的字符串为"3Aa26Bx5Y9",则h指向的链表如下图所示。
#include
#include
struct node
{
char data;
struct node *next;
};
struct node *cre(char *s)
{
struct node *p,*p1,*h;
if (___(27)___) return NULL;
h=p1=p=(struct node *)malloc(sizeof(struct node)) ;
p->data=*s ;
s++;
while (*s)
{
if(*s>='A' && *s<='Z')
{
p= (struct node *) malloc (sizeof (struct node)) ;
p->data=*s ;
___(28)____ = p;
p1=p ;
}
s++;
}
___ (29)___ =NULL;
___(30)___;
}
void print(struct node *h)
{
struct node *p=h;
while (p!=NULL)
{ printf ("%3c", p->data) ;
p=p->next ;
}
printf ("/n") ;
}
int main()
{
struct node *head=NULL;
char *p=”3Aa26Bx5Y9";
head=cre (p) ; print (head) ;
return 0;
}
【正确答案】 (27)*s=='/0'或 *s==0 或!*s
(28) p1 -> next
(29) p1 -> next或 p -> next
(30) return h
【答案解析】