有趣的图十字链表创建和遍历算法

前言

十字链表是图结构数据算法比较好的操作形式,在此基础上可以实现很多有价值的算法。注意:此算法还并未进行完整性测试,同时提醒里边埋藏了典型的溢出漏洞,供对信息安全的朋友参考。

//
//  Gr.hpp
//  GR
//
//  Created by 韩立国 on 2021/10/31.
//

#ifndef Gr_hpp
#define Gr_hpp

#include <stdio.h>

struct Gr_Note{
    struct Gr_Arc*first_Arc;
    char Date[20];
    struct Gr_Arc*back_Arc;
    bool tag;
};

struct Gr_Arc{
    struct Gr_Note *Gr_Arc_Head;
    struct Gr_Note *Gr_Arc_Tail;
    struct Gr_Arc *Gr_head_N;
    struct Gr_Arc *Gr_tail_N;
    unsigned int info;
    bool tag;
};

extern bool Create_Gr(struct Gr_Note *Gr_Arry,struct Gr_Arc *Gr_Arc_arc ,int N,int M);
//extern int *create_Arc(struct Gr_Note *Gr_ArryT);

#endif /* Gr_hpp */
//
//  Gr.cpp
//  GR
//
//  Created by 韩立国 on 2021/10/31.
//
#include <iostream>
#include "Gr.hpp"
#include <string.h>
using namespace std;

struct Gr_Note Gr_ArrS[600000];
//Gr_Note HiNote={0,0,0};
//Gr_Arc  HiArc={0,0,0,0,0};

bool Create_Gr(struct Gr_Note *Gr_ArrS,struct Gr_Arc *Gr_Arc_arc ,int N,int M)
{
    int i;
    char Dateee[20];
    bool Gatekeeper=false;
    cout<<"I will create a Cross linked list:  \n";

    for(i=0;i<N;i++)
    {
        cout<<"Plaase put the Head date :     "<<i<<"\n";
        cin>>Dateee;
        strcpy(Gr_ArrS[i].Date,Dateee);
        Gr_ArrS[i].first_Arc=NULL;
        cout<<"Head  first_Arc  is :"<<Gr_ArrS[i].first_Arc<<"\n";
        Gr_ArrS[i].back_Arc=NULL;
        cout<<"Head  Back_Arc  is :"<<Gr_ArrS[i].back_Arc<<"\n";
    }
    char note[20],note_one[20],note_two[20];
    Gr_Arc *Next_Arc=NULL;
    Gr_Arc *Midll_Arc=NULL;
    int j;
    for(j=0;j<M;j++)
    {
        Gr_Arc *Gr_Arc_arc_arc=new Gr_Arc;//Dyns request a Arc
        Gr_Arc_arc_arc->Gr_Arc_Tail=NULL;
        Gr_Arc_arc_arc->Gr_Arc_Head=NULL;
        Gr_Arc_arc_arc->Gr_head_N=NULL;
        Gr_Arc_arc_arc->Gr_tail_N=NULL;
        Gr_Arc_arc_arc->info=NULL;
       
//put in a head

        cout<<"Please put in the arc head-note  :\n";
        cin>>note;
        
        for(i=0;i<N;i++)                       //check you put is a exsit!
        {
            if(strcmp(note,Gr_ArrS[i].Date)==0)
            {cout<<"U Are  put a exsit char !\n";
                Gatekeeper=true;
                break;
            }else{Gatekeeper=false;}
        }
        if(Gatekeeper==false)
        {cout<<"You are an idiot, you typed it wrong!!!!!!\n";
            delete Gr_Arc_arc_arc;
            break;}
        strcpy(note_one,note);
                
            for(i=0;i<N;i++)
            {
            if(strcmp(note,Gr_ArrS[i].Date)==0)
            {
            if(Gr_ArrS[i].first_Arc==NULL)
            {
                Gr_ArrS[i].first_Arc=Gr_Arc_arc_arc;    //put the Arc address in the note
                Gr_Arc_arc_arc->Gr_Arc_Head=&Gr_ArrS[i]; //put the note address in the arc
                cout<<"Gr_ArrS.first_Arc is :"<<Gr_ArrS[i].first_Arc<<"\n";
                cout<<"note   is:"<<i<<"\n";
            }else
            {
                Gr_Arc_arc_arc->Gr_Arc_Head=&Gr_ArrS[i];
                cout<<"Gr_Arc_arc_arc->Gr_Arc_Head is :"<<Gr_Arc_arc_arc->Gr_Arc_Head<<"\n";
                Midll_Arc=Gr_ArrS[i].first_Arc;
                while(Midll_Arc->Gr_head_N!=NULL)
                {
                    Next_Arc=Midll_Arc->Gr_head_N;
                    Midll_Arc=Next_Arc->Gr_head_N;
                }
                Midll_Arc->Gr_head_N=Gr_Arc_arc_arc;
            }
            }
            }
        
//put in a tail

        cout<<"Please put in the arc tail-note  :\n";
        cin>>note;
        
        for(i=0;i<N;i++)                       //check you put is a exsit!
        {
            if(strcmp(note,Gr_ArrS[i].Date)==0)
            {
                cout<<"U Are  put a exsit char !\n";
                Gatekeeper=true;
                break;
            }else{Gatekeeper=false;}
        }
        if(Gatekeeper==false)
        {cout<<"You are an idiot, you typed it wrong!!!!!!\n";
            delete Gr_Arc_arc_arc;
            break;}
        
        strcpy(note_two,note);
                
            for(i=0;i<N;i++)
            {
                if(strcmp(note,Gr_ArrS[i].Date)==0)
                {

            if(Gr_ArrS[i].back_Arc==NULL)
            { Gr_ArrS[i].back_Arc=Gr_Arc_arc_arc;
                Gr_Arc_arc_arc->Gr_Arc_Tail=&Gr_ArrS[i];
                cout<<"Gr_ArrS.back_Arc is :"<<Gr_ArrS[i].back_Arc<<"\n";
                cout<<"note is :"<<i<<"\n";

            }else
            {
                Gr_Arc_arc_arc->Gr_Arc_Tail=&Gr_ArrS[i];
                cout<<"Gr_Arc_arc_arc->Gr_Arc_Tail    is:"<<Gr_Arc_arc_arc->Gr_Arc_Tail<<"\n";
                Midll_Arc=Gr_ArrS[i].back_Arc;
                while(Midll_Arc->Gr_tail_N!=NULL)
                {
                    Next_Arc=Midll_Arc->Gr_tail_N;
                    Midll_Arc=Next_Arc->Gr_tail_N;
                }
                Midll_Arc->Gr_tail_N=Gr_Arc_arc_arc;
            }
                }
            }
        
        cout<<"PUT IN the ARC INFO  !!!!!!:\n";
        cin>>Gr_Arc_arc_arc->info;
        cout<<"IS          :"<<Gr_Arc_arc_arc->info;
        cout<<"the arc is:       "<<note_one<<"---"<<Gr_Arc_arc_arc->info<<"--->"<<note_two<<"\n";
        cout<<"yes !  U are PUt in a ARc ,please next !"<<Gr_Arc_arc_arc->info<<"\n";
        memset(note_one,'\0',sizeof(note_one));
        memset(note_two,'\0',sizeof(note_two));
        //Next_Arc=Gr_Arc_arc_arc;
    }
    return Gatekeeper;
    //
}

//
//  main.cpp
//  GR
//
//  Created by 韩立国 on 2021/10/31.
//

#include <iostream>
#include "Gr.hpp"
using namespace std;

int main(int argc, const char * argv[])
{
    
    struct Gr_Note Gr_ArrS[100];
    struct Gr_Note *Note_Point=NULL;
    struct Gr_Arc  *Arc_Point=NULL;
    int q;
    
    if(bool Auditor=Create_Gr(Gr_ArrS,0 ,4,4))
    {
        cout<<"Great, you created a cross-linked list!\n";
        
    }else
    {
        cout<<"You failed hahahaha!!!!!!\n";
    }
  
    
    for(q=0;q<4;q++)    //Traverse the cross-linked list space!
    {
        cout<<Gr_ArrS[q].Date<<"\n";
           
        if(Gr_ArrS[q].first_Arc!=NULL&&Gr_ArrS[q].back_Arc!=NULL)
        {
 
            Arc_Point=Gr_ArrS[q].first_Arc;
            
            do{
                 if(Arc_Point->info!=0)
                 
                      Arc_Point->tag=true;
                      Note_Point=Arc_Point->Gr_Arc_Tail;
                         if(Note_Point->first_Arc!=NULL)
                         {
                          cout<<"next note is :"<<Note_Point->Date<<"\n";
                          cout<<"the arc info is :"<<Arc_Point->info<<"\n";
                          Arc_Point=Note_Point->first_Arc;
                          continue;
                         }else{break;}
            }while(Arc_Point->Gr_Arc_Tail!=NULL&&Note_Point->first_Arc!=NULL&&Arc_Point->tag==false);
        
            Arc_Point=NULL;
            Note_Point=NULL;
            Arc_Point=Gr_ArrS[q].first_Arc;
            
            do{
                  if(Arc_Point->info!=0)
                     
                      Arc_Point->tag=false;
                      Note_Point=Arc_Point->Gr_Arc_Tail;
                         if(Note_Point->first_Arc!=NULL)
                         {
                         Arc_Point=Note_Point->first_Arc;
                         continue;
                         }else{break;}
            }while(Arc_Point->Gr_Arc_Tail!=NULL&&Note_Point->first_Arc!=NULL&&Arc_Point->tag==true);
        
            Arc_Point=NULL;
            Note_Point=NULL;
            
          //if(Gr_ArrS[q].back_Arc!=NULL)
          //{
            
            Arc_Point=Gr_ArrS[q].back_Arc;
        
            do{
                 if(Arc_Point->info!=0)
                 
                 Arc_Point->tag=true;
                 Note_Point=Arc_Point->Gr_Arc_Head;
                     if(Note_Point->back_Arc!=NULL)
                     {
                     cout<<"front note is :"<<Note_Point->Date<<"\n";
                     cout<<"the arc info is :"<<Arc_Point->info<<"\n";
                     Arc_Point=Note_Point->back_Arc;
                     continue;
                     }else{break;}

             }while(Arc_Point->Gr_Arc_Head!=NULL&&Note_Point->back_Arc!=NULL&&Arc_Point->tag==false);

             Arc_Point=NULL;
             Note_Point=NULL;
             Arc_Point=Gr_ArrS[q].back_Arc;
            
             do{
                  if(Arc_Point->info!=0)
                  
                  Arc_Point->tag=false;
                  Note_Point=Arc_Point->Gr_Arc_Head;
                      if(Note_Point->back_Arc!=NULL)
                      {
                      Arc_Point=Note_Point->back_Arc;
                      continue;
                      }else{break;}
             }while(Arc_Point->Gr_Arc_Head!=NULL&&Note_Point->back_Arc!=NULL&&Arc_Point->tag==true);
            Arc_Point=NULL;
            Note_Point=NULL;
         // }
            
        }else if (Gr_ArrS[q].first_Arc==NULL&&Gr_ArrS[q].back_Arc!=NULL)
        {
            Arc_Point=Gr_ArrS[q].back_Arc;
        
            do{
                 if(Arc_Point->info!=0)
                 
                 Arc_Point->tag=true;
                 Note_Point=Arc_Point->Gr_Arc_Head;
                     if(Note_Point->back_Arc!=NULL)
                     {
                     cout<<"front note is :"<<Note_Point->Date<<"\n";
                     cout<<"the arc info is :"<<Arc_Point->info<<"\n";
                     Arc_Point=Note_Point->back_Arc;
                     continue;
                     }else{break;}

             }while(Arc_Point->Gr_Arc_Head!=NULL&&Note_Point->back_Arc!=NULL&&Arc_Point->tag==false);

             Arc_Point=NULL;
             Note_Point=NULL;
             Arc_Point=Gr_ArrS[q].back_Arc;
            
             do{
                  if(Arc_Point->info!=0)
                  
                  Arc_Point->tag=false;
                  Note_Point=Arc_Point->Gr_Arc_Head;
                      if(Note_Point->back_Arc!=NULL)
                      {
                      Arc_Point=Note_Point->back_Arc;
                      continue;
                      }else{break;}
             }while(Arc_Point->Gr_Arc_Head!=NULL&&Note_Point->back_Arc!=NULL&&Arc_Point->tag==true);
            Arc_Point=NULL;
            Note_Point=NULL;
            
        }else if(Gr_ArrS[q].first_Arc!=NULL&&Gr_ArrS[q].back_Arc==NULL){
            
                       Arc_Point=Gr_ArrS[q].first_Arc;
                       
                       do{
                            if(Arc_Point->info!=0)
                            
                                 Arc_Point->tag=true;
                                 Note_Point=Arc_Point->Gr_Arc_Tail;
                                    if(Note_Point->first_Arc!=NULL)
                                    {
                                     cout<<"next note is :"<<Note_Point->Date<<"\n";
                                     cout<<"the arc info is :"<<Arc_Point->info<<"\n";
                                     Arc_Point=Note_Point->first_Arc;
                                     continue;
                                    }else{break;}
                       }while(Arc_Point->Gr_Arc_Tail!=NULL&&Note_Point->first_Arc!=NULL&&Arc_Point->tag==false);
                   
                       Arc_Point=NULL;
                       Note_Point=NULL;
                       Arc_Point=Gr_ArrS[q].first_Arc;
                       
                       do{
                             if(Arc_Point->info!=0)
                                
                                 Arc_Point->tag=false;
                                 Note_Point=Arc_Point->Gr_Arc_Tail;
                                    if(Note_Point->first_Arc!=NULL)
                                    {
                                    Arc_Point=Note_Point->first_Arc;
                                    continue;
                                    }else{break;}
                       }while(Arc_Point->Gr_Arc_Tail!=NULL&&Note_Point->first_Arc!=NULL&&Arc_Point->tag==true);
                   
                       Arc_Point=NULL;
                       Note_Point=NULL;
        }else if ((Gr_ArrS[q].first_Arc==NULL&&Gr_ArrS[q].back_Arc==NULL))
            
        {
            Arc_Point=NULL;
            Note_Point=NULL;
            continue;}
    }
    
    return 0;
}
I will create a Cross linked list:  
Plaase put the Head date :     0
guangzhou
Head  first_Arc  is :0x0
Head  Back_Arc  is :0x0
Plaase put the Head date :     1
beijing
Head  first_Arc  is :0x0
Head  Back_Arc  is :0x0
Plaase put the Head date :     2
shanghai
Head  first_Arc  is :0x0
Head  Back_Arc  is :0x0
Plaase put the Head date :     3
chengdu
Head  first_Arc  is :0x0
Head  Back_Arc  is :0x0
Please put in the arc head-note  :
beijing
U Are  put a exsit char !
U Are  put a exsit char !
Gr_ArrS.first_Arc is :0x108824a20
note   is:1
Please put in the arc tail-note  :
chengdu
U Are  put a exsit char !
Gr_ArrS.back_Arc is :0x108824a20
note is :3
PUT IN the ARC INFO  !!!!!!:
4000
IS          :4000the arc is:       beijing---4000--->chengdu
yes !  U are PUt in a ARc ,please next !4000
Please put in the arc head-note  :
shanghai
U Are  put a exsit char !
U Are  put a exsit char !
Gr_ArrS.first_Arc is :0x10262a960
note   is:2
Please put in the arc tail-note  :
guangzhou
U Are  put a exsit char !
Gr_ArrS.back_Arc is :0x10262a960
note is :0
PUT IN the ARC INFO  !!!!!!:
3000
IS          :3000the arc is:       shanghai---3000--->guangzhou
yes !  U are PUt in a ARc ,please next !3000
Please put in the arc head-note  :
chengdu
U Are  put a exsit char !
U Are  put a exsit char !
Gr_ArrS.first_Arc is :0x1022373d0
note   is:3
Please put in the arc tail-note  :
guangzhou
U Are  put a exsit char !
Gr_Arc_arc_arc->Gr_Arc_Tail    is:0x7ff7bfefe0f0
PUT IN the ARC INFO  !!!!!!:
2500
IS          :2500the arc is:       chengdu---2500--->guangzhou
yes !  U are PUt in a ARc ,please next !2500
Please put in the arc head-note  :
guangzhou
U Are  put a exsit char !
U Are  put a exsit char !
Gr_ArrS.first_Arc is :0x102237400
note   is:0
Please put in the arc tail-note  :
beijing
U Are  put a exsit char !
Gr_ArrS.back_Arc is :0x102237400
note is :1
PUT IN the ARC INFO  !!!!!!:
6000
IS          :6000the arc is:       guangzhou---6000--->beijing
yes !  U are PUt in a ARc ,please next !6000
Great, you created a cross-linked list!
guangzhou
next note is :beijing
the arc info is :6000
next note is :chengdu
the arc info is :4000
beijing
next note is :chengdu
the arc info is :4000
next note is :guangzhou
the arc info is :2500
next note is :beijing
the arc info is :6000
front note is :guangzhou
the arc info is :6000
shanghai
next note is :guangzhou
the arc info is :3000
next note is :beijing
the arc info is :6000
next note is :chengdu
the arc info is :4000
next note is :guangzhou
the arc info is :2500
chengdu
next note is :guangzhou
the arc info is :2500
next note is :beijing
the arc info is :6000
next note is :chengdu
the arc info is :4000
front note is :beijing
the arc info is :4000
front note is :guangzhou
the arc info is :6000
Program ended with exit code: 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值