【www.guakaob.com--结构师考试】
6.0让我看看你的步法篇一
《数据结构算法VC++6.0程序集-我敲的几个程序,很多无法一次上传!先上传一部分看看效果》
const int MaxV = 10;
struct edgenode{
int adjvex;
int weight;
edgenode *next;
edgenode() {}
edgenode(int d,int w):adjvex(d),weight(w) { next = NULL; } };
struct top{
char data;
edgenode *adj;
};
struct RCW{
int row;
int col;
int weight;
};
class AdjAdjion{
private:
top g[MaxV];
int size;
int numE;
public:
edgenode **GL;
AdjAdjion() {}
AdjAdjion(edgenode **gl,int n);
bool GraphEmpty() { return size==0; }
int numV() { return size; }
int NumEdges() { return numE; }
char GetValue(const int i);
int GetWeight(const int v1,const int v2);
void InsertV(const char &v);
void InsertEdges(const int v1,const int v2,int weight);
void InsertEdges(const int v1,const int v2,int weight,int k1); void deleteV(const int v);
void deleteEdges(const int v1,const int v2);
void deleteEdges(const int v1,const int v2,int k1);
void deleteAdjion(int n);
void CreateGraph(char V[],int n,RCW r[],int e);
void CreateAdjion(int n,int k1,int k2,RCW r[]);
void dfsAdjion(bool *&visited,int i,int n);
void bfsAdjion(bool *&visited,int i,int n);
void check(int n,int &i,int &j);
void dfsAdjion(int n);
void bfsAdjion(int n);
};
#include<iostream>
#include<iomanip>
using namespace std;
#include"(6.3)graph.h"
AdjAdjion::AdjAdjion(edgenode **gl,int n)
{
GL = gl;
for(int i=0;i<n;i++)
g[i].adj = GL[i] = NULL;
size= numE =0;
}
void AdjAdjion::CreateAdjion(int n,int k1,int k2,RCW r[]) {
int i,j,k,e,w;
cout<<"cin the e"<<endl;
cin>>e;
if(k1==0&&k2==0){
cout<<"cin the e numE \n";
for(k=1;k<=e;k++){
cin>>i>>j;
check(n,i,j);
edgenode *p = new edgenode;
p->adjvex = j;
p->weight = 1;
p->next = GL[i];
GL[i] = p;
p = new edgenode;
p->adjvex = i;
p->weight = 1;
p->next = GL[j];
GL[j] = p;
}
}
else if(k1==0&&k2!=0){ cout<<"cin the e numE \n"; for(k=0;k<e;k++){ i = r[k].row; j = r[k].col; w = r[k].weight; check(n,i,j); edgenode *p = new edgenode; p->adjvex = j; p->weight = w; p->next = GL[i]; GL[i] = p; p = new edgenode; p->adjvex = i; p->weight = w; p->next = GL[j]; GL[j] = p; } } else if(k1!=0&&k2==0){ cout<<"cin the e numE \n"; for(k=1;k<=e;k++){ cin>>i>>j; check(n,i,j); edgenode *p = new edgenode; p->adjvex = j; p->weight = 1; p->next = GL[i]; GL[i] = p; } } else if(k1!=0&&k2!=0){ cout<<"cin the e numE \n"; for(k=0;k<e;k++){ cin>>i>>j>>w; check(n,i,j); edgenode *p = new edgenode; p->adjvex = j; p->weight = w; p->next = GL[i]; GL[i] = p; } } size = n;
numE = e;
}
void AdjAdjion::dfsAdjion(bool *& visited,int i,int n) {
cout<<g[i].data<<":"<<i<<" ";
visited[i] = true;
edgenode *p = g[i].adj;
while(p!=NULL){
int j =p->adjvex;
if(!visited[j])
dfsAdjion(visited,j,n);
p = p->next;
}
}
void AdjAdjion::bfsAdjion(bool *& visited,int i,int n) {
const int MaxLength = 30;
int Q[MaxLength];
int front =0,rear =0;
cout<<g[i].data<<":"<<i<<" ";
visited[i] = true;
rear = (rear+1)%MaxLength;
Q[rear] = i;
edgenode *p;
while(front!=rear){
front = (front+1)%MaxLength;
p = g[Q[front]].adj;
while(p!=NULL){
int j = p->adjvex;
if(!visited[j]){
cout<<g[j].data<<":"<<j<<" ";
visited[j] = true;
rear = (rear+1)%MaxLength;
Q[rear] = j;
}
p = p->next;
}
}
}
void AdjAdjion::check(int n,int &i,int &j)
{
if(i<0||i>=n||j<0||j>=n)
cerr<<"error cin again"<<endl;
else
return;
cin>>i>>j;
}
char AdjAdjion::GetValue(const int i)
{
if(i<0||i>=size){
cerr<<"error eixt(1)";
exit(1);
}
return g[i].data;
}
int AdjAdjion::GetWeight(const int v1,const int v2)
{
if(v1<0||v1>=size||v2<0||v2>=size){
cerr<<"error exit"<<endl;
exit(1);
}
edgenode *p = g[v1].adj;
while(p!=NULL&&p->adjvex<v2)
p = p->next;
if(!p||v2!=p->adjvex){
cerr<<"v1 v2 not exist exit"<<endl;
exit(1);
}
return p->weight;
}
void AdjAdjion::InsertV(const char &v)
{
g[size].adj = NULL;
g[size].data = v;
size++;
}
void AdjAdjion::InsertEdges(const int v1,const int v2,int weight) {
//this k1!=0
//? k1==0 exchange v1 v2
if(v1<0||v1>=size||v2<0||v2>=size){
本文来源:http://www.guakaob.com/jianzhugongchengkaoshi/175211.html
上一篇:创业计划书创业组织
下一篇:人,要有五识阅读题及答案