求解,指针,链表
#include<stdio.h>#include<stdlib.h>
#include<string.h>
struct stu{
int num;
char name[20];
struct stu * next;
};
struct stu * creat();
struct stu * show(struct stu * head);
int main(void)
{
int choice;
struct stu * head = NULL;
printf("1:creat link\n");
printf("2:show link\n");
printf("0:exit\n");
printf("enter 0-2 !\n");
scanf("%d",&choice);
switch(choice){
case 1:
head = creat();
case 2:
show(head); break;
case 0:
break;
}
printf("input error!");
return 0;
}
struct stu * creat()
{
struct stu * head,* p,* tail;
int num;
char name[20];
head = tail = NULL;
int size = sizeof(struct stu);
printf("input 0 exit!\n");
printf("inout num and name:\n");
scanf("%d%s",&num,name);
do{
p = (struct stu *)malloc(size);
p->num = num;
strcpy(p->name,name);
if(head == NULL)
head = p;
else
tail->next = p;
tail = p;
scanf("%d",&num);
scanf("%s",name);
}while(num != 0);
return head;
}
struct stu * show(struct stu * head)
{
struct stu * ptr;
printf("输出链表:");
if(head == NULL){
printf("NULL!\n");
return 0;
}
for(ptr = head;ptr;ptr = ptr->next)
printf("%d %s\n",ptr->num,ptr->name);
return 0;
}
[local]1[/local]