临接表

来源:互联网 发布:大脚插件 for mac 编辑:程序博客网 时间:2024/05/17 06:16
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 1010;int n = 0, m = 0;typedef struct node {    int to;    node* next;}node;node* tab[maxn];node* newnode() {    node* tmp = (node*)malloc(sizeof(node));    tmp->next = NULL;    return tmp;}int init(){    for (int i = 0; i < maxn; i++) {        tab[i] = NULL;    }    node* now = NULL;    int a = 0, b = 0;    for (int i = 0; i < m; i++) {        scanf("%d%d", &a, &b);        now = newnode();        now->to = b;        now->next = tab[a];        tab[a] = now;    }    return 0;}int work() {    node *now = NULL;    for (int i = 1; i <= n; i++) {        now = tab[i];        printf("from = %d:", i);        while (now) {            printf(" %d", now->to);            now = now->next;        }        printf("\n");    }    return 0;}int main(){    struct stu{        int a;        //int b;        long long c;        //double d;        //long long c;    };    //printf("%d %d\n", sizeof(float), sizeof(long long));    printf("%d\n", sizeof(stu));    while (2 == scanf("%d%d", &n, &m)) {        init();        work();    }    return 0;}

原创粉丝点击