注:若有错误或要改善的地方请告之,谢谢,新手上路,有些函数还是很冗余的,慢慢改进。
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<ctype.h>
using namespace std;
/**包含无向图、无向网、有向图、有向网
***全部类型的图中,两个结点间若不存在弧,设置其edge[][]数组中的值为0
***在构建图时,根据输入顺序编号,存放在vex[]数组中,edg[][]数组中顶点的编号与vex[]相同
***vexnum从1开始,指向的是顶点表中顶点的下一位
***编程为无法重新赋值一天边的权值
**/
#define MAXSIZE 100
bool visited[MAXSIZE];
typedef enum
{
NN,//无向图
NY,//无向网
YN,//有向图
YY //有向网
}MGragh_type;
/***************结构体**************/
/*邻接矩阵法*/
typedef struct
{
char vex[MAXSIZE]; //顶点表
int edge[MAXSIZE][MAXSIZE]; //边表
int vexnum,arcnum; //顶点数与边数
int MGragh_type;
}MGragh;
/*循环顺序队列*/
typedef struct seqqueue
{
int data[MAXSIZE]; //存储图结点的编号
int front_,rear_,mark;
}seqqueue;
/*顺序栈*/
typedef struct seqstack
{
int data[MAXSIZE]; //存储图结点的编号
int top;
}seqstack;
/******************队列**************************/
/*队列初始化*/
seqqueue *init_seqqueue()
{
seqqueue *q;
q=(seqqueue*)malloc(sizeof(seqqueue));
q->front_=q->rear_=0;
q->mark=0; //mark为0表示队空,mark为1表示队满
return q;
}
/*判断队空*/
bool isempty_seqqueue(seqqueue q) //true为队空
{
if(q.front_==q.rear_&&(q.mark==0))
{
return true;
}
else
{
return false;
}
}
/*判断队满*/
bool isfull_seqqueue(seqqueue q) //true为队满
{
if(q.front_==q.rear_&&q.mark==1)
{
return true;
}
else
{
return false;
}
}
/*入队*/
void enqueue(seqqueue *q,int e)
{
if(isfull_seqqueue(*q))
{
printf("队满\n");
}
else
{
q->data[q->rear_]=e;
q->rear_=(q->rear_+1)%MAXSIZE;
q->mark=1;
}
}
/*出队*/
void dequeue(seqqueue *q,int &e)
{
if(isempty_seqqueue(*q))
{
printf("队空\n");
}
else
{
e=q->data[q->front_];
q->front_=(q->front_+1)%MAXSIZE;
q->mark=0;
}
}
/******************栈**********************/
/*栈的初始化*/
seqstack *init_stack()
{
seqstack *s;
s=(seqstack*)malloc(sizeof(seqstack));
s->top=-1;
return s;
}
/*判断栈空*/
bool isempty_seqstack(seqstack s)
{
if(s.top==-1)
{
return true;
}
else
{
return false;
}
}
/*判断栈满*/
bool isfull_seqstack(seqstack s)
{
if(s.top==MAXSIZE-1)
{
return true;
}
else
{
return false;
}
}
/*入栈*/
void push(seqstack *s,int e)
{
if(isfull_seqstack(*s))
{
printf("栈满\n");
}
else
{
s->data[++s->top]=e;
}
}
/*出栈*/
void pop(seqstack *s,int &e)
{
if(isempty_seqstack(*s))
{
printf("栈空\n");
}
else
{
e=s->data[s->top--];
}
}
/******************对图操作的公共函数***********************/
/*返回边数*/
int return_arcnum(MGragh *m)
{
int i,j;
m->arcnum=0;
for(i=0;i<m->vexnum;i++)
{
for(j=