一道链表题!

来源:百度知道 编辑:UC知道 时间:2024/06/06 04:52:27
48~50.以下程序的功能是:建立一个带有头结点的单向链表,并将存储在数组中的
字符依次转储到链表的各个结点中,请从与下划线处号码对应的一组选若中选择出
正确的选项。
#include
stuct node
{ char data; struct node *next;};
(48) CreatList(char *s)
{ struct node *h,*p,*q);
h=(struct node *) malloc(sizeof(struct node));
p=q=h;
while(*s!='\0')
{ p=(struct node *) malloc(sizeof(struct node));
p->data= (49) ;
q->next=p;
q= (50) ;
s++;
}
p->next='\0';
return h;
}
main()
{ char str[]="link list";
struct node *head;
head=CreatList(str);
...
}
(48)
A)char *
B)struct node
C) struct node*
D) char
(49)
A)*s
B)s
C)*s++
D)(*s)++
(50)
A)p->next
B)p
C)s
D)s->next
答案C A B

stuct node
{ char data; struct node *next;//定义结构体

struct node* CreatList(char *s)//定义建立链表的函数,返回一个指向连表头的指针

{ struct node *h,*p,*q;
h=(struct node *) malloc(sizeof(struct node));
p=q=h;
while(*s!='\0')
{ p=(struct node *) malloc(sizeof(struct node));//开辟一个新单元,让p指向新开辟的结点并赋值
p->data= *s;

q->next=p;//q指向最后一个结点,把p所指的结点连接在q所指的结点的后面
q=p;

s++;
}
p->next='\0';
return h;
}
找书看看动态链表的创建