Skip to content

Commit 07a1786

Browse files
committed
第六章Part1
1 parent df42e09 commit 07a1786

13 files changed

+592
-0
lines changed

5-2-3

13.2 KB
Binary file not shown.

5-2-3.cpp

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include<iostream>
2+
using namespace std;
3+
#define MAXSIZE 50
4+
#define DATATYPE int
5+
#define NOP -1
6+
//邻接表表示的图 无向图arcnum等于2×边数
7+
typedef struct ArcNode{
8+
int value;
9+
int adjvex;
10+
struct ArcNode *next;
11+
}ArcNode,*Arc;
12+
typedef struct VNode{
13+
ArcNode *next;
14+
DATATYPE data;
15+
}VNode,AdjList[MAXSIZE];
16+
typedef struct{
17+
AdjList vertices;
18+
int vexnum,arcnum;
19+
}Graph;
20+
void AddArc(Graph &G,int x,int y,int value){
21+
Arc p,pre=NULL,s;
22+
for(p=G.vertices[x].next;p;pre=p,p=p->next){
23+
if(p->adjvex==y)return;
24+
}
25+
s=(Arc)malloc(sizeof(ArcNode));
26+
s->adjvex=y;
27+
s->value=value;
28+
s->next=NULL;
29+
if(pre)pre->next=s;
30+
else{
31+
G.vertices[x].next=s;
32+
}
33+
}
34+
void InitGraph(Graph &g){
35+
int vn,an,x=0,num1,num2,value;
36+
cin>>vn;
37+
cin>>an;
38+
g.vexnum=vn;
39+
g.arcnum=an;
40+
for(int i=0;i<g.vexnum;i++){
41+
g.vertices[i].next=NULL;
42+
}
43+
for(x=0;x<an;x++){
44+
cin>>num1;
45+
cin>>num2;
46+
cin>>value;
47+
AddArc(g,num1,num2,value);
48+
}
49+
}
50+
void PrintAdjList(Graph g){
51+
Arc p,tmp;
52+
for(int i=0;i<g.vexnum;i++){
53+
p=g.vertices[i].next;
54+
while(p){
55+
cout<<i<<"-"<<p->adjvex<<":"<<p->value<<" ";
56+
tmp=p->next;
57+
free(p);
58+
p=tmp;
59+
}
60+
cout<<endl;
61+
}
62+
}
63+
void InitEdge(Graph &g,int Edge[][MAXSIZE]){
64+
Arc p;
65+
for(int i=0;i<g.vexnum;i++){
66+
for(int j=0;j<g.vexnum;j++)
67+
if(i==j)Edge[i][j]=0;
68+
else Edge[i][j]=NOP;
69+
}
70+
for(int i=0;i<g.vexnum;i++){
71+
for(p=g.vertices[i].next;p;p=p->next)Edge[i][p->adjvex]=p->value;
72+
}
73+
}
74+
void PrintEdge(Graph &g,int Edge[][MAXSIZE]){
75+
for(int i=0;i<g.vexnum;i++){
76+
for(int j=0;j<g.vexnum;j++)cout<<Edge[i][j]<<" ";
77+
cout<<endl;
78+
}
79+
}
80+
int main(){
81+
Graph G;
82+
InitGraph(G);
83+
//PrintAdjList(G);
84+
int Edge[MAXSIZE][MAXSIZE];
85+
InitEdge(G,Edge);
86+
PrintEdge(G,Edge);
87+
PrintAdjList(G);
88+
return 0;
89+
}

5-3-2

27.8 KB
Binary file not shown.

