首页 > 其他分享 >【锐格】数据结构-实验-图

【锐格】数据结构-实验-图

时间:2023-06-07 14:06:47浏览次数:32  
标签:typedef struct int Graph 锐格 NUM 实验 VerNode 数据结构


7039

#include<iostream>
#include<cstdio>

using namespace std;

const int MAX_NUM=100;

int w;
int mark[MAX_NUM];

typedef int EdgeData;

typedef struct Node
{
    int dest;
    EdgeData weight;//边权
    struct Node *next;//next route
}EdgeNode;

typedef struct
{
    char data;
    EdgeNode *point;
}VerNode;

typedef struct
{
    VerNode VList[MAX_NUM];
    int n,e;//图中顶点个数与边数
}Graph;

void createGraph(Graph &G)
{
    int tail, head;
    cin>>G.n>>G.e;
    for(int i=1;i<=G.n;i++)
    {
        cin>>G.VList[i].data;
        G.VList[i].point=NULL;
    }
    for(int i=1;i<=G.e;i++)
    {
        cin>>tail>>head;
        EdgeNode *p=new EdgeNode ;
        p->dest=head;
        p->next=G.VList[tail].point;
        G.VList[tail].point=p;
    }
}

void showGraph(Graph G)
{
    for(int i=1;i<=G.n;i++)
    {
        cout<<G.VList[i].data<<":";
        EdgeNode *p=G.VList[i].point;
        while(p!=NULL)
        {
            cout<<p->dest<<" ";
            p=p->next;
        }
        cout<<endl;
    }
}

void DFS(Graph G,int v)
{
    cout<<v<<" ";
    mark[v]=1;
    EdgeNode *p=G.VList[v].point;
    while(p!=NULL)
    {
        w=p->dest;
        if(!mark[w]) DFS(G, w);
        p=p->next;
    }
}

int main()
{
    Graph G;
    createGraph(G);
    showGraph(G);
    DFS(G, 1);
    return 0;
}

7040

#include<iostream>
#include<cstring>
#include<queue>

using namespace std;

const int MAX_NUM=110;

int in[MAX_NUM],out[MAX_NUM];

typedef struct VerNode{
    int adjvex;
    struct VerNode * nextarc;
}VerNode;

typedef struct VNode{
    int data;
    VerNode *firstarc;
}VNode,AdjList[MAX_NUM];

typedef struct{
    AdjList verlist;
    int vexnum,arcnum;
}Graph;

void createGraph(Graph &G){
    cin>>G.vexnum>>G.arcnum;
    for(int i=1;i<=G.vexnum;i++){
        cin>>G.verlist[i].data;
        G.verlist[i].firstarc=NULL;
    }
    for(int i=1;i<=G.arcnum;i++){
        int x,y;cin>>x>>y;

        VerNode * p1= new VerNode;

        p1->adjvex=y;
        p1->nextarc=G.verlist[x].firstarc;
        G.verlist[x].firstarc=p1;
    }
}

void GetVec(Graph G){
    for(int i=1;i<=G.vexnum;i++){
        VerNode* p=G.verlist[i].firstarc;
        while(p!=NULL){
            out[i]++;
            in[p->adjvex]++;
            p=p->nextarc;
        }
    }
}


int main(){
    Graph G;
    createGraph(G);
    GetVec(G);
    for(int i=1;i<=G.vexnum;i++){
        cout<<i<<":"<<in[i]<<" "<<out[i]<<" "<<in[i]+out[i]<<endl;
    }
    return 0;
}


标签:typedef,struct,int,Graph,锐格,NUM,实验,VerNode,数据结构
From: https://blog.51cto.com/u_15567308/6431256

相关文章

  • 【NEFU】数据结构阶段二机试代码
    个人拙见,难免有不足之处,望大佬们斧正P1#defineMAXNODE64#include<stdio.h>#include<stdlib.h>typedefstructNode//边的信息{intadjvex;structNode*next;}ArcNode;typedefstructVNode//顶点的信息{intdata;ArcNode*firstarc;}Ve......
  • 【数据结构】图的基本操作
    #include<iostream>#include<cstdio>#include<stack>#include<queue>#include<cstring>constintMAX_SUM=110;usingnamespacestd;typedefstructArcNode{intadj;structArcNode*nextarc;}ArcNode;typedefstruct......
  • 实验六 turtle绘图与python库应用编程体验
    '''task1_1.py'''fromturtleimport*defmove(x,y):penup()goto(x,y)pendown()defdraw(n,size=100):foriinrange(n):fd(size)left(360/n)defmain():pensize(2)pencolor('red&#......
  • 实验6
    实验任务1task1源代码:fromturtleimport*defmove(x,y):penup()goto(x,y)pendown()defdraw(n,size=100):foriinrange(n):fd(size)left(360/n)defmain():pensize(2)pencolor('red')move(-200,0)......
  • 实验五 文件应用编程
    task6withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()data.remove('原始数据\n')data0=[]foriindata:ifi[-1]=='\n':i=i[:-1]data0.append(float(i))print('原始......
  • 实验7 面向对象编程与内置模块
    task1classAccount:def__init__(self,name,account_number,initial_amount=10):self._name=nameself._card_no=account_numberself._balance=initial_amountdefdeposit(self,account):self._balance+=accountdefwit......
  • 实验5
    task6源代码:importcsvtitle=['原始数据','四舍五入后的数据']lst=[]yuan_lst=[]hou_lst=[]withopen('data6.csv','r')asf:read_lst=f.readlines()foriinrange(1,len(read_lst)):y=float(read_l......
  • 实验7 面向对象编程与内置模块
    实验任务1task1.py1classAccount:23def__init__(self,name,account_number,initial_amount=10):4self._name=name5self._card_no=account_number6self._balance=initial_amount7defdeposit(self,amount):......
  • 实验五 文件应用编程
    task6实验源码1importcsv23title=['原始数据','四舍五入后的数据']45lst=[]6yuan_lst=[]7hou_lst=[]8withopen('data6.csv','r')asf:9read_lst=f.readlines()10foriinrange(1,len(read_l......
  • 实验5
    task61importcsv2title=[]3info=[]45withopen('data6.csv','r',encoding='gbk')asf:6f_reader=csv.reader(f)7forlineinf_reader:8info.append(line)9x=info.pop(0)10title.appe......