邻接表的创建与深搜

#include<stdio.h>
#include<stdlib.h>
#define MAX 20  //最多顶点个数
typedef struct Arc
{
 int adjvex;
 Arc *next;
 int other;
}Arc;
int visited[MAX];
typedef struct Node
{
 char date;
 Arc *firstarc;
}Node;

typedef struct Adj
{
 Node vertex[MAX];
 int vexnum,arcnum;
}Adj;
int locat(Adj *G,char v)
{
 int j=0,k;
 for(k=1;k<=G->vexnum;k++)
 {
  if(v==G->vertex[k].date)
  {
   j=k;
   break;
  }
 }
 return j;
}

void creat(Adj *G)
{
 Arc *p;
 int i,j,k;
 char v1,v2;
 printf("请输入顶点数和边数:");
 scanf("%d%d",&G->vexnum,&G->arcnum);
 for(i=1;i<=G->vexnum;i++)
 {
  printf("请输入图中的顶点");
  getchar();
  G->vertex[i].date=getchar();
  G->vertex[i].firstarc=NULL;
 }
 for(k=1;k<=G->arcnum;k++)
 {
  getchar();
  printf("请输入两个顶点:");
  scanf("%c,%c,%d",&v1,&v2);
        i=locat(G,v1);
  j=locat(G,v2);
        p=(Arc *)malloc(sizeof(Arc));
  p->adjvex=j;
  p->next=G->vertex[i].firstarc;
  G->vertex[i].firstarc=p;
  p=(Arc *)malloc(sizeof(Arc));// 如果输出有向图此段程序不要
  p->adjvex=i;
  p->next=G->vertex[j].firstarc;
     G->vertex[j].firstarc=p;//不要的程序到此
 }
}

void DepthFirstSearch(Adj *G,int v0)
{
 Arc *p;
 printf("%c,",G->vertex[v0].date);
 visited[v0]=1;
 p=G->vertex[v0].firstarc;
 while(p!=NULL)
 {
  if(!visited[p->adjvex])
  {
   DepthFirstSearch(G,p->adjvex);
  }
  p=p->next;
 }
}

void Trave(Adj *G)
{
 int vi;
 for(vi=1;vi<=G->vexnum;vi++)
  visited[vi]=0;
 for(vi=1;vi<=G->vexnum;vi++)
 {
  if(!visited[vi])
   DepthFirstSearch(G,vi);
 }
}

int main()
{
 Adj *G=(Adj *)malloc(sizeof(Adj));
 creat(G);
 Trave(G);
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邻接表是一种图的存储结构,它可以用于实现图的深度优先搜索(DFS)和广度优先搜索(BFS)。下面是邻接表深搜和广搜的C语言实现: 1. 邻接表的深度优先搜索(DFS)C语言实现: ```c #define MAXNODE 1000 #define MAXSIDE 10000 typedef struct SIDE { int tailNode; int nextSide; int weight; } SIDE; SIDE edge[MAXSIDE]; int head[MAXNODE]; int visited[MAXNODE]; int counttEdge = 0; void CreateABoundary(int headNode, int tailNode, int weight) { edge[counttEdge].tailNode = tailNode; edge[counttEdge].weight = weight; edge[counttEdge].nextSide = head[headNode]; head[headNode] = counttEdge; counttEdge++; } void DFS(int node) { visited[node] = 1; printf("%d ", node); for (int i = head[node]; i != -1; i = edge[i].nextSide) { int nextNode = edge[i].tailNode; if (!visited[nextNode]) { DFS(nextNode); } } } int main() { // 初始化 memset(head, -1, sizeof(head)); memset(visited, 0, sizeof(visited)); counttEdge = 0; // 创建图 CreateABoundary(1, 2, 0); CreateABoundary(1, 3, 0); CreateABoundary(2, 4, 0); CreateABoundary(2, 5, 0); CreateABoundary(3, 6, 0); CreateABoundary(3, 7, 0); // DFS遍历 DFS(1); return 0; } ``` 2. 邻接表的广度优先搜索(BFS)C语言实现: ```c #define MAXNODE 1000 #define MAXSIDE 10000 typedef struct SIDE { int tailNode; int nextSide; int weight; } SIDE; SIDE edge[MAXSIDE]; int head[MAXNODE]; int visited[MAXNODE];int counttEdge = 0; void CreateABoundary(int headNode, int tailNode, int weight) { edge[counttEdge].tailNode = tailNode; edge[counttEdge].weight = weight; edge[counttEdge].nextSide = head[headNode]; head[headNode] = counttEdge; counttEdge++; } void BFS(int node) { int queue[MAXNODE]; int front = 0, rear = 0; visited[node] = 1; printf("%d ", node); queue[rear++] = node; while (front < rear) { int curNode = queue[front++]; for (int i = head[curNode]; i != -1; i = edge[i].nextSide) { int nextNode = edge[i].tailNode; if (!visited[nextNode]) { visited[nextNode] = 1; printf("%d ", nextNode); queue[rear++] = nextNode; } } } } int main() { // 初始化 memset(head, -1, sizeof(head)); memset(visited, 0, sizeof(visited)); counttEdge = 0; // 创建图 CreateABoundary(1, 2, 0); CreateABoundary(1, 3, 0); CreateABoundary(2, 4, 0); CreateABoundary(2, 5, 0); CreateABoundary(3, 6, 0); CreateABoundary(3, 7, 0); // BFS遍历 BFS(1); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值