pta数据结构-中国mooc邻接矩阵解法之一

本文介绍了如何使用C语言通过邻接矩阵解决PTA(Programming Task Assessment)平台上的数据结构问题。主要内容涉及内存分配(malloc)和队列的应用,详细阐述了邻接矩阵在数据结构问题中的实现思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#include<stdio.h>
#include<stdlib.h>
//vertex 顶点 edge 边

struct map{//图 栈 队列 边节点的定义
    int data[10][10];
};
struct stack{
    int Ver1,Ver2;
    struct stack*next;
};
struct line{
    int Ver1,Ver2;
    struct line*next;
};
struct edge{
    int Ver1,Ver2;
    int weight;
};
struct map* Creat_map(){//图  边 队列头结点 栈头插法头结点 的创建
    struct map*p;
    p=(struct map*)malloc(sizeof(struct map));
    return p;
}
struct edge*Creat_edge(){
    struct edge*p;
    p=(struct edge*)malloc(sizeof(struct edge));
    return p;
}
struct line *Creat_line(){
    struct line*p;
    p=(struct line*)malloc(sizeof(struct line));
    p->next=NULL;
    return p;
}
struct stack*Creat_stack(){
    struct stack*p;
    p=(struct stack*)malloc(sizeof(struct stack));
    p->next=NULL;
    return p;
}

void add(struct edge*p_edge){
    scanf("%d%d",&p_edge->Ver1,&p_edge->Ver2);
    p_edge->weight=1;
}
void insert_edge(struct edge*p_edge,struct map*p_map){//边入图 入栈 入队操作
    p_map->data[p_edge->Ver1][p_edge->Ver2]=p_edge->weight;
    p_map->data[p_edge->Ver2][p_edge->Ver1]=p_edge->weight;
}
void insert_stack(struct stack*head_stack,int hang,int lie){
    struct stack*p;
    p=(struct stack*)malloc(sizeof(struct stack));
    p->Ver1=hang;
    p->Ver2=lie;
    p->next=head_stack->next;
    head_stack->next=p;
}
struct line*inesert_line(struct line*rear,int hang,int lie){
    struct line*p;
    p=(struct line *)malloc(sizeof(struct line));
    p->Ver1=hang;
    p->Ver2=lie;
    p->next=NULL;
    rear->next=p;
    return p;
}

void del_stack(struct stack*head_stack){//出栈 出队操作
    struct stack*p;
    p=head_stack->next;
    head_stack->next=p->next;
    free(p);
}
void del_line(struct line*head_line){
    struct line*p;
    p=head_line->next;
    head_line->next=p->next;
    free(p);
}

void Clear(int N,struct map*p){
    int i,j;
    for(i=0;i<N;i++)
        for(j=0;j<N;j++){
            p->data[i][j]=0;
        }
}

void DFS(int N,struct map*p_map,struct stack*head_stack){//只差dfs和bfs 操作函数了
    int *data;
    int i,j;
    int lie;
    int count;
    data=(int *)malloc(N*sizeof(int));
    for(i=N-1;i>=0;i--)
        data[i]=0;
    i=0;
    while(i<N){
        if(!data[i]){
            printf("{ %d",i);
            data[i]=1;
            count=0;
            for(j=N-1;j>=0;j--){
                if(p_map->data[i][j]){
                    insert_stack(head_stack,i, j);
                    count++;
                }
            }
            while(count){
                lie=head_stack->next->Ver2;
                del_stack(head_stack);
                count--;
                if(!data[lie]){
                    printf(" %d",lie);
                    data[lie]=1;
                               }
                for(j=N-1;j>=0;j--){
                    if(p_map->data[lie][j]&&!data[j]){
                        insert_stack(head_stack, lie, j);
                        count++;
                    }
                }
            }
            printf(" }\n");
        }
        i++;
    }\
    free(data);
}

void BFS(struct line*front,struct line*rear,int N,struct map*p_map){
    int *data;
    data=(int *)malloc(N*sizeof(int ));
    int i=0,j,lie,count;
    for(j=0;j<N;j++)
        data[j]=0;
    while(i<N){
        if(!data[i]){
            printf("{ %d",i);
            data[i]=1;
            count=0;
            for(j=0;j<N;j++){
                if(p_map->data[i][j]){
                    rear=inesert_line(rear, i, j);
                    count++;
                }
            }
            while(count){
                lie=front->next->Ver2;
                del_line(front);
                count--;
                if(!data[lie]){
                    printf(" %d",lie);
                    data[lie]=1;
                }
                for(j=0;j<N;j++){
                    if(p_map->data[lie][j]&&!data[j]){
                        rear=inesert_line(rear, lie, j);
                        count ++;
                    }
                }
            }
            printf(" }\n");
        }
        i++;
        rear=front;
    }
    free(data);
}

int main(){
    struct map*p_map;
    struct edge*p_edge;
    struct line*front,*rear;
    struct stack*head_stack;
    int M,E,i;
    scanf("%d%d",&M,&E);
    p_map=Creat_map();
    p_edge=Creat_edge();
    front=rear=Creat_line();
    head_stack=Creat_stack();
    Clear(M, p_map);
    for(i=0;i<E;i++){
        add(p_edge);
        insert_edge(p_edge, p_map);
    }
    DFS(M, p_map, head_stack);
    BFS(front, rear, M, p_map);
    free(p_edge);
    free(p_map);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值