zoukankan      html  css  js  c++  java
  • 排序算法---链表反转和成环判断

    1.链表反转

      思想:正向遍历链表,一次取出每一个节点,然后以头插的方式将节点插入,就可以逆序

    2.成环判断

      思想:定义两个指针,然后遍历链表,一个指针一次走一个节点,另一个指针一次走两个节点,如果链表成环,那么两个指针肯定有取值相等的情况;否则,遍历正常完成。

      1 #include <iostream>
      2 
      3 using namespace std;
      4 
      5 struct Node
      6 {
      7     struct Node *next;
      8     int data;
      9 };
     10 
     11 Node *createNode(int x)
     12 {
     13     Node *p = NULL;
     14     
     15     p = new Node;
     16     p->next = NULL;
     17     p->data = x;
     18     
     19     return p;
     20 }
     21 
     22 void addNode(Node **head, Node *p)
     23 {
     24     Node *temp = NULL;
     25     if(*head == NULL)
     26     {
     27         *head = p;
     28     }
     29     else
     30     {
     31         temp = *head;
     32         while(temp->next != NULL)
     33         {
     34             temp = temp->next;
     35         }
     36         temp->next = p;
     37     }
     38 }
     39 
     40 void showList(Node *head)
     41 {
     42     while(head != NULL)
     43     {
     44         cout << head->data << "  ";
     45         head = head->next;
     46     }
     47     cout << endl;
     48 }
     49 
     50 void swap(int& a, int& b)
     51 {
     52     int tmp = b;
     53     b = a;
     54     a = tmp;
     55 }
     56 
     57 // 链表反转
     58 Node *reverse(Node *head)
     59 {
     60     Node *pCur = NULL;
     61     Node *pNext = NULL;
     62     
     63     if(head == NULL || head->next == NULL)
     64     {
     65         return head;
     66     }
     67     
     68     pCur = head->next;
     69     head->next = NULL;
     70     
     71     while(pCur != NULL)
     72     {
     73         pNext = pCur->next;
     74         pCur->next = head;
     75         head = pCur;
     76         pCur = pNext;
     77     }
     78     
     79     return head;
     80 }
     81 
     82 // 链表是否成环
     83 bool isCirCle(Node *head)
     84 {
     85     bool bIsCirCle = false;
     86     Node *pFast = NULL;
     87     Node *pSlow = NULL;
     88     
     89     if(head == NULL || head->next == NULL)
     90     {
     91         return bIsCirCle;
     92     }
     93     
     94     pFast = head;
     95     pSlow = head;
     96     while(pFast->next != NULL && pFast->next->next != NULL)
     97     {
     98         pFast = pFast->next->next;
     99         pSlow = pSlow->next;
    100         
    101         if(pFast == pSlow)
    102         {
    103             bIsCirCle = true;
    104             break;
    105         }
    106     }
    107     
    108     return bIsCirCle;
    109 }
    110 
    111 int main()
    112 {
    113     Node *head = NULL;
    114     
    115     addNode(&head, createNode(2));
    116     addNode(&head, createNode(5));
    117     addNode(&head, createNode(7));
    118     addNode(&head, createNode(4));
    119     addNode(&head, createNode(6));
    120     addNode(&head, createNode(3));
    121     addNode(&head, createNode(1));
    122     addNode(&head, createNode(9));
    123     addNode(&head, createNode(8));
    124 
    125     cout << "Sort Before:" << endl;
    126     showList(head);
    127     
    128     //ListSort_1(&head);
    129     //ListSort_2(&head);
    130     ListSort_4(&head);
    131     
    132     cout << "Sort After:" << endl;
    133     showList(head);
    134     
    135     cout << "revert:" << endl;
    136     showList(reverse(head));
    137     
    138     cout << "list is circle: " << isCirCle(head) << endl;
    139 
    140     while(1);
    141     return 0;
    142 }
  • 相关阅读:
    Windows Azure Storage (17) Azure Storage读取访问地域冗余(Read Access – Geo Redundant Storage, RA-GRS)
    SQL Azure (15) SQL Azure 新的规格
    Azure China (5) 管理Azure China Powershell
    Azure China (4) 管理Azure China Storage Account
    Azure China (3) 使用Visual Studio 2013证书发布Cloud Service至Azure China
    Azure China (2) Azure China管理界面初探
    Azure China (1) Azure公有云落地中国
    SQL Azure (14) 将云端SQL Azure中的数据库备份到本地SQL Server
    [New Portal]Windows Azure Virtual Machine (23) 使用Storage Space,提高Virtual Machine磁盘的IOPS
    Android数据库升级、降级、创建(onCreate() onUpgrade() onDowngrade())的注意点
  • 原文地址:https://www.cnblogs.com/chusiyong/p/11325107.html
Copyright © 2011-2022 走看看