zoukankan      html  css  js  c++  java
  • 一个邻接表

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #define MAX 20
    typedef struct node
    {
        struct node * nextarc;
        char Info;
    }ArcNode,*pArcNode;
    typedef struct
    {
        int kind;        //类型
        int Degree;   //度数
        int VerCount;//顶点数目
        int ArcCount;    //弧的数目
        pArcNode Adj;
    }AdjList,*pAdjList;
    void CreatAdj(pAdjList myAdjList)
    {
        int i,j;
        char tail, head;
        pArcNode p,q;
        myAdjList->Degree = 0;
        printf("Please input the kind of Adjacency List :1-Digraph    2-Undigraph");//表的种类
        scanf("%d", &myAdjList->kind);
        if (myAdjList->kind != 1 && myAdjList->kind != 2)
            return ;
        printf("Please input the num of Vertex:");//顶点个数
        scanf("%d", &myAdjList->VerCount);
        printf("Pleaase input the num of Arc");//弧的个数
        scanf("%d", &myAdjList->ArcCount);
        myAdjList->Degree = 0;                //度数初始化为0
    
        myAdjList->Adj = (pArcNode)malloc(myAdjList->VerCount  *sizeof(ArcNode));
        for (i = 0; i < myAdjList->VerCount; i++)
            myAdjList->Adj[i].nextarc = NULL;    
    
        printf("Please input the Graph: 
    ");
        printf("Please input all of the Vertex:
    ");
        fflush(stdin);
        for (i = 0; i < myAdjList->VerCount; i++)
        {
            scanf("%c", &myAdjList->Adj[i].Info);
            fflush(stdin);
        }
        printf("Please input the Arc:
    ");
        for (i = 0; i < myAdjList->ArcCount; i++)
        {
            printf("Please input tail of Arc%d :", i);
            scanf("%c", &tail);        
            p = (pArcNode)malloc(sizeof(ArcNode));
            p->nextarc = NULL;
            fflush(stdin);
            printf("Please input head of Arc%d :", i);
            scanf("%c", &p->Info);
            getchar();
            for ( j = 0; j < myAdjList->VerCount; j++)
            {
                if (myAdjList->Adj[j].Info == tail)
                {
                    q = &myAdjList->Adj[j];
                    while (q->nextarc != NULL)
                        q = q->nextarc;
    
                    q->nextarc = p;
                }
            }
            
        }
    
    }
    void Print(pAdjList myAdjList)
    {
        int i = 0, j = 0, count = 0;
        pArcNode p1,p2;
        for (i = 0; i < myAdjList->ArcCount; i++)
        {
            p1 = &myAdjList->Adj[i];
            p2 = p1;
        
            while (p1->nextarc != NULL)
            {
                printf("%c--%c
    ",p2->Info,p1->nextarc->Info);
                count++;
                p1 = p1->nextarc;
            }
            
            if (count>myAdjList->Degree)
                myAdjList->Degree = count;
            count = 0;
        }
        if (myAdjList->Degree == 0)
            printf("Not Creat
    ");
        else
            printf("Degree is %d
    ", myAdjList->Degree+1);
    }    
    bool Judge(char tail, char head, pAdjList myAdjList)
    {
        bool findhead=false,findtail = false;
        int i = 0;
        pArcNode p;
        for (i = 0; i < myAdjList->VerCount; i++)
        {
            if (tail == myAdjList->Adj[i].Info)
                findtail = true;
            if (findtail == true)
            {
                p = &myAdjList->Adj[i];
                while (p)
                {
                    if (p->Info == head)
                    {
                        findhead = true;
                        return true;
                    }
                    else
                        p = p->nextarc;
                }
                return
                    false;
            }
    
        }
        return false;
    }
    int main()
    {
        AdjList myAdjList;
        char tail, head;
        bool find=false;
        int choice = 0;
        while (1)
        {
            printf("Please input your choice :  1-Creat   2-Print  3-Judge");
            scanf("%d", &choice);
            switch (choice)
            {
            case 1:
                CreatAdj(&myAdjList);
                break;
            case 2:
                Print(&myAdjList);
                break;
            case 3:
                printf("Please input the tail:");
                fflush(stdin);
                scanf("%c", &tail);
                fflush(stdin);
                printf("Please input the head:");
                scanf("%c", &head);
                getchar();
                find=Judge(tail,head,&myAdjList);
                if (find == true)
                    printf("Have Found
    ");
                else
                    printf("Not Found
    ");
                break;
            default:
                return 0;
            }
        }
    
    
    
        system("pause");
        return 0;
    }
  • 相关阅读:
    七、阿里巴巴中文站商品信息如何存放
    四、为什么要使用NOSQL NOT ONLY SQL
    二、数据库架构发展历程
    十三、负载均衡
    三、MySQL的扩展性瓶颈
    一、秒杀架构设计
    数据库概述
    五、传统RDBMS VS NOSQL
    PHP框架开发:三、MVC设计模式及本框架的实现方式
    PHP Iterator的使用
  • 原文地址:https://www.cnblogs.com/da-peng/p/5004363.html
Copyright © 2011-2022 走看看