C语言创建单链表,输出单链表的内容。

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define NEW (struct node *)malloc(sizeof(struct node))
struct node{
char name[20],tel[9];
struct node *next;
};
struct node *create() //返回指针节点的地址
{
struct node *h,*p,*q; //头节点,第一个节点,第二节点
char name[20];  //姓名
h=NULL;  //初始h为空
printf("name: ");
gets(name);  //输入名字
while(strlen(name)!=0)  //当输入名字不为空则循环
{
p=NEW; //创建新的内存空间
if(p==NULL)  //如果创建失败
{
printf("Allocation failure\n");
exit(0);
}
strcpy(p->name,name);  //给结点赋值
printf("tel: ");  
gets(p->tel);  //输入电话号码
p->next=NULL;   //下一结点为空
if(h==NULL)  //如果h为空,则h=p
h=p;
else        //否则往下循环
q->next=p;  
q=p;
printf("name: ");
gets(name);
}
return(h);
}
void printlist(struct node *head)
{
struct node *p;
p=head;
while(p!=NULL)
{
printf("%s\t%s\t\n",p->name,p->tel);
p=p->next;
}
}
main()
{
struct node *head;
head=create();
printlist(head);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值