判断链表是否有环

#include<iostream>
using namespace std;
// 循环链表结构体
typedef struct _Node
{
    int data;  // 数据域;
    struct _Node *next;   // 指针域;
}DNode,*DLinkList;

// 初始化函数
void InitDLinkList(DLinkList *pHead)
{
    *pHead = new DNode;
    (*pHead)->data = 0;
    (*pHead)->next = *pHead;
}

// 输出函数
void  ShowDLinkList(DLinkList pHead)
{
    DLinkList nPos = pHead;
    while (nPos->next!=pHead)
    {
        nPos = nPos->next;
        cout << nPos->data << "  ";
    }
    cout << endl;
}

// 循环链表长度(结点个数)
int GetDLinkListLength(DLinkList pHead)
{
    DLinkList nPos = pHead;
    int count = 0;
    while (nPos->next != pHead)
    {
        nPos = nPos->next;
        count++;
    }
    return count;
}

// 头插函数
void InsertHeadDLinkList(DLinkList *pHead,int value)
{
    DLinkList pNew = new DNode;
    pNew->data = value;
    // 如果头节点pHead后没有结点
    if ((*pHead)->next ==*pHead)
    {
        // 把新申请的结点,连接在头节点后;
        (*pHead)->next = pNew;
        pNew->next = *pHead;
    }
    // pHead后有节点的话
    else
    {
        // 把新申请的next,连接在头节点后的next链上;
        pNew->next = (*pHead)->next;
        // 把新申请的结点,连接在头节点后;
        (*pHead)->next = pNew;
    }
    // 销毁头节点的
}

// 尾插函数
void InsertTailDLinkList(DLinkList *pHead,int value)
{
    DLinkList pNew = new DNode;
    pNew->data = value;
    if ((*pHead)->next==*pHead)
    {  
        InsertHeadDLinkList(pHead,value);
    }
    else
    {
        DLinkList nPos = *pHead;
        while (nPos->next != *pHead)
        {
            nPos = nPos->next;
        }
        nPos->next = pNew;
        pNew->next = *pHead;
    }
}

// 头删函数
void DeleteHeadDlinkList(DLinkList* pHead)
{
    DLinkList nPos = *pHead;
    if (nPos->next==*pHead)
    {
        return;
    //    delete nPos;
    //    nPos= NULL;
    }
    else
    {
        DLinkList temp = nPos->next;
        nPos->next = temp->next;
        delete temp;
        temp = NULL;
    }
}

// 尾删函数
void DeleteTailDLinkList(DLinkList *pHead)
{
    if ((*pHead)->next==*pHead)
    {
        return;
    }
    else
    {
        DLinkList nPos = *pHead;
        while (nPos->next->next!=*pHead)
        {
            nPos = nPos->next;
        }
        DLinkList temp = nPos->next;    // 用临时变量记住倒数第二个数的结点
        nPos->next = *pHead;
        delete temp;
        temp= NULL;
    }
}

// 按下标查找结点并返回其值
int FindByIndex(DLinkList *pHead,int index)
{
    DLinkList nPos = *pHead;
    int i=0;
    if (i<0 || i>GetDLinkListLength(*pHead))
    {
        return -1;
    }
    while(i<index)
    {
        nPos= nPos->next;
        i++;
    }
    if (nPos->next==*pHead)
    {
        return -1;
    }
    return nPos->data;
}

// 按值查找并返回其下标
int FindByValue(DLinkList *pHead, int value)
{
    DLinkList nPos = *pHead;
    while (nPos->next != *pHead)
    {
        nPos = nPos->next;
    }

    if (nPos->data == value)
    {
        return nPos->data;
    }
    else
    {
        return -1;
    }
}

// 按下标插入结点
void InsertByIndex(DLinkList *pHead,int pos,int value)
{
    //  下标位置不能小于1,否则无法插入;
    if (pos<0)
    {
        return;
    }
     if(1==pos)     // 下标位置为1的时候进行头插;
    {  
        InsertHeadDLinkList(pHead,value);
        return;
    }
    if(pos>=GetDLinkListLength(*pHead))  // 当插入位置超过循环链表的长度时,进行尾插;
    {
        InsertTailDLinkList(pHead,value);
        return;
    }
    else
    {
        DLinkList nPos = *pHead;
        DLinkList pNew = new DNode;
        pNew->data = value;
        for (int i = 0; i < pos - 1; i++)
            nPos = nPos->next;
        DLinkList temp = nPos;   // 临时变量记住nPos的位置
        pNew->next = temp->next;
        nPos->next = pNew;
    }
}

// 按值下标删除函数
void DeletByIndex(DLinkList *pHead, int pos)
{
    DLinkList nPos = *pHead;
    if (1 == pos)
    {
        DeleteHeadDlinkList(pHead);
        return;
    }
    if (pos >= GetDLinkListLength(*pHead))
    {
        DeleteTailDLinkList(pHead);
        return;
    }
    for (int i = 0; i < pos - 1; i++)
        nPos = nPos->next;
    DLinkList temp = nPos->next;
    nPos->next = temp->next;
    delete temp;
    temp = NULL;
}

// 按值删除函数
void DleteByValue(DLinkList *pHead,int value)
{
    DLinkList nPos = *pHead;
    for (int i = 0; i < GetDLinkListLength(*pHead) && nPos->next->data != value; i++)
    {
        nPos = nPos->next;
    }
    DLinkList temp = nPos->next;
    nPos->next = temp->next;
    delete temp;
    temp = NULL;
}

// 销毁头节点函数
void DestoryHeadDNode(DLinkList *pHead)
{
    delete *pHead;
    *pHead = NULL;
}

// 销毁节点函数
void DestoryDLinkList(DLinkList* pHead)
{
    DLinkList nPos = *pHead;
    while (nPos->next!=*pHead)
    {
        DeleteHeadDlinkList(pHead);
    }
    if ((*pHead)->next=*pHead)
    {
        DestoryHeadDNode(pHead);
    }
}

//-------------------------------------------判断是否有环问题-------------------------------

// 尾插法创建有环链表
void CreateLoop(DLinkList *pLooop,int index)
{
    InitDLinkList(pLooop);
    for (int i = 0; i < index; i++)
    {
        InsertTailDLinkList(pLooop,i+1);
    }
    DLinkList nPos = *pLooop;
    DLinkList Tail = *pLooop;
    while (Tail->next!=*pLooop)
    {
        Tail = Tail->next;
    }
    int i = 0; // 统计循环次数;
    int temp = 0;  // 请输入要结成环的结点
    cout << "请输入要结成环的结点" << endl;
    cin >> temp;
    while (i<temp)
    {
        nPos = nPos->next;
        i++;
        if (nPos->next==*pLooop)
        {
            nPos=nPos->next;
        }
    }

    // 把循环链表的尾指针指向值为3的那个结点;
    Tail->next = nPos;
    cout << "成功创建尾插法有环链表" << endl;
}

// 头插法创建无环链表
void HeadCreateLoop(DLinkList *pLoop,int index)
{
    InitDLinkList(pLoop);
    for (int i = 0; i < index; i++)
    {
        InsertHeadDLinkList(pLoop,i+1);
    }
    DLinkList nPos = *pLoop;
    while (nPos->next != *pLoop)
    {
        nPos = nPos->next;
    }
    nPos->next = *pLoop;
}

// 判断链表是否有环 方法一:
void  IsLoop1(DLinkList *pHead)
{
    DLinkList p = *pHead,q;
    q = p;

    int CountByP = 0;  // 计算p走的步数
    int CountByQ = 0;  // 计算q走的步数
    while (1)
    {
        if (p==q&&CountByP==CountByQ)
        {
            while (p->next != *pHead)
            {
                p = p->next;
                CountByP++;
                while (q!=p)
                {
                    q = q->next;
                    CountByQ++;
                }
                if (p==q&&CountByQ<=5&&CountByP>5)
                {
                    cout << "第"<<CountByQ<<"个结点"<<endl;
                    cout << "链表有环" << endl;
                    return ;
                }
                q = *pHead;
                CountByQ = 0;
            }
            
        }
    }
//    cout << "不是带环链表" << endl;
}

// 判断链表是否有环 方法二:快慢指针法
void JudgeDLinkListIsLoop(DLinkList *pHead)
{
    
    DLinkList p = *pHead;
    DLinkList q = *pHead;

    while (1)
    {
        p = p->next;
        q = q->next->next;
        if (p->next == *pHead)
        {
            p = p->next;
        }
        if (q->next==*pHead)
        {
            q = q->next;
        }
        if (p == q&&p->next != *pHead)
        {
            cout << "p=" << p->data << ",  q=" << q->data << endl;
            cout << "链表有环" << endl;
            return;
        }
        cout << "p=" << p->data << ",  q=" << q->data << endl;
    
    }
    cout << "链表无环" << endl;
}


void test()
{
    DLinkList pLood = NULL;  // 有环链表
    DLinkList pHead = NULL;  // 无环链表
    int count = 0;   //请输入要插入结点个数
    int num = 0;  // 无环链表的结点个数
    cout << "1.创建有环来链表(尾插法): " << endl;
    cout << "2.创建无环来链表(尾插头法): " << endl;
    cout << "3.判断是否有环: " << endl;
    cout << "4.退出 :" << endl;
    int selection=0;
    cout << "请输入选项:" << endl;
    cin >> selection;
    while (selection<5)
    {
        switch (selection)
        {
        case 1:
            cout << "请输入要插入结点个数:" << endl;
            cin >> count;
            CreateLoop(&pLood, count);
            break;
        case 2:
            cout << "请输入无环链表的结点个数:" << endl;
            cin >> num;
            HeadCreateLoop(&pHead,num);
            JudgeDLinkListIsLoop(&pHead);
            break;
        case 3:
            IsLoop1(&pLood);
            break;
        case 4:
            exit(0);
            break;
        default:
            break;
        }
        cout << "请输入选项:" << endl;
        cin >> selection;
    }
    DestoryDLinkList(&pLood);
    DestoryDLinkList(&pHead);
}
int main(void)
{

test();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值