(一)链表学习之简单链表基本认识

1.简单静态链表定义

struct Node{
int num;
struct Node *pNext
};

int main()
{
	struct Node *p=NULL;//定义头指针指向第一个节点
	struct Node n1,n2,n3,n4,n5;
	n1.num=1;
	n2.num=2;
	n3.num=3;
	n4.num=4;
	n5.num=5;
	p=&n1;
	n1.pNext=&n2;//实现链表的串联
	n2.pNext=&n3;
	n3.pNext=&n4;
	n4.pNext=&n5;
	n5.pNext=NULL;
    
    //链表的访问
    struct Node *px=p;//创建一个指针副本保存头节点的地址
    while(px!=NULL)//如果这里用p,就只能访问一次,后面p为NULL了
    {
    	printf("%d",px->num);
    	if(px->num==10)//链表修改
    	{
    		px->num=8;
    	}
    	px=px->pNext;
    }
    //链表删除
    n2.pNext=n3.pNext;//删除n3
    //尾部插入
    struct Node n6;
    n6.num=6;
    n6.pNext=NULL;
    n5.pNext=&n6;
    // //2-4之间插入一个节点,10
    struct Node n7;
    n7.num=10;
    n7.pNext=n2.pNext;//测试看看应该就是&n4 
    n2.pNext=&n7;
}

2.简单动态链表定义

struct Node{
int num;
struct Node *pNext
};

int main()
{
	struct Node *p=NULL;//定义头指针指向第一个节点
	struct Node *p1,*p2,*p3,*p4,*p5;
	p1=(struct Node*)malloc(sizeof(struct Node));
	p2=(struct Node*)malloc(sizeof(struct Node));
	p3=(struct Node*)malloc(sizeof(struct Node));
	p4=(struct Node*)malloc(sizeof(struct Node));
	p5=(struct Node*)malloc(sizeof(struct Node));
}
//其余部分同上,记住free掉就行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值