111
This commit is contained in:
+192
@@ -0,0 +1,192 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define queuesize 10
|
||||
#define maxvernum 8
|
||||
|
||||
typedef struct queue {
|
||||
int elements[queuesize];
|
||||
int front;
|
||||
int rear;
|
||||
} queue;
|
||||
|
||||
// 入队函数
|
||||
int enQueue(queue* q, int value) {
|
||||
if ((q->rear + 1) % queuesize == q->front) {
|
||||
return FALSE;
|
||||
}
|
||||
q->elements[q->rear] = value;
|
||||
q->rear = (q->rear + 1) % queuesize;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// 出队函数
|
||||
int deQueue(queue* q, int* value) {
|
||||
if (q->front == q->rear) {
|
||||
return FALSE;
|
||||
}
|
||||
*value = q->elements[q->front];
|
||||
q->front = (q->front + 1) % queuesize;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// 初始化队列
|
||||
void initQueue(queue* q) {
|
||||
q->front = 0;
|
||||
q->rear = 0;
|
||||
}
|
||||
|
||||
// 单链表节点定义
|
||||
typedef struct Node {
|
||||
int data;
|
||||
struct Node* next;
|
||||
} Node;
|
||||
|
||||
// 顶点结点(邻接表头)
|
||||
typedef struct VexNode {
|
||||
int vexnum;
|
||||
Node* firstedge;
|
||||
} VexNode;
|
||||
|
||||
// 邻接表
|
||||
typedef struct AdjList {
|
||||
VexNode adjlist[maxvernum];
|
||||
int vexnum;
|
||||
int edgenum;
|
||||
} AdjList;
|
||||
|
||||
// 定位顶点值
|
||||
int locateVex(AdjList* G, int v) {
|
||||
for (int i = 0; i < G->vexnum; i++) {
|
||||
if (G->adjlist[i].vexnum == v) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 创建有向图
|
||||
void Creategraph(AdjList* g) {
|
||||
int i, k, v1, v2;
|
||||
printf("请输入顶点数和边数(空格分隔):", maxvernum);
|
||||
scanf_s("%d %d", &g->vexnum, &g->edgenum);
|
||||
|
||||
// 初始化顶点
|
||||
for (i = 0; i < g->vexnum; i++) {
|
||||
g->adjlist[i].vexnum = i + 1;
|
||||
g->adjlist[i].firstedge = NULL;
|
||||
}
|
||||
|
||||
printf("当前顶点为:");
|
||||
for (i = 0; i < g->vexnum; i++) {
|
||||
printf("v%d ", g->adjlist[i].vexnum);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// 依次输入每条有向边(弧头 v1 -> 弧尾 v2),用空格分隔
|
||||
for (k = 0; k < g->edgenum; k++) {
|
||||
printf("请输入第%d条有向边的弧头 和 弧尾(用空格分隔):", k + 1);
|
||||
scanf_s("%d %d", &v1, &v2);
|
||||
int i_idx = locateVex(g, v1);
|
||||
int j_idx = locateVex(g, v2);
|
||||
if (i_idx >= 0 && j_idx >= 0) {
|
||||
// 在 v1 的链表末尾加入 v2
|
||||
Node* bss = (Node*)malloc(sizeof(Node));
|
||||
if (!bss) {
|
||||
printf("内存分配失败");
|
||||
return;
|
||||
}
|
||||
bss->data = v2;
|
||||
bss->next = NULL;
|
||||
if (g->adjlist[i_idx].firstedge == NULL) {
|
||||
g->adjlist[i_idx].firstedge = bss;
|
||||
}
|
||||
else {
|
||||
Node* q = g->adjlist[i_idx].firstedge;
|
||||
while (q->next) q = q->next;
|
||||
q->next = bss;
|
||||
}
|
||||
}
|
||||
else {
|
||||
printf("顶点 %d 或 %d 不存在,边被忽略\n", v1, v2);
|
||||
}
|
||||
}
|
||||
|
||||
// 输出邻接表
|
||||
printf("输出各个顶点的邻接表:\n");
|
||||
for (i = 0; i < g->vexnum; i++) {
|
||||
printf("顶点 v%d", g->adjlist[i].vexnum);
|
||||
Node* p = g->adjlist[i].firstedge;
|
||||
while (p) {
|
||||
printf(" --> v%d", p->data);
|
||||
p = p->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// 深度优先遍历
|
||||
void DFS(AdjList* G, int v, int visited[]) {
|
||||
int i = locateVex(G, v);
|
||||
visited[i] = 1;
|
||||
printf("%d ", G->adjlist[i].vexnum);
|
||||
Node* p = G->adjlist[i].firstedge;
|
||||
while (p != NULL) {
|
||||
int j = locateVex(G, p->data);
|
||||
if (j != -1 && !visited[j]) {
|
||||
DFS(G, p->data, visited);
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
|
||||
// 广度优先遍历
|
||||
void BFS(AdjList* G, int v, int visited[]) {
|
||||
queue q;
|
||||
initQueue(&q);
|
||||
int i = locateVex(G, v);
|
||||
if (i == -1) return;
|
||||
visited[i] = 1;
|
||||
printf("%d ", G->adjlist[i].vexnum);
|
||||
enQueue(&q, v);
|
||||
int w;
|
||||
while (deQueue(&q, &w)) {
|
||||
int k = locateVex(G, w);
|
||||
if (k == -1) continue;
|
||||
Node* p = G->adjlist[k].firstedge;
|
||||
while (p != NULL) {
|
||||
int j = locateVex(G, p->data);
|
||||
if (j != -1 && !visited[j]) {
|
||||
visited[j] = 1;
|
||||
printf("%d ", G->adjlist[j].vexnum);
|
||||
enQueue(&q, p->data);
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
AdjList G;
|
||||
Creategraph(&G);
|
||||
int visited[maxvernum] = { 0 };
|
||||
printf("深度优先遍历结果:");
|
||||
for (int i = 0; i < G.vexnum; i++) {
|
||||
if (!visited[i]) {
|
||||
DFS(&G, G.adjlist[i].vexnum, visited);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
for (int i = 0; i < maxvernum; i++) visited[i] = 0;
|
||||
printf("广度优先遍历结果:");
|
||||
for (int i = 0; i < G.vexnum; i++) {
|
||||
if (!visited[i]) {
|
||||
BFS(&G, G.adjlist[i].vexnum, visited);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.14.36623.8 d17.14
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lab", "lab.vcxproj", "{5D999FD0-2593-4479-ADDA-A7FAAEFDF13E}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{5D999FD0-2593-4479-ADDA-A7FAAEFDF13E}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{5D999FD0-2593-4479-ADDA-A7FAAEFDF13E}.Debug|x64.Build.0 = Debug|x64
|
||||
{5D999FD0-2593-4479-ADDA-A7FAAEFDF13E}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{5D999FD0-2593-4479-ADDA-A7FAAEFDF13E}.Debug|x86.Build.0 = Debug|Win32
|
||||
{5D999FD0-2593-4479-ADDA-A7FAAEFDF13E}.Release|x64.ActiveCfg = Release|x64
|
||||
{5D999FD0-2593-4479-ADDA-A7FAAEFDF13E}.Release|x64.Build.0 = Release|x64
|
||||
{5D999FD0-2593-4479-ADDA-A7FAAEFDF13E}.Release|x86.ActiveCfg = Release|Win32
|
||||
{5D999FD0-2593-4479-ADDA-A7FAAEFDF13E}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {AD21B75E-63D4-4614-872B-9EBA69D211F0}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{5d999fd0-2593-4479-adda-a7faaefdf13e}</ProjectGuid>
|
||||
<RootNamespace>lab</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="lab.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="源文件">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="资源文件">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="lab.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user