인접 표 데이터 구조 - 업데이트 중...

4378 단어
인접 표 정의: 클릭 하면 됩 니 다.
유방 향 도와 무방 향 도 를 실현 할 수 있다
#include<iostream>
#include <cstdlib>
using namespace std;

#define MAXVEX 100
typedef int VertexType;
typedef int EdgeType;

typedef struct EdgeNode
{
    int adjvex;
    struct EdgeNode *next;
} EdgeNode;

typedef struct VextexNode
{
    VertexType data;//   ,      
    int number;//    
    int visit;//         
    EdgeNode *firstedge;//      
} VextexNode, AdjList[MAXVEX];

typedef struct
{
    AdjList adjList;
    int numNodes, numEdges; //           
} GraphAdjList;
void CreateALGraph_nodir(GraphAdjList *Gp)
{
    int i, j, k;
    EdgeNode *pe;
    cout<< "       。"<<endl;
    cout << "        :" << endl;
    cin >> Gp->numNodes >> Gp->numEdges;

    for (i = 1; i <= Gp->numNodes; i++)//   
    {
        Gp->adjList[i].data=i;
        Gp->adjList[i].firstedge = NULL;
        Gp->adjList[i].number=0;
    }

    for (k = 0; k <  Gp->numEdges; k++)//  
    {
        cout<<" "<<k+1<< "       :" << endl;
        cin >> i >> j;
        Gp->adjList[i].number++;
        Gp->adjList[j].number++;
        pe = (EdgeNode *)malloc(sizeof(EdgeNode));
        pe->adjvex = j;
        pe->next = Gp->adjList[i].firstedge;
        Gp->adjList[i].firstedge = pe;
        pe = (EdgeNode *)malloc(sizeof(EdgeNode));
        pe->adjvex = i;
        pe->next = Gp->adjList[j].firstedge;
        Gp->adjList[j].firstedge = pe;
    }
     GraphAdjList *G=Gp;
     for (k = 1; k <= G->numNodes; k++){
        cout<<k<<"    "<<G->adjList[k].number*2<<endl;
        cout<<" "<<k<<"     :";
        int d=-1;
        while(G->adjList[k].firstedge){
            if(G->adjList[k].firstedge->adjvex!=d)
            cout<<G->adjList[k].firstedge->adjvex<<"   ";
            d=G->adjList[k].firstedge->adjvex;
            G->adjList[k].firstedge=G->adjList[k].firstedge->next;
        }
        cout<<endl;
     }
}
void CreateALGraph_dir(GraphAdjList *Gp)
{
    int i, j, k;
    EdgeNode *pe;
    cout<<"       。"<<endl;
    cout << "        :" << endl;
    cin >> Gp->numNodes >> Gp->numEdges;
   for (i = 1; i <= Gp->numNodes; i++)
    {
        Gp->adjList[i].data=i;
        Gp->adjList[i].firstedge = NULL;//       
        Gp->adjList[i].number =0;//     0
    }


    for (k = 0; k <  Gp->numEdges; k++)//     
    {
        cout<<" "<<k+1<< "          :" << endl;
        cin >> i >> j;
        Gp->adjList[i].number++;
        Gp->adjList[j].number++;
        pe = (EdgeNode *)malloc(sizeof(EdgeNode));
        pe->adjvex = j;//  pe               
        pe->next = Gp->adjList[i].firstedge;
        Gp->adjList[i].firstedge = pe;//           pe
    }
    GraphAdjList *G=Gp;
    for (k = 1; k <= G->numNodes; k++){
        cout<<k<<"    "<<G->adjList[k].number<<endl;
        cout<<" "<<k<<"     :";
        int d=-1;
        while(G->adjList[k].firstedge){
            if(G->adjList[k].firstedge->adjvex!=d)
            cout<<G->adjList[k].firstedge->adjvex<<"   ";
            d=G->adjList[k].firstedge->adjvex;
            G->adjList[k].firstedge=G->adjList[k].firstedge->next;
        }
        cout<<endl;
    }
}
int main(void)
{
    while(1){
    cout<<"Please enter a directed graph or undirected graph."<<endl;
    cout<<"1 indicates directed graph"<<endl<<"2 indicates nodirected graph"<<endl<<"3 indicates end"<<endl;;
    int type;
    cin>>type;
    GraphAdjList GL;
    if(type==1)
    CreateALGraph_dir(&GL);
    else if(type==2)
    CreateALGraph_nodir(&GL);
     else{
        cout<<"the program is end!"<<endl;
        break;
     }
    }
return 0;}<u>
</u>

인용 전 삼 을 사용 할 때 항상 문제 가 있 기 때문에 사용 하 는 지침 은 많은 기능 이 분리 되 지 않 았 다.그래서 코드 가 좀 못 생 겼 어 요. 업데이트 중...

좋은 웹페이지 즐겨찾기