C语言单链表的创建和添加结点
2017-09-29 14:21
330 查看
#include<stdio.h>
//无头结点
#define LINKLIST_H
typedef struct
{
int data;
struct LNode *next;
}Node,*lnode; //Node结构体名称,*lnode结构体指针类型
lnode create(){
//创建的时候用到了尾指针。但是下面的add结点没有用到。两者用的都是尾插入法
lnode L= (lnode)malloc(sizeof(Node));
lnode new,end;
int x=0;
//初始化一个列表
if (L != NULL) {
L->data = 1;
L->next = NULL;
}
end = L;
//尾插入法
for (int i = 2; i <= 6; i++) {
new = (lnode)malloc(sizeof(Node));
new->data = i;
new->next = NULL;
end->next = new;
end = new;
}
return L;
}
void show(lnode t) {
{
//printf("%%%%%%%%%%%%%\n");
lnode tem;
tem =(lnode)malloc(sizeof(Node));
tem = t;
while (tem!=NULL)
{
printf("%d\n", tem->data);
tem = tem->next;
printf("\n");
}
}
}
void main() {
lnode T;
//Node *p;
T=create( );
show(T); //错因,此处的不能用&T,因为定义是指针
system("pause");
}
//无头结点
#define LINKLIST_H
typedef struct
{
int data;
struct LNode *next;
}Node,*lnode; //Node结构体名称,*lnode结构体指针类型
lnode create(){
//创建的时候用到了尾指针。但是下面的add结点没有用到。两者用的都是尾插入法
lnode L= (lnode)malloc(sizeof(Node));
lnode new,end;
int x=0;
//初始化一个列表
if (L != NULL) {
L->data = 1;
L->next = NULL;
}
end = L;
//尾插入法
for (int i = 2; i <= 6; i++) {
new = (lnode)malloc(sizeof(Node));
new->data = i;
new->next = NULL;
end->next = new;
end = new;
}
return L;
}
void show(lnode t) {
{
//printf("%%%%%%%%%%%%%\n");
lnode tem;
tem =(lnode)malloc(sizeof(Node));
tem = t;
while (tem!=NULL)
{
printf("%d\n", tem->data);
tem = tem->next;
printf("\n");
}
}
}
void main() {
lnode T;
//Node *p;
T=create( );
show(T); //错因,此处的不能用&T,因为定义是指针
system("pause");
}
相关文章推荐
- C语言实现单链表的创建和添加结点
- c语言实现对传统单链表的创建、添加 遍历 删除 反转元素操作
- C语言进阶-第18讲:单链表的遍历、创建、插入和删除结点
- [置顶] 不带头结点的 单链表的各种操作实现c语言
- 【C语言】-单链表的创建和输出
- C语言实现单链表的逆序打印(带头结点)
- C语言单链表的创建和简单使用
- C语言实现创建单链表及求长度
- c语言实现单链表的操作:创建,删除,插入,反转, 排序等
- c语言创建单链表
- 单链表的创建、销毁与K结点处插入新结点
- 单向链表之创建添加(C语言实现)
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- C语言:单链表的创建,打印及销毁
- 单链表(带头结点)的创建与倒置
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- c语言之单链表的创建及排序
- Java双向链表的创建结点,清空链表,添加结点,删除结点
- 单链表的创建和反转(C语言)
- [Object-C语言随笔之三] 类的创建和实例化以及函数的添加和调用!