链表的创建与输出
来源:互联网 发布:超越无限 知乎 编辑:程序博客网 时间:2024/06/05 19:12
创建两个学生链表,含有姓名,年龄的信息,一个链表存放男生,一个链表存放女生
并且合并这两个链表。
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof (struct student)
struct student
{
char name[100];
int age;
struct student *next;
};
int n;
struct student *create ()
{
struct student *head;
struct student *p1 = NULL;
struct student *p2 = NULL;
n = 0;
p1 = (struct student *) malloc (LEN);
p2 = p1;
if (p1 == NULL)
{
printf ("Cann't create it, try it again in a monent!\n");
return NULL;
}
else
{
head = NULL;
printf ("Please input %d node -- name, year: \n",n+1);
scanf ("%s",&(p1->name));
scanf ("%d",&(p1->age));
}
while (strcmp(p1->name,"0") != 0)
{
n += 1;
if (n == 1)
{
head = p1;
p2 -> next = NULL;
}
else
{
p2 -> next = p1;
}
p2 = p1;
p1 = (struct student *) malloc (LEN);
printf ("Please input %d node -- name,age: \n",n+1);
scanf ("%s",&(p1->name));
if (strcmp(p1->name,"0") == 0)
break;
scanf ("%d",&(p1->age));
}
p2->next = NULL;
free (p1);
p1 = NULL;
return head;
}
void printA (struct student *head)
{
struct student *p;
printf ("Now, these %d records are: \n",n);
p = head;
if (p != NULL)
{
printf ("head is %p\n",p);
while (p != NULL)
{
printf ("%p %s %d %p\n", p, p->name, p->age, p->next);
p = p->next;
}
}
}
struct student *comb(struct student *b, struct student *g)
{
struct student *p = NULL;
p = b;
while (p->next != NULL)
{
p = p->next;
}
p->next = g;
return b;
}
int main()
{
struct student *boy = NULL;
struct student *girl = NULL;
boy = create ();
printA (boy);
girl = create ();
printA (girl);
printA( comb (boy,girl) );
return 0;
}
- 链表的创建与输出
- 循环链表的创建与输出
- VC实现链表的创建与输出
- 数据结构有序表的创建与输出
- 单链表的创建与输出
- 双链表的创建与输出
- 邻接表的创建与输出(C语言)
- 单向链表的创建 输入 输出
- 链表的创建,插入,删除,输出
- 链表的创建,插入,输出,打印
- 连续存储---非循环链表的创建与遍历输出
- 数据结构的单链表创建与输出
- 表达式树的创建与输出
- 链表的创建 输出 增加删除的完全笔记
- new的链表,创建链表,输出链表
- 链表的基本操作,包括创建,输出链表。
- 单向动态链表的创建、输入、插入、删除、输出
- C语言中,链表的创建、输入、输出、链接。
- js中input标签内容改变的触发事件
- hdu 2030 汉字统计
- Qt防止本程序多次启动
- Android Studio 中的 gradle 详解
- C#多线程学习(一) 多线程的相关概念
- 链表的创建与输出
- 升级Win下工具链以支持C++17
- TextView、Button的drawableLeft和drawableRight与文本一起居中显示
- UITableView嵌套WKWebView的那些坑
- Promise-使用整理
- 排序算法(三) —— 插入排序
- 机器学习实战 笔记 debug kNN (三)
- 数据库索引类型及实现方式
- MongoDB 与 MySQL操作数据库对比