5-3-2.cpp

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include<iostream>
2+
#include<queue>
3+
using namespace std;
4+
#define MAXSIZE 50
5+
#define DATATYPE int
6+
//邻接表表示的图 无向图arcnum等于2×边数
7+
typedef struct ArcNode{
8+
int value;
9+
int adjvex;
10+
struct ArcNode *next;
11+
}ArcNode,*Arc;
12+
typedef struct VNode{
13+
ArcNode *next;
14+
DATATYPE data;
15+
}VNode,AdjList[MAXSIZE];
16+
typedef struct{
17+
AdjList vertices;
18+
int vexnum,arcnum;
19+
}Graph;
20+
void AddArc(Graph &G,int x,int y,int value){
21+
Arc p,pre=NULL,s;
22+
for(p=G.vertices[x].next;p;pre=p,p=p->next){
23+
if(p->adjvex==y)return;
24+
}
25+
s=(Arc)malloc(sizeof(ArcNode));
26+
s->adjvex=y;
27+
s->value=value;
28+
s->next=NULL;
29+
if(pre)pre->next=s;
30+
else{
31+
G.vertices[x].next=s;
32+
}
33+
}
34+
void InitGraph(Graph &g){
35+
int vn,an,x=0,num1,num2,value;
36+
cin>>vn;
37+
cin>>an;
38+
g.vexnum=vn;
39+
g.arcnum=an;
40+
for(int i=0;i<g.vexnum;i++){
41+
g.vertices[i].next=NULL;
42+
}
43+
for(x=0;x<an;x++){
44+
cin>>num1;
45+
cin>>num2;
46+
cin>>value;
47+
AddArc(g,num1,num2,value);
48+
}
49+
}
50+
void PrintAdjList(Graph g){
51+
Arc p,tmp;
52+
for(int i=0;i<g.vexnum;i++){
53+
p=g.vertices[i].next;
54+
while(p){
55+
cout<<i<<"-"<<p->adjvex<<":"<<p->value<<" ";
56+
tmp=p->next;
57+
free(p);
58+
p=tmp;
59+
}
60+
cout<<endl;
61+
}
62+
}
63+
int FirstNeighbor(Graph &G,int v){
64+
if(G.vertices[v].next)return G.vertices[v].next->adjvex;
65+
else return -1;
66+
}
67+
int NextNeighbor(Graph &G,int v,int w){
68+
Arc p;
69+
for(p=G.vertices[v].next;p;p=p->next){
70+
if(p->adjvex==w){
71+
if(p->next){
72+
return p->next->adjvex;
73+
}else return -1;
74+
}
75+
}
76+
return -1;
77+
}
78+
bool visted[MAXSIZE];
79+
void BFS(Graph &G,int v){
80+
queue<int> q;
81+
cout<<v<<" ";
82+
visted[v]=true;
83+
int p,w;
84+
q.push(v);
85+
while(!q.empty()){
86+
w=q.back();
87+
q.pop();
88+
for(p=FirstNeighbor(G,w);p!=-1;p=NextNeighbor(G,w,p)){
89+
if(!visted[p]){
90+
cout<<p<<" ";
91+
visted[p]=true;
92+
q.push(p);
93+
}
94+
}
95+
}
96+
}
97+
int count=0;
98+
void DFS(Graph &G,int v){
99+
int p;
100+
cout<<v<<" ";
101+
visted[v]=true;
102+
for(p=FirstNeighbor(G,v);p!=-1;p=NextNeighbor(G,v,p)){
103+
count++;
104+
if(!visted[p]){
105+
DFS(G,p);
106+
}
107+
}
108+
}
109+
int main(){
110+
for(int i=0;i<MAXSIZE;i++)visted[i]=false;
111+
Graph G;
112+
InitGraph(G);
113+
DFS(G,0);
114+
cout<<endl;
115+
for(int i=0;i<G.vexnum;i++)if(!visted[i]){
116+
cout<<"false"<<endl;
117+
PrintAdjList(G);
118+
return 1;
119+
}
120+
if((count/2)!=G.vexnum-1){
121+
cout<<"false"<<endl;
122+
PrintAdjList(G);
123+
return 1;
124+
}
125+
cout<<"true";
126+
for(int i=0;i<MAXSIZE;i++)visted[i]=false;
127+
//BFS(G,0);
128+
cout<<endl;
129+
PrintAdjList(G);
130+
return 0;
131+
}

5-3-3

27.8 KB
Binary file not shown.

