您好我正在进行深度优先搜索的项目,程序使用每个相邻顶点的链表并使用堆栈。该程序有两个结构Vertex Struct,它有两个字段,一个是链表,指针和一个名为visted的布尔变量标记为false。第二个结构是一个Graph,它有两个字段,一个Vertex Struct数组和一个顶点数的整数。我遇到的问题是,当调用深度优先搜索功能时,即使堆栈为空,它也永远不会停止,深度优先功能会打印每个被访问的顶点。有人可以帮我找到一种方法让程序停止堆栈是空的;
#include <iostream>
#include "linklist.h"
#include "Stack.h"
using namespace std;
typedef int vertexNum;
Stack S;
struct Vertex
{
List p;
bool visted;
Vertex()
{
p = NULL;
visted = false;
}
};
struct Graph
{
int numVert;
Vertex* vertArr;
Graph(int num)
{
numVert = num;
vertArr = new Vertex[numVert+1];
}
};
void readGraph(Graph& G)
{
cout << "Enter adjacent vertices, enter 0 to stop" << endl;
vertexNum v1, v2;
cin >> v1;
while (v1 != 0)
{
cin >> v2;
insertTail(v2,G.vertArr[v1].p);
insertTail(v1,G.vertArr[v2].p);
//G.vertArr[v1].p = cons(v2,G.vertArr[v1].p);
//G.vertArr[v2].p = cons(v1,G.vertArr[v2].p);
cin >> v1;
}
}
void printGraph(Graph& G)
{
for (int i = 1; i <= G.numVert; i++)
{
cout << "Vertex "<< i << " Adjacent list ";
printList(G.vertArr[i].p);
}
}
void DFS(int x,Graph& G)
{
push(x,S);
printList(S.p);
G.vertArr[x].visted = true;
if (!G.vertArr[head(G.vertArr[x].p)].visted)
{
DFS(head(G.vertArr[x].p),G);
}
else
{
cout << ""<< endl;
List temp = tail(G.vertArr[x].p);
if(isEmpty(temp))
{
pop(S);
G.vertArr[peek(S)].visted = false;
DFS(pop(S),G);
}
else if(!G.vertArr[head(temp)].visted)
{
DFS(head(temp),G);
}
else
{
int num =0;
for (List pp = temp; !isEmpty(pp); pp = tail(pp))
{
if(!G.vertArr[head(pp)].visted)
{
num = head(pp);
break;
}
}
if (num == 0)
{
pop(S);
G.vertArr[peek(S)].visted = false;
DFS(pop(S),G);
}
else
DFS(num,G);
}
}
}
int main()
{
int x,y;
cout << "Enter the number of vertices" << endl;
cin >> x;
Graph p(x);
readGraph(p);
printGraph(p);
cout << "\nEnter start vertex" << endl;
cin >> y;
DFS(y,p);
}
程序输出
Enter the number of vertices
13
Enter adjacent vertices, enter 0 to stop
1 4
1 5
1 2
1 3
2 7
2 8
8 13
8 7
11 12
10 9
13 12
9 6
6 5
0
Vertex 1 Adjacent list 4 5 2 3
Vertex 2 Adjacent list 1 7 8
Vertex 3 Adjacent list 1
Vertex 4 Adjacent list 1
Vertex 5 Adjacent list 1 6
Vertex 6 Adjacent list 9 5
Vertex 7 Adjacent list 2 8
Vertex 8 Adjacent list 2 13 7
Vertex 9 Adjacent list 10 6
Vertex 10 Adjacent list 9
Vertex 11 Adjacent list 12
Vertex 12 Adjacent list 11 13
Vertex 13 Adjacent list 8 12
Enter start vertex
5
5
1 5
4 1 5
1 5
2 1 5
7 2 1 5
8 7 2 1 5
13 8 7 2 1 5
12 13 8 7 2 1 5
11 12 13 8 7 2 1 5
12 13 8 7 2 1 5
13 8 7 2 1 5
8 7 2 1 5
7 2 1 5
2 1 5
1 5
3 1 5
1 5
5
6 5
9 6 5
10 9 6 5
9 6 5
6 5
5
这是堆栈和链接列表h文件
#ifndef LINKLIST_H_
#define LINKLIST_H_
#include <cstdlib>
#include <iostream>
using namespace std;
struct listCell
{
int head;
listCell* tail;
listCell(int h, listCell* t)
{
head =h;
tail = t;
}
};
typedef listCell* List;
typedef const listCell* constList;
List emptyList = NULL;
int head(List L)
{
return L->head;
}
List tail(List L)
{
return L->tail;
}
bool isEmpty(List L)
{
return L == emptyList;
}
List cons(int h, List t)
{
return new listCell(h,t);
}
int listLength(List L)
{
if (isEmpty(L))
{
return 0;
}
else
{
return 1 + listLength(tail(L));
}
}
void printList(List L)
{
List k = L;
while (!isEmpty(k))
{
int val = head(k);
k = k->tail;
cout << val << " ";
}
cout << "\n";
}
这是堆栈文件
#ifndef STACK_H_
#define STACK_H_
#include "linklist.h"
struct Stack
{
List p;
Stack()
{
p = NULL;
}
};
void push(int x, Stack& list)
{
list.p =cons(x,list.p);
}
int peek(Stack& list)
{
return head(list.p);
}
int pop(Stack& list)
{
return delHead(list.p);
}
bool isEmpty(Stack& list)
{
return isEmpty(list.p);
}
int size(Stack& list)
{
return listLength(list.p);
}
#endif /* STACK_H_ */