版权声明:转载请说明出处 https://blog.csdn.net/qq_33166886/article/details/86641068
链表逆序输出元素,采用单、双向链表实现
逆序输出方法又好几种,个人认为,在不开辟其他空间的条件下,实现的原理都是差不多的(使用链表的情况),其根本就是改变链表头,下面的代码在不开辟其他空间的条件实现,另外,编写程序时,特别注意特殊情况下是否能正常运行(其实完全用不到双向链表)
双向链表(没有头节点)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Link{
int data;
struct Link *prior;
struct Link *next;
}link;
link *init(int n){
link *head = (link *)malloc(sizeof(link));
if(head == NULL){
printf("init error! malloc failed \n");
return NULL;
}
head->next = NULL;
head->data = 1;
head->prior = NULL;
link *temp = head;
for(int i=2;i<=n;i++){
link *p = (link *)malloc(sizeof(link));
p->data = rand()%10;//产生n个随机数
p->next = NULL;
p->prior = temp;
temp->next = p;
temp = temp->next;
}
return head;
}
void show(link *head){
link *temp = head;
while(temp){
printf(" %d ",temp->data);
temp = temp->next;
}
printf("\n");
}
link *reversal(link *head){//反转函数 cur为temp的下一个数据元素,temp不变动,每一次循环将temp的下一个元素插入到链表头即可,链表没有头节点,这点注意一下
link *temp = head;
link *cur = temp;
while(temp->next){//temp为当前链表的最后一个数据时,不再继续
cur = temp->next;
if(cur->next!=NULL){//删除cur结点
temp->next = cur->next;
cur->next->prior = temp;
}else{
temp->next = NULL;
}
//将cur插入到链表头,更新链表头
head->prior = cur;
cur->next = head;
head = cur;
}
return head;
}
int main(){
link *head = init(6);
show(head);
head = reversal(head);
show(head);
return 0;
}
输出结果
root@ubuntu:/c_language/project/link_project# vim reversal.c
root@ubuntu:/c_language/project/link_project# gcc -o reversal reversal.c
root@ubuntu:/c_language/project/link_project# ./reversal
1 3 6 7 5 3
3 5 7 6 3 1
root@ubuntu:/c_language/project/link_project#
单链表(没有头节点)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Link{
int data;
struct Link *next;
}link;
link *init(int n){
link *head = (link *)malloc(sizeof(link));
if(head == NULL){
printf("init error! malloc failed \n");
return NULL;
}
head->next = NULL;
head->data = 1;
link *temp = head;
for(int i=2;i<=n;i++){
link *p = (link *)malloc(sizeof(link));
p->data = rand()%10;
p->next = NULL;
temp->next = p;
temp = temp->next;
}
return head;
}
void show(link *head){
link *temp = head;
while(temp){
printf(" %d ",temp->data);
temp = temp->next;
}
printf("\n");
}
link *reversal(link *head){
link *temp = head;
link *cur = temp;
while(temp->next){
cur = temp->next;
if(cur->next!=NULL){
temp->next = cur->next;
}else{
temp->next = NULL;
}
cur->next = head;
head = cur;
}
return head;
}
int main(){
link *head = init(6);
show(head);
head = reversal(head);
show(head);
return 0;
}
输出结果
root@ubuntu:/c_language/project/link_project# ./reversal_singel
1 3 6 7 5 3
3 5 7 6 3 1
root@ubuntu:/c_language/project/link_project# gedit reversal_singel.c
可见结果是一样的
单链表(有头节点)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Link{
int data;
struct Link *next;
}link;
link *init(int n){
link *head = (link *)malloc(sizeof(link));
if(head == NULL){
printf("init error! malloc failed \n");
return NULL;
}
head->next = NULL;
head->data = NULL;//区别
link *temp = head;
for(int i=1;i<=n;i++){
link *p = (link *)malloc(sizeof(link));
p->data = rand()%10;
p->next = NULL;
temp->next = p;
temp = temp->next;
}
return head;
}
void show(link *head);
link *reversal(link *head){
link *temp = head->next;//区别
link *cur = temp->next;//区别
while(temp->next){
cur = temp->next;
if(cur->next!=NULL){
temp->next = cur->next;
}else{
temp->next = NULL;
}
cur->next = head->next;
head->next = cur;
}
return head;
}
void show(link *head){
link *temp = head->next;//区别
while(temp){
printf(" %d ",temp->data);
temp = temp->next;
}
printf("\n");
}
int main(){
link *head = init(6);
show(head);
head = reversal(head);
show(head);
return 0;
}
输出结果
root@ubuntu:/c_language/project/link_project# ./reversal_head
3 6 7 5 3 5
5 3 5 7 6 3
root@ubuntu:/c_language/project/link_project#