利用十字链表存储树结构(便于同时求出某一点的入度与出度)------C语言实现

#include <stdio.h>
#include<conio.h>
#include<stdlib.h>

/*
利用十字链表存储有向图,可用于同时查找某个顶点的出度与入度;
*/

typedef struct edge {//顶点表
    int headvex, tailvex;//headvex弧的七点在顶点表中的下标,tailvex是边的重点在顶点表中的下标
    edge *headlink, *taillink;//headlink指向与边相同起点的边,taillink指向与边相同终点的边
}Edge;

typedef struct vex {//边表
    int data;//顶点表数据
    Edge *firstin, *firstout;//firstin表示指向第一个入边,firstout指向第一个出边
}Vex;

typedef struct vexedge {
    Vex ve[100];
    int vexnum, edgenum;
}Vexedge;

//创建表

void create(Vexedge *v) {
    Edge *e;
    int i, j, k;
    printf("请输入顶点个数与边的条数:");
    scanf("%d%d", &v->vexnum, &v->edgenum);
    for (i = 0; i<v->vexnum; i++) {//初始化顶点表
        printf("请输入顶点的值:");
        scanf("%d", &v->ve[i].data);
        v->ve[i].data = getchar();
        v->ve[i].firstin = NULL;
        v->ve[i].firstout = NULL;
    }
    for (k = 0; k<v->edgenum; k++) {//初始化边表
        printf("请输入边的顶点坐标");
        scanf("%d%d", &i, &j);
        e = (Edge *)malloc(sizeof(Edge));
        e->headvex = i;
        e->tailvex = j;
        e->headlink = v->ve[i].firstout;
        e->taillink = v->ve[j].firstin;
        v->ve[i].firstout = e;
        v->ve[j].firstin = e;
    }
}

void degree(int n, Vexedge v) {
    int sumin = 0, sumout = 0;
    Edge *e1, *e2;
    e2 = v.ve[n].firstout;
    e1 = v.ve[n].firstin;
    while (e1) {
        sumin++;
        e1 = e1->taillink;
    }
    while (e2) {
        sumout++;
        e2 = e2->headlink;
    }
    printf("出度为:%d 入度为:%d", sumout, sumin);
}

int main() {
    Vexedge v;
    int m;
    create(&v);
    printf("请输入你想要查询的顶点:");
    scanf("%d", &m);
    degree(m, v);
    getch();
    return 0;

}