2020-11-26

数据结构实验

#pragma once
#define INF 32767
#define MAXV 100
typedef struct ANode{
int adjvex;
struct ANode* nextarc;
int weight;
}ArcNode;
typedef struct Vnode {
char info;
ArcNode* firstarc;
}VNode;
typedef struct {
VNode adjlist[MAXV];
int n, e;
}AdjGraph;
void DFS(AdjGraph* G, int v);
void BFS(AdjGraph* G, int v);


#pragma once
typedef int Elemtype;
#define MaxSize 100
typedef struct
{
Elemtype data[MaxSize];
int front, rear;
}SqQueue;
bool deQueue(SqQueue*& q, Elemtype &e);
bool enQueue(SqQueue* q, Elemtype e);
bool QueueEmpty(SqQueue* q);
void DestroyQueue(SqQueue*& q);
void InitQueue(SqQueue*& q);


#include <cstdio>
#include "head.h"
#include "head2.h"
int visited[MAXV] = { 0 };
void DFS(AdjGraph* G, int v)       //深度优先搜索
{

ArcNode* p;
visited[v] = 1;
printf("%d ", v+1);
p = G->adjlist[v].firstarc;
while (p != NULL)
{
if (visited[p->adjvex] == 0)
DFS(G, p->adjvex);
p = p->nextarc;
}
}

void BFS(AdjGraph* G, int v)      //广度优先搜索
{
int w=1 , i;
ArcNode* p;
SqQueue* qu;
InitQueue(qu);
int visited1[MAXV];
for (i = 0; i < G->n; i++)
visited1[i] = 0;
printf("%d ", v+1);
visited1[v] = 1;
enQueue(qu, v);
while (!QueueEmpty(qu))
{
deQueue(qu, w);
p = G->adjlist[w].firstarc;
while (p != NULL)
{
if (visited1[p->adjvex] == 0)
{
printf("%d ", p->adjvex+1);
visited1[p->adjvex] = 1;
enQueue(qu, p->adjvex);
}
p = p->nextarc;
}
}
printf("\n");
}


#include <stdio.h>
#include <cstdlib>
#include "head.h"
#include "head2.h"
//int visited[MAXV][MAXV] = { 0 };
void CreateAdj(AdjGraph*& G, int A[MAXV][MAXV], int n, int e)     //由图的邻接链表创建图的邻接表
{
int i, j;
ArcNode* p;
G = (AdjGraph*)malloc(sizeof(AdjGraph));
for (i = 0; i < n; i++)
G->adjlist[i].firstarc = NULL;
for (i = 0; i < n; i++)
{
for (j = n - 1; j >= 0; j--)
if (A[i][j] != 0 && A[i][j] != INF)
{
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = j;
p->weight = A[i][j];
p->nextarc = G->adjlist[i].firstarc;
G->adjlist[i].firstarc = p;
}
}
G->n = n;
G->e = e;
}
void DispAdj(AdjGraph* G)    //输出邻接表
{
int i; ArcNode* p;
for (i = 0; i < G->n; i++)
{
p = G->adjlist[i].firstarc;
printf("%3d:", i);
while (p != NULL)
{
printf("%3d[%d]->", p->adjvex, p->weight);
p = p->nextarc;
}
printf("\n");
}
}
int main()
{
int s[MAXV][MAXV];
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++)
scanf("%d",&s[i][j]);
AdjGraph* p;
CreateAdj(p,s,12,14);
printf("建立图的邻接如下\n");
DispAdj(p);
printf("进行图的深度优先搜索结果如下：\n");
DFS(p,0);
printf("\n进行图的广度优先搜索结果如下：\n");
BFS(p,0);
/*for(int i=0;i<6;i++)
printf("\n%d", p->adjlist[5+i].firstarc->adjvex);*/
return 0;
}


queue.cpp

//环形队列算法
#include <stdlib.h>
#include "head2.h"
void InitQueue(SqQueue*& q)
{
q = (SqQueue*)malloc(sizeof(SqQueue));
q->front = q->rear = 0;
}
void DestroyQueue(SqQueue*& q)
{
free(q);
}
bool QueueEmpty(SqQueue* q)
{
return(q->front == q->rear);
}
bool enQueue(SqQueue* q, Elemtype e)
{
if ((q->rear + 1) % MaxSize == q->front)
return false;
q->rear = (q->rear + 1) % MaxSize;
q->data[q->rear] = e;
return true;
}

bool deQueue(SqQueue*& q, Elemtype &e)
{
if (q->front == q->rear)
return false;
q->front = (q->front + 1) % MaxSize;
e = q->data[q->front];
return true;
}
`
• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

weixin_45769783

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

05-02 283

01-11 1139
04-27 142
06-11 1037
01-26 2706
03-14 301
06-07 342
06-23 538
07-10 227
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客