首页 > 其他 > 详细

临接表

时间:2014-07-03 11:30:05      阅读:443      评论:0      收藏:0      [点我收藏+]

2邻接表

  • 邻接表的C语言描述

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

 

#include<iostream>
#include<string.h>
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];
            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;
                }
            }
            cur = ljbsz;
        }
    }
    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;
        }
    }
    void dfs(Node * tem)
    {
        if(tem -> flag==1)cout<<tem->data<<" ";
        while(tem != NULL)
        {
            if(tem->next!=NULL&&visit[tem->data]==0)
            {
                visit[tem->data] = 1;
                dfs(tem->next);
            }
            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;
        cout<<temsz[0]<<" ";
        while(Front != rear)
        {
            cur = ljbsz[Front].next;
            while ( cur != NULL)
            {
                if(visit[cur->data]==0)
                {
                    cout<<cur -> data<<" ";
                    temsz[rear++] = cur -> data;
                    visit[cur->data] = 1;
                }
                cur = cur -> next;
            }
            Front ++;
        }
    }

};
int main()
{
    int j = 3;
    linjiebiao duskcl(j);
    duskcl.creat();
    int tem = 2;
    //duskcl.chazhaodian(2);
    //duskcl.chazhaoweizhi(tem);
    //duskcl.dfs(duskcl.cur);
    //duskcl.print();
    duskcl.bfs();

}

 

  

 

临接表,布布扣,bubuko.com

临接表

原文:http://www.cnblogs.com/Duskcl/p/3815687.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!