數據結構-判斷鏈表是否存在環形鏈表


1:這里有一個比較簡單的解法。設置兩個指針p1、p2。每次循環p1向前走兩步。直到p2碰到NULL指針或者兩個指針相等時結束循環。如果兩個指針相等,則說明存在環。代碼如下:

// ConsoleApplication15.cpp : 定義控制台應用程序的入口點。
//

#include "stdafx.h"
#include <malloc.h>
#include <iostream>
using namespace std;

typedef struct node//定義鏈表結構體
{
    int data;//節點內容
    node *next;//指向結構體的指針,下一個節點
}node;

node *create()//創建單鏈表
{
    int i = 0;//鏈表中數據的個數
    node *head, *p, *q;//這些的本質是節點的地址
    int x = 0;
    head = NULL;
    q = NULL;//初始化q,q代表末節點
    p = NULL;
    while (1)
    {
        printf("please input the data:");
        scanf_s("%d", &x);
        if (x == 0)
            break;//data為0時創建結束
        p = (node *)malloc(sizeof(node));//用於每次輸入鏈表的數據
        p->data = x;
        if (++i == 1)//鏈表頭的指針指向下一個節點
        {
            head = p;
            q = p;
        }
        else
        {
            q->next = p;//連接到鏈表尾端
            q = p;
        }
        q->next = NULL;/*尾結點的后繼指針為NULL(空)*/
    }
    return head;
}

int length(node *head)
{
    int len = 0;
    node *p;
    p = head->next;
    while (p != NULL)
    {
        len++;
        p = p->next;
    }
    return len;
}

void print(node *head)
{
    node *p;
    p = head;
    while (p)/*直到結點q為NULL結束循環*/
    {
        printf("%d ", p->data);/*輸出結點中的值*/
        p = p->next;/*指向下一個結點*/
    }
}

node *search_node(node *head, int pos)//查找單鏈表pos位置的節點,返回節點的指針。pos從0開始,0返回head節點
{
    node *p = head->next;
    if (pos < 0)//pos位置不正確
    {
        printf("incorrect position to search node!");//pose位置不正確
        return NULL;
    }
    if (pos == 0)
    {
        return head;
    }
    if (pos == NULL)
    {
        printf("Link is empty!");//鏈表為空
        return NULL;
    }
    while (--pos)
    {
        if ((p = p->next) == NULL)
        {
            printf("incorrect position to search node!");//超出鏈表返回
            break;
        }
    }
    return p;
}

node *insert_node(node *head, int pos, int data)//單鏈表的插入
{
    node *item = NULL;
    node *p;
    item = (node *)malloc(sizeof(node));
    item->data = data;
    if (pos == 0)//插在head后面
    {
        head->next = item;//head后面是item
        return head;
    }
    p = search_node(head, pos);//獲得pos的節點指針
    if (p != NULL)
    {
        item->next = p->next;//item指向原pos節點的后一個節點
        p->next = item;//把item插入到pos的后面
    }
    return head;
}

node *delete_node(node *head, int pos)//刪除節點
{
    node *item = NULL;
    node *p = head->next;
    if (p = NULL)
    {
        printf("link is empty!");
        return NULL;
    }
    p = search_node(head, pos - 1);//獲得位置pos節點的指針
    if (p != NULL&&p->next != NULL)
    {
        item = p->next;
        p->next = item->next;
        delete item;
    }
    return head;
}

node *reverse(node *head)//鏈表的逆置
{
    node *next;
    node *prev = NULL;
    while (head != NULL)
    {
        next = head->next;
        head->next = prev;
        prev = head;
        head = next;
    }
    return prev;
}

node *search(node *head)//尋找單鏈表的中間元素
{
    int i = 0;
    int j = 0;
    node *current = NULL;
    node *middle = NULL;
    current = middle = head->next;
    while (current != NULL)
    {
        if (i / 2 > j)
        {
            j++;
            middle = middle->next;
        }
        i++;
        current = current->next;
    }
    return middle;
}

node *InsertSort(void)//單鏈表的正向排序
{
    int data = 0;
    struct node *head = NULL, *New, *Cur, *Pre=NULL;
    while (1)
    {
        printf("please input the data\n");
        scanf_s("%d", &data);
        if (data == 0)
        {
            break;
        }
        New = (struct node*)malloc(sizeof(struct node));
        New->data = data;
        New->next = NULL;
        if (head == NULL)//第一次循環時對頭節點賦值
        {
            head = New;
            continue;
        }
        if (New->data <= head->data)
        {//head之前插入節點
            New->next = head;
            head = New;
            continue;
        }
        Cur = head;
        while (New->data > Cur->data && Cur->next != NULL)//找到需要插入的位置
        {
            Pre = Cur;
            Cur = Cur->next;
        }
        if (Cur->data >= New->data)//位置在中間
        {
            Pre->next = New;
            New->next = Cur;
        }
        else//位置在末尾
            Cur->next = New;//把New節點插到Cur之后
    }
    return head;
}

bool IsLoop(node *head, node **start)//判斷鏈表是否存在環形鏈表,start為回環開始節點的地址
{
    node *p1 = head, *p2 = head;
    if (head == NULL || head->next == NULL)//head為NULL或者鏈表為空時返回false
    {
        return false;
    }
    do
    {
        p1 = p1->next;//p1走一步
        p2 = p2->next->next;//p2走兩步
    } while (p2 && p2->next && p1 != p2);
    if (p1 == p2)
    {
        *start = p1;//p1為回環開始節點
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    bool bLoop = false;
    node *head = create();
    node *start = head->next->next->next;//使第4個節點為回環開始位置
    start->next = head->next;//回環連接到第2個節點
    node *loopStart = NULL;
    bLoop = IsLoop(head, &loopStart);
    printf("bLoop=%d\n", bLoop);
    printf("bLoop==loopStart?%d\n", (loopStart == start));
    return 0;


    

    return 0;
}
View Code

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM