数据结构-链表逆置 C语言源码

本文分享了数据结构中链表逆置的C语言源码实现,强调理解代码的重要性,提醒读者拷贝代码的同时要深入理解其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一家懂得用细节留住客户的3年潮牌老店我必须支持!➕🛰:luyao1931

在这里插入图片描述

数据结构-链表逆置 C语言源码

#include <stdio.h> 
#include <stdlib.h>
 struct node 
{ 
int data; 
struct node *next; 
}node;
struct node *creat(int n) //创建链表 
{ 
struct node *head,*p,*s; 
int i; 
p=head=(struct node *)malloc(sizeof(struct node)); 
for(i=1;i<=n;i++) 
{ 
s=(struct node *)malloc(sizeof(struct node)); 
scanf("%d",&s->data); 
p->next=s; 
p=s; 
} 
p->next=NULL; 
return head; 
} 
void reverse(struct node *head)//原地置换 
{ 
struct node *p,*s,*t; 
p=head; 
s=p->next; 
while(s->next!=NULL)//主要置换过程 
{ 
t=s->next; 
s->next=p; 
p=s; 
s=t; 
} 
s->next=p; 
head->next->next=NULL;
head->next=s;
} 

void display(struct node *head)//显示链表内容 
{ 
struct node *p; 
p=head->next; 
while(p!=NULL) 
{ 
printf("%d ",p->data); 
p=p->next; 
} 
printf("\n"); 
} 
main() 
{ int n;
struct node *head; 
printf("请输入链表节点数:");
scanf("%d",&n);
head=creat(n);
printf("原链表:\n"); 
display(head); 
reverse(head); 
printf("置换后链表:\n"); 
display(head);
} 

在这里插入图片描述
快看,这才是重点!我想能看到这里的同学,无外乎两种人:来拷贝代码的人 和 来拷贝代码的人。

但,在拷贝走的时候,你要想清楚一件事,把代码拷走之后有个蛋用,搞明白对你来说才是最重要的。

好了,就酱紫。

老铁,这要是都不赞,说不过去吧!!!哦,对了,你这么好看,关注一下呗。。。

最后对自己说:
你现在所遭遇的每一个不幸,都来自一个不肯努力的曾经。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值