`

C的链表

阅读更多
#include <stdio.h>
//定义一个结构类型
struct student{
	int num;
	char name[20];
	struct student *next;
};
//声明空间分配函数
void *malloc(unsigned size);
//创建链表的函数
struct student *linklist(){

	struct student *head,*tail,*p;
	
	char name[20];
	
	int num,size=sizeof(struct student);
	
	head = tail = NULL;
	
	scanf("%d %s",&num,&name);
	
	while(num){
	
		p=(struct student *)malloc(size);
		
		p->num = num;
		
		strcpy(p->name,name);
		
		p->next = NULL;


		
		if(head==NULL) head = p;
		
		else tail->next = p;
		
		tail = p;
		
		scanf("%d %s",&num,&name);
		
	}

	
	return head;
}
void main(){

	struct student *list; 
	list = linklist();

	while(list!=NULL){
		printf("Num is %d And Name is %s \n",list->num,list->name);
		list = list->next;
	};
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics