zoukankan      html  css  js  c++  java
  • C++实现查找链表中环的入口节点

    /*
     * 寻找链表中环的入口节点.cpp
     *
     *  Created on: 2018年4月10日
     *      Author: soyo
     */
    #include<iostream>
    using namespace std;
    struct Node{
        int num;
        Node * next;
    };
    Node * creat()
    {
        Node *head;
        Node *p;
        head=new Node;
        p=head;
        p->num=10;
        p->next=NULL;
       return head;
    }
    Node * insert(Node*head,int data)
    {
        Node *p1,*p;
        p1=new Node;
        p1->num=data;
        p1->next=NULL;
         p=head;
         while(p->next!=NULL)
         {
             p=p->next;
         }
         p->next=p1;
         return head;
    }
    Node * makeListCircle(Node *head,int n)    //n代表链表第几个节点处设置为环的入口节点
    {
        Node *p=head;
        Node *p2;   //环的入口节点
        int count=1;
        while(p->next!=NULL)
        {
            p=p->next;
            count++;
            if(count==n)
            {
                p2=p;
            }
        }
        p->next=p2;
        return head;
    }
    
    void printl(Node *head)
    {
          Node *p=head;
          while(p!=NULL)
          {
              cout<<"数据为:"<<p->num;
              p=p->next;
          }
          cout<<endl;
    }
    void printl_circle(Node *head)
    {
          Node *p=head;
          int count=0;
          while(p!=NULL)
          {
              if(count==10) break;       //控制打印的总个数(不然无限循环)
              cout<<"数据为:"<<p->num;
              p=p->next;
              count++;
          }
          cout<<endl;
    }
    Node* meetNode(Node*head)    //找到环中的节点
    {
        if(head==NULL)
            return NULL;
        Node *pslow;
        pslow=head->next;
        Node *pfast;
        pfast=pslow->next;
        while(pfast!=NULL&&pslow!=NULL)
        {
            if(pfast==pslow)
                return pfast;
            pfast=pfast->next;
            pslow=pslow->next;
            if(pfast!=NULL)
                pfast=pfast->next;
        }
       return NULL;
    }
    Node * ringEntrance(Node * head)    //找到环的入口
    {
        Node*meetN=meetNode(head);
        int count=1;
        Node *p=meetN;
        Node *p2;
        while(p->next!=meetN)//确定环中节点的数目
        {
            p=p->next;
            count++;
        }
        p=head;
        for(int i=0;i<count;i++)
        {
            p=p->next;
        }
       p2=head;
       while(p!=p2)
       {
           p=p->next;
           p2=p2->next;
       }
       return p2;
    }
    
    int main()
    {
       Node *head=creat();
      // cout<<head->num<<endl;
       int i,data;
       for(i=0;i<5;i++)
       {
           cin>>data;
           head=insert(head,data);
       }
       printl(head);
       makeListCircle(head,5);
       printl_circle(head);
       Node *p;
       p=ringEntrance(head);   //环的入口节点
       cout<<"环的入口节点的值为:"<<p->num<<endl;
    }

    结果:

    1 2 3 4 5
    数据为:10数据为:1数据为:2数据为:3数据为:4数据为:5
    数据为:10数据为:1数据为:2数据为:3数据为:4数据为:5数据为:4数据为:5数据为:4数据为:5
    环的入口节点的值为:4
  • 相关阅读:
    DBGrid的字体颜色用户自定义之模拟条件的判断(近似的实现,都是测试代码)
    DBGrid的字体颜色用户自定义之模拟条件的判断(ANDOR的优先级问题)
    Delphi中由控件坐标模拟点击控件
    Delphi中由句柄返回控件FindControl
    Qt4 QWebView的使用例子
    使用QtScript库解析Json数组例子
    将某个Qt4项目升级到Qt5遇到的问题
    Vim多窗口编辑
    Ubuntu12.04下删除文件夹内所有的.svn文件
    Putty远程登录VMware虚拟机Linux(Ubuntu12.04)
  • 原文地址:https://www.cnblogs.com/soyo/p/8780275.html
Copyright © 2011-2022 走看看