C语言创建单链表,输出单链表的内容。
来源:互联网 发布:电脑工作备忘录软件 编辑:程序博客网 时间:2024/06/05 00:36
#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);
}
#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
- C语言创建单链表,输出单链表的内容。
- C语言单链表和双链表的创建和输出
- C语言基础 将控制台上面输出的内容清屏
- c语言创建单链表
- C语言创建单链表
- c语言创建单链表
- C语言创建单链表
- c语言 逆序 输出文件内容
- C语言改变控制台输出内容颜色
- C语言中,链表的创建、输入、输出、链接。
- 邻接表的创建与输出(C语言)
- c语言单链表的创建、插入、删除
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 倒序输出单链表的内容
- c 语言的输出
- c语言输出的%
- c语言创建并输出二叉树
- 2013年12月15日
- 将博客搬至CSDN
- 将博客搬至CSDN
- IOS后台运行机制详解(二)
- 有限状态机(转)
- C语言创建单链表,输出单链表的内容。
- c语言练习题 模拟n个人参加选举的过程,四个 候选人A、B、C、D。若选举某人直接 输入其编号,最后按获得票数从高到 低排序并输出候选人编号和票数。
- ZOJ-1760
- java的线程同步
- java 删除有文件的文件夹
- 注意变化:愚人熬汤
- SAE搭建WordPress教程 免费建WordPress博客站
- 10款优秀Bootstrap响应式后台管理系统模板
- 某班5个学生,3门课,编写3个函数实现以下功能: //(1)求各门课的平均分 //(2)找出三门都不及格的学生,输出其全部信息 //(3)找出3门课平均分在80-90之间的学生,输出其信息。