您的位置:首页 > 编程语言 > C语言/C++

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");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: