zoukankan      html  css  js  c++  java
  • 单链表的就地逆置

      1 #include <stdio.h>
      2 #include <assert.h>
      3 #include <malloc.h>
      4 
      5 typedef struct NODE
      6 {
      7     int data;
      8 
      9     struct NODE *next;
     10 }Linklist;
     11 
     12 void Reverse(Linklist *head)
     13 {
     14     assert (NULL != head);
     15 
     16     Linklist *p1 = NULL;
     17     Linklist *p2 = NULL;
     18 
     19     if ((NULL == (head->next)) || (NULL == (head->next->next)))
     20     {
     21         return;
     22     }
     23 
     24     p1 = head->next;
     25     p2 = p1->next;
     26 
     27     while (NULL != p2)
     28     {
     29         p1->next = p2->next;
     30         p2->next = head->next;
     31         head->next = p2;
     32         p2 = p1->next;
     33     }
     34 }
     35 
     36 void initialLinklist(Linklist **head)
     37 {
     38     assert (head != NULL);
     39 
     40     if (NULL == (*head = (Linklist *)malloc(sizeof(Linklist))))
     41     {
     42         printf ("Fail to malloc space to *head!\n");
     43         return;
     44     }
     45 
     46     (*head)->next = NULL;
     47 }
     48 
     49 void CreatLinklist(Linklist *head)
     50 {
     51     assert (head != NULL);
     52 
     53     int data;
     54     Linklist *p = head;
     55 
     56     printf ("Please input NODE number, end with -1:\n");
     57     scanf ("%d", &data);
     58     while (data != -1)
     59     {
     60         if (NULL == ((p->next) = (Linklist *)malloc(sizeof(Linklist))))
     61         {
     62             printf ("Fail to malloc space to head->next!\n");
     63             return;
     64         }
     65 
     66         p = p->next;
     67         p->data = data;
     68         p->next = NULL;
     69 
     70         scanf ("%d", &data);
     71     }
     72 }
     73 
     74 void PrintLinklist(Linklist *head)
     75 {
     76     assert (head != NULL);
     77 
     78     Linklist *p = head->next;
     79 
     80     while (p != NULL)
     81     {
     82         printf ("%d ", p->data);
     83 
     84         p = p->next;
     85     }
     86 
     87     printf ("\n");
     88 }
     89 
     90 void FreeLinklist(Linklist *head)
     91 {
     92     assert (head != NULL);
     93 
     94     Linklist *p = head->next;
     95 
     96     while (p != NULL)
     97     {
     98         head->next = p->next;
     99 
    100         free (p);
    101         
    102         p = head->next;
    103     }
    104 
    105     free (head);
    106     head = NULL;
    107 }
    108 
    109 int main(void)
    110 {
    111     Linklist *head = NULL;
    112 
    113     initialLinklist (&head);
    114 
    115     CreatLinklist (head);
    116 
    117     Reverse (head);
    118 
    119     PrintLinklist (head);
    120 
    121     FreeLinklist (head);
    122 
    123     return (0);
    124 }

  • 相关阅读:
    分布式系统(五)——容错问题
    分布式系统(四)—— 一致性问题
    分布式系统(三)——选举问题
    分布式系统(二)——互斥问题
    结合中断上下文切换和进程上下文切换分析Linux内核的一般执行过程
    深入理解系统调用
    基于mykernel 2.0编写一个操作系统内核
    微信小程序npm安装pinyin库
    SpringBoot实现下载文件以及前台应当如何对接
    OSX 安装mujoco
  • 原文地址:https://www.cnblogs.com/ldjhust/p/3013797.html
Copyright © 2011-2022 走看看