单链表的逆操作2

xiaoxiao2021-02-28  99

头结点没有数据的单链表

方法1:

LinkList reverse_list(LinkList list)   {       LinkedNode *pre = list;       LinkedNode *cur = list->next;       LinkedNode *next = NULL;          if(list == NULL || list->next == NULL)           return;       next=cur->next;     cur->next = NULL; pre=cur; cur=next;      /*在这里实现翻转*/       while(cur != NULL)       {           next = cur->next;           cur->next = pre;           pre = cur;           cur = next;       }       list->next = pre;       return list;   }  

方法2,3

也可以直接调用上一讲头结点有数据的逆置操作

完整代码如下:

#include <stdio.h>   #include <malloc.h>      typedef int DataType;   typedef struct node   {       DataType data;       struct node *next;   }LinkedNode, *LinkList;   LinkList create_list()   {       DataType value[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};       int len = sizeof(value) / sizeof(DataType);       int i = 0;       LinkedNode *list_head = NULL;       LinkedNode *temp = NULL;       LinkedNode *p = NULL;          list_head = (LinkedNode*)malloc(sizeof(LinkedNode));       //list_head->data = value[0];       list_head->next = NULL;          temp = list_head;       for(i = 0; i < len; i++)       {           while (temp->next != NULL)             {                 temp = temp->next;             }             p = (LinkedNode*)malloc(sizeof(LinkedNode));           p->data = value[i];           p->next = NULL;           temp->next = p;       }       return list_head;   }   void print(LinkList list)   {       LinkedNode *temp = NULL;       if(list == NULL)           return;              temp = list->next;       while(temp != NULL)       {           printf("%d  ", temp->data);           temp = temp->next;       }       printf("\n");       return;   }   LinkList reverse_list(LinkList list)   {       LinkedNode *pre = list;       LinkedNode *cur = list->next;       LinkedNode *next = NULL;          if(list == NULL || list->next == NULL)           return;       next=cur->next;     cur->next = NULL; pre=cur; cur=next;      /*在这里实现翻转*/       while(cur != NULL)       {           next = cur->next;           cur->next = pre;           pre = cur;           cur = next;       }       list->next = pre;       return list;   }   LinkList reverse_list2(LinkList list)   {       LinkedNode *pre = list;       LinkedNode *cur = list->next;       LinkedNode *next = NULL;          if(list == NULL || list->next == NULL)           return;              /*在这里实现翻转*/       while(cur != NULL)       {           next = cur->next;           cur->next = pre;           pre = cur;           cur = next;       }       list->next = NULL;       list = pre;         return  list;   }  void reverse_the_list(LinkedNode *cur, LinkList *list)   {       LinkedNode *next = NULL;       if(cur == NULL || cur->next == NULL)       {           *list = cur;       }       else       {           next = cur->next;           reverse_the_list(next, list);           next->next = cur;           cur->next = NULL;       }       return;   }   int main()   {       LinkList list = NULL;       LinkedNode *temp = NULL;       LinkedNode *temp1 = NULL;      LinkList list2 = NULL;       LinkList list3 = NULL;       list = create_list();       print(list);       list=reverse_list(list);       printf("after first reverse:\n");     print(list);      temp=list->next;     temp1=list;     temp=reverse_list2(temp);     temp1->next=temp;     printf("after second reverse:\n");     print(temp1);       list2=temp1->next;      list3=temp1->next;     temp = temp1;     reverse_the_list(list2, &list3);       temp->next=list3;     printf("after third reverse:\n");     print(temp);     return 0;   } 

实验结果:

转载请注明原文地址: https://www.6miu.com/read-19657.html

最新回复(0)