zoukankan      html  css  js  c++  java
  • 临接表

    2邻接表

    • 邻接表的C语言描述

    • 基本运算的算法——建立无向网的邻接表、求图中与顶点i邻接的第一个顶点、求图中顶点i相对于顶点j的下一个邻接点、若图G中存在顶点u,则返回该顶点在图中的位置、图的广度优先遍历、图的深度优先遍历2.

    #include<iostream>
    #include<string.h>
    #include<cstdio>
    using namespace std;
    class linjiebiao
    {
    public:
        struct Node
        {
            int data;
            Node * next;
            int flag;
            Node ():next(NULL) {}
            Node (int a,int b):data(a),flag(b),next(NULL) {}
        };
        Node * ljbsz;
        Node * cur;
        int *visit;
        int maxsize;
        linjiebiao(int tem = 10)
        {
            maxsize = tem;
            ljbsz = new Node[maxsize];
            for(int i = 0 ; i < maxsize ; i++)
            {
                //ljbsz[i].data = i;
                ljbsz[i].flag = 0;
            }
            cur = ljbsz;
            visit = new int[maxsize];
            memset(visit , 0 , sizeof(int)*maxsize);
        }
        void creat()
        {
    
            for(int i = 0 ; i < maxsize ; i++)
            {
                cout<<"please input "<<i<<" point"<<endl;
                cur = ljbsz+i;
                //freopen("in.cpp","r",stdin);
                while(1)
                {
                    int tem;
                    cin>>tem;
                    if(tem==-1)
                    {
                        cur -> next = NULL;
                        break;
                    }
                    else
                    {
                        Node * temnode = new Node(tem,1);
                        cur -> next = temnode;
                        cur = cur -> next;
                    }
                }
            }
        }
        void print()
        {
            for(int i = 0 ; i < maxsize ; i++)
            {
                cur = &ljbsz[i];
                cur = cur -> next;
                while(cur!= NULL)
                {
                    if(cur -> flag==1)cout<<cur->data<<" ";
                    cur = cur -> next;
                }
                cout<<endl;
            }
        }
        void chazhaodian(int a)//求图中与顶点i邻接的第一个顶点
        {
            cout<<ljbsz[a-1].next -> data<<endl;
        }
        void chazhaoweizhi(int a)//若图G中存在顶点u,则返回该顶点在图中的位置
        {
            cur = &ljbsz[a];
            cur = cur -> next;
            cout<<"yu "<<a<<" xiang lin de dian you:"<<endl;
            while(cur != NULL)
            {
                cout<<cur->data<<" ";
                cur = cur -> next;
            }
            cur = ljbsz;
        }
        void dfs(Node * tem)
        {
            while(tem)
            {
                if(tem->flag == 1)
                {
                    if(!visit[tem->data])
                    {
                        cout << tem->data << " ";
                        visit[tem->data] = 1;
                        dfs(ljbsz+tem->data);
                    }
                }
                tem = tem->next;
            }
        }
        void bfs()
        {
            cout<<"begining bfs!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
            int temsz[maxsize+1];
            int Front;
            int rear;
            memset( visit , 0 , sizeof(int)*maxsize);
            temsz[0] = 0;
            Front = temsz[0];
            rear = Front + 1;
            visit[0] = 1;
            while(Front != rear)
            {
    
                cur = ljbsz[temsz[Front]].next;
                while ( cur != NULL )
                {
                    if(visit[cur->data]==0)
                    {
                        temsz[rear++] = cur -> data;
                        visit[cur->data] = 1;
                    }
                    cur = cur -> next;
                }
                cout<<temsz[Front]<<" ";
                Front ++;
            }
        }
    
    };
    int main()
    {
        int j = 5;
        linjiebiao duskcl(j);
        duskcl.creat();
        int tem = 2;
        cout<<"求图中与顶点2邻接的第一个顶点"<<endl;
        duskcl.chazhaodian(2);
        cout<<endl;
        cout<<"若图G中存在顶点2,则返回该顶点在图中的位置"<<endl;
        duskcl.chazhaoweizhi(tem);
        cout<<endl;
        cout<<"图的深度优先遍历"<<endl;
        duskcl.cur = &duskcl.ljbsz[0];
        duskcl.dfs(duskcl.ljbsz);
        cout<<endl;
        cout<<"图的广度优先遍历"<<endl;
        duskcl.bfs();
    }
    

      

      

  • 相关阅读:
    linux解释器、内建和外建命令
    linux文件cat/tac/more/less/head/tail/find/vimdiff
    zk和eureka的区别(CAP原则)
    Hystrix断路器中的服务熔断与服务降级
    windows 查看端口被占用,解除占用
    JS中操作JSON总结
    Ajax请求($.ajax()为例)中data属性传参数的形式
    通过 Ajax 发送 PUT、DELETE 请求的两种实现方式
    feignclient发送get请求,传递参数为对象
    Spring Boot 和 Spring Cloud Feign调用服务及传递参数踩坑记录
  • 原文地址:https://www.cnblogs.com/Duskcl/p/3815687.html
Copyright © 2011-2022 走看看