5-3-3.cpp

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include<iostream>
2+
#include<stack>
3+
using namespace std;
4+
#define MAXSIZE 50
5+
#define DATATYPE int
6+
//邻接表表示的图 无向图arcnum等于2×边数
7+
typedef struct ArcNode{
8+
int value;
9+
int adjvex;
10+
struct ArcNode *next;
11+
}ArcNode,*Arc;
12+
typedef struct VNode{
13+
ArcNode *next;
14+
DATATYPE data;
15+
}VNode,AdjList[MAXSIZE];
16+
typedef struct{
17+
AdjList vertices;
18+
int vexnum,arcnum;
19+
}Graph;
20+
void AddArc(Graph &G,int x,int y,int value){
21+
Arc p,pre=NULL,s;
22+
for(p=G.vertices[x].next;p;pre=p,p=p->next){
23+
if(p->adjvex==y)return;
24+
}
25+
s=(Arc)malloc(sizeof(ArcNode));
26+
s->adjvex=y;
27+
s->value=value;
28+
s->next=NULL;
29+
if(pre)pre->next=s;
30+
else{
31+
G.vertices[x].next=s;
32+
}
33+
}
34+
void InitGraph(Graph &g){
35+
int vn,an,x=0,num1,num2,value;
36+
cin>>vn;
37+
cin>>an;
38+
g.vexnum=vn;
39+
g.arcnum=an;
40+
for(int i=0;i<g.vexnum;i++){
41+
g.vertices[i].next=NULL;
42+
}
43+
for(x=0;x<an;x++){
44+
cin>>num1;
45+
cin>>num2;
46+
cin>>value;
47+
AddArc(g,num1,num2,value);
48+
}
49+
}
50+
void PrintAdjList(Graph g){
51+
Arc p,tmp;
52+
for(int i=0;i<g.vexnum;i++){
53+
p=g.vertices[i].next;
54+
while(p){
55+
cout<<i<<"-"<<p->adjvex<<":"<<p->value<<" ";
56+
tmp=p->next;
57+
free(p);
58+
p=tmp;
59+
}
60+
cout<<endl;
61+
}
62+
}
63+
int FirstNeighbor(Graph &G,int v){
64+
if(G.vertices[v].next)return G.vertices[v].next->adjvex;
65+
else return -1;
66+
}
67+
int NextNeighbor(Graph &G,int v,int w){
68+
Arc p;
69+
for(p=G.vertices[v].next;p;p=p->next){
70+
if(p->adjvex==w){
71+
if(p->next){
72+
return p->next->adjvex;
73+
}else return -1;
74+
}
75+
}
76+
return -1;
77+
}
78+
bool visted[MAXSIZE];
79+
void DFS(Graph &G,int v){
80+
int p;
81+
cout<<v<<" ";
82+
visted[v]=true;
83+
for(p=FirstNeighbor(G,v);p!=-1;p=NextNeighbor(G,v,p)){
84+
if(!visted[p])
85+
DFS(G,p);
86+
}
87+
}
88+
void fun(Graph &G,int v){
89+
stack<int> s;
90+
int p,w;
91+
s.push(v);
92+
visted[v]=true;
93+
while(!s.empty()){
94+
w=s.top();
95+
s.pop();
96+
cout<<w<<" ";
97+
for(p=FirstNeighbor(G,w);p!=-1;p=NextNeighbor(G,w,p)){
98+
if(!visted[p]){
99+
s.push(p);//在此令visted为true 避免重复入栈
100+
visted[p]=true;
101+
}
102+
}
103+
}
104+
}
105+
int main(){
106+
for(int i=0;i<MAXSIZE;i++)visted[i]=false;
107+
Graph G;
108+
InitGraph(G);
109+
DFS(G,0);
110+
cout<<endl;
111+
for(int i=0;i<G.vexnum;i++)visted[i]=false;
112+
fun(G,0);
113+
cout<<endl;
114+
PrintAdjList(G);
115+
return 0;
116+
}

5-3-4

27.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)