数据结构:链表的实现(1)单链表的编写

step 1:单节点的创建:

struct node{
int data;
struct node *pNext;//一个结构体里包含一个指针,这个指针指向的是这个结构体的数据类型,指针指向的是数据类型是这种结构,但不一定是自己所在的这个结构
};

step2 :使用堆内存创建节点

struct node *create_node(int data){
struct node *p=(struct node*)malloc(sizeof(struct node))};
if(NULL==p){
printf("malloc error");
return NULL;}
p->data=data;
p->pNext=NULL;
return p;
}

step 3;节点连接函数

void insert_tail(struct node *pH,struct node *new)
{
struct node *p=pH;
if(NULL!=p->pNext)
p=p->pNext;

p->pNext=new;
}

step 4;构建第一个单链表

#include<stdio.h>
#include<strings.h>
#include<stdlib.h>
struct node{
int data;
struct node *pNext;
}
struct node *creatd_node(int data);
void insert_tail(struct node *pH,struct node *new);
int main()
{
struct node *pHeader=create_node(1);
insert_tail(pHeader,create_node(2));
insert_tail(pHeader,create_node(3));
printf("node1 data:%d\n",pHeader->data);
printf("node2 data:%d\n",pHeader->pNext->data);
printf("node3 data:%d\n",pHeader->pNext->pNext->data);
return 0;
}
疑问:链表的头节点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值