linux c语言链表的简单应用之创建链表

/* ************************************************************************
* Filename: link.c
* Description:
* Version: 1.0
* Created: 2011骞?4鏈?9鏃?17鏃?2鍒?3绉?
* Revision: none
* Compiler: gcc
* Author: wen hao (WH), hnrain1004@gmail.com
* Company: sunplusapp
* ***********************************************************************
*/


#include
<stdio.h>
#include
<stdlib.h>
#include
"link.h"

#define LEN sizeof(struct stu)
//声明结构体
typedef struct stu
{
int num;
char name[10];
struct stu *next;
}TYPE;

//链表创建函数,返回类型为结构体指针类型
TYPE * create(int n)
{
TYPE
*head,*prev,*curre;
int i;

for(i = 0; i < n; i++)
{
curre
= (TYPE *)malloc(LEN);//申请空间
printf("input number and name:\n");
scanf(
"%d %s",&curre->num,curre->name);//等待用户输入数据

if(i == 0)
prev
=head=curre;
else
prev
->next=curre;
prev
=curre;
}
curre
->next =NULL;
return head;
}

//打印输出函数,形参为链表头指针
void print(TYPE *head)
{
printf(
"\nthe link message is :\n");
printf(
"number \t\tname \n");
while(head!=NULL)//如果没有指向链表尾就一直打印
{
printf(
"%d\t\t%s\n",head->num,head->name);
head
=head->next;
}
printf(
"\n");
}

int main(void)
{
TYPE
*head;//定义结构体变量
head = create(3);//创建链表
print(head);//打印链表

return 0;
}
posted @ 2011-04-29 14:12  hnrainll  阅读(1397)  评论(0编辑  收藏  举报