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

版权声明:未经原作者允许不得转载本文内容,否则将视为侵权 https://blog.csdn.net/springhammer/article/details/88617855

数据结构-链表逆置 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);
} 

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

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

好了,就酱紫。

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

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

猜你喜欢

转载自blog.csdn.net/springhammer/article/details/88617855