课本是 《数据结构 C语言版第2版》 严蔚敏 李冬梅 吴伟民 编著
顺序表的基本操作实现
#include <iostream>
#include <iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType*elem;
int length;
}SqList;
//初始化
Status InitList_Sq(SqList &L){
L.elem=new ElemType[MAXSIZE];
if(!L.elem)
exit(OVERFLOW);
L.length=0;
//初始化了一下数组
for(int i=0;i<L.length;i++)
L.elem[i]=0;
return OK;
}
//取值
Status GetElem(SqList L,int i ,ElemType &e){
if((i<1)||(i>L.length+1))
return ERROR;
e=L.elem[i-1];
return OK;
}
//插入
Status ListInsert_Sq(SqList &L,int i,ElemType e){
if((i<1)||(i>L.length+1))
return ERROR;
if(L.length==MAXSIZE)
return ERROR;
for(int j=L.length;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
L.length++;
return OK;
}
//查找
int LocateElem_Sq(SqList L,int e){
for(int i=0;i<L.length+1;i++)
if(L.elem[i]==e)
return i+1;
return 0;
}
//删除
Status ListDelete(SqList &L,int i){
if((i<1)||(i>L.length+1))
return ERROR;
for(int j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
L.length--;
return OK;
}
//打印链表
void ListDisplay_sq(SqList L){
for (int i=0;i<L.length+1;i++)
cout<<L.elem[i]<<' ';
cout <<endl;
}
int main()
{
SqList L;
int i=0,temp;
ElemType e;
InitList_Sq(L);
ListInsert_Sq(L,1,2);
ListInsert_Sq(L,1,3);
ListInsert_Sq(L,1,4);
ListInsert_Sq(L,1,5);
ListInsert_Sq(L,1,6);
ListInsert_Sq(L,1,7);
ListInsert_Sq(L,1,8);
ListInsert_Sq(L,1,9);
ListInsert_Sq(L,1,10);
ListDisplay_sq(L);
temp=GetElem(L,3,e);
cout<<"第3位查找结果为:"<<e<<endl;
temp=LocateElem_Sq(L,6);
cout<<"6的位序为:"<<temp<<endl;
//ListInsert_Sq(L,4,66);
//ListDisplay_sq(L);
ListDelete(L,6);
cout<<"删除第6位:"<<endl;
ListDisplay_sq(L);
}
输出结果:
-842150451
第3位查找结果为:-858993460
6的位序为:0
删除第6位:
-842150451
Press any key to continue
单链表基本操作的实现
#include <iostream>
#include <stdio.h>
#include <iomanip>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
int j;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
//对同一结构体指针类型起了两个名字,LinkList=LNode*
//初始化
Status InitList(LinkList &L){
//L为头结点
L=new LNode;
//头结点指向的下一位(指针域),即首元结点为NULL,说明这是个空表
L->next=NULL;
return OK;
}
//取值
Status GetElem(LinkList L,int i ,ElemType &e){
//p指向首元结点
LNode*p=L->next;
j=1;
//读取,直到第i位
while(p&&(j<i))
{
//获取下一位(指针域)
p=p->next;
++j;
}
if(!p||(j>i)) return ERROR;
//取数据域
e=p->data;
return OK;
}
//查找
Status LocateElem(LinkList L, ElemType e){
//p指向首元结点
LNode*p=L->next;
j=1;
//读取,直到找到e/p为空
while(p&&(p->data!=e))
{
//获取下一位(指针域)
p=p->next;
++j;
}
return j;
}
//插入
Status ListInsert(LinkList &L,int i ,ElemType e){
LNode*p=L;
j=0;
//p从头结点开始巡逻,直到j=i-1/p为空
while(p&&(j<i-1))
{
p=p->next;
++j;
}
//i合规
if(!p||(j>i-1)) return ERROR;
LNode*s=0;
//生成新节点
s=new LNode;
//为s结点赋值为e
s->data=e;
//s的指针域为原i-1指向的值,即为i
s->next=p->next;
//p此时在i-1的位置,指针域为s
p->next=s;
return OK;
}
//删除
Status ListDelete(LinkList &L,int i ){
//p指向首元结点
LNode*p=L->next;
j=1;
LNode*q=0;
//读取,直到第i-1位/p->next为空
while((p->next)&&(j<i-1))
{
//获取下一位(指针域)
p=p->next;
++j;
}
if(!(p->next)||(j>i-1)) return ERROR;
q=p->next;
p->next=q->next;
delete q;
return OK;
}
//打印链表
void ListDisplay(LinkList L){
LNode*p=L;
j=1;
for (;p->next!=0;j++)
{
cout<<((p->next)->data)<<' ';
p=p->next;
}
cout <<endl;
}
void main()
{
LNode* L;
int i=0,temp=0;
LNode* TEMP;
ElemType e;
InitList(L);
ListInsert(L,1,2);
cout<<"在第一个位置插入2:"<<endl;
ListDisplay(L);
ListInsert(L,1,3);
cout<<"在第一个位置插入3:"<<endl;
ListDisplay(L);
ListInsert(L,1,4);
cout<<"在第一个位置插入4:"<<endl;
ListDisplay(L);
temp=GetElem(L,3,e);
cout<<"第3位的取值为:"<<e<<endl;
ListDisplay(L);
temp=LocateElem(L,4);
cout<<"4的位序为:"<<temp<<endl;
ListDisplay(L);
ListDelete(L,2);
cout<<"删除第2位:"<<endl;
ListDisplay(L);
}
输出:
在第一个位置插入2:
2
在第一个位置插入3:
3 2
在第一个位置插入4:
4 3 2
第3位的取值为:2
4 3 2
4的位序为:1
4 3 2
删除第2位:
4 2
Press any key to continue
双向链表
线性表的应用
线性表的合并
#include <iostream>
#include <iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType*elem;
int length;
}SqList;
//初始化
Status InitList_Sq(SqList &L){
L.elem=new ElemType[MAXSIZE];
if(!L.elem)
exit(OVERFLOW);
L.length=0;
//初始化了一下数组
for(int i=0;i<L.length;i++)
L.elem[i]=0;
return OK;
}
//取值
Status GetElem(SqList L,int i ,ElemType &e){
if((i<1)||(i>L.length+1))
return ERROR;
e=L.elem[i-1];
return OK;
}
//插入
Status ListInsert_Sq(SqList &L,int i,ElemType e){
if((i<1)||(i>L.length+1))
return ERROR;
if(L.length==MAXSIZE)
return ERROR;
for(int j=L.length;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
L.length++;
return OK;
}
//查找
int LocateElem_Sq(SqList L,int e){
for(int i=0;i<L.length+1;i++)
if(L.elem[i]==e)
return i+1;
return 0;
}
//删除
Status ListDelete(SqList &L,int i){
if((i<1)||(i>L.length))
return ERROR;
for(int j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
L.length--;
return OK;
}
//打印链表
void ListDisplay_sq(SqList L){
for (int i=0;i<L.length;i++)
cout<<L.elem[i]<<' ';
cout <<endl;
}
//线性表的合并
void hebing(SqList&A,SqList B){
int temp=0;
for(int i=1;i<B.length+1;i++){
// cout<<"i为:"<<i<<endl;
GetElem(B,i,temp);
// cout<<"temp为:"<<temp<<endl;
if(!LocateElem_Sq(A,temp))
ListInsert_Sq(A,A.length+1,temp);
// cout<<"A表长度为:"<<A.length<<endl;
// cout<<"A表为:"<<endl;
// ListDisplay_sq(A);
}
}
//依次取出B表中的每个元素
//在A表中查找该元素
//如果找不到,则将其插在A表之后
int main()
{
SqList A;
SqList B;
int i=0,temp;
ElemType e;
InitList_Sq(A);
InitList_Sq(B);
ListInsert_Sq(A,1,1);
ListInsert_Sq(A,1,2);
ListInsert_Sq(A,1,3);
ListInsert_Sq(A,1,4);
// cout<<"A表长度为:"<<A.length<<endl;
cout<<"A表为:"<<endl;
ListDisplay_sq(A);
ListInsert_Sq(B,1,3);
ListInsert_Sq(B,1,4);
ListInsert_Sq(B,1,5);
ListInsert_Sq(B,1,6);
cout<<"B表为:"<<endl;
ListDisplay_sq(B);
hebing(A,B);
cout<<"合并后的A表为:"<<endl;
ListDisplay_sq(A);
}
栈
顺序栈
#include <iostream>
#include <iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;
//顺序栈的定义
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//初始化
Status InitStack(SqStack &S){
S.base=new SElemType[MAXSIZE];
if(!S.base) exit(ERROR);
S.top=S.base;
S.stacksize=MAXSIZE;
return OK;
}
//入栈
Status Push(SqStack &S,SElemType e){
if(S.top-S.base==S.stacksize) return ERROR;//栈满了
*S.top++=e;//导入e
return OK;
}
//出栈(删除栈顶元素)
Status Pop(SqStack &S,SElemType &e){
if(S.top-S.base==0) return ERROR;//栈空
e=*(--S.top);
return OK;
}
//取栈顶元素
SElemType GetTop(SqStack S){
if(S.top!=S.base)
return *(S.top-1);
}
//打印栈中元素
void ListDisplay_Stack(SqStack S){
for (int i=1;i<(S.top-S.base)+1;i++)
cout<<*(S.top-i)<<' ';
cout <<endl;
}
int main(){
SqStack S;
int e;
InitStack(S);
Push(S,6);
Push(S,7);
Push(S,8);
Push(S,9);
Push(S,10);
cout<<"入栈:"<<endl;
ListDisplay_Stack(S);
Pop(S,e);
cout<<"删除栈顶元素"<<e<<"后:"<<endl;
ListDisplay_Stack(S);
cout<<"取栈顶元素:"<<GetTop(S)<<endl;
return 0;
}
输出结果:
入栈:
10 9 8 7 6
删除栈顶元素10后:
9 8 7 6
取栈顶元素:9
Press any key to continue
链栈
#include <iostream>
#include <iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;
//链栈的定义
typedef struct StackNode{
SElemType data;
struct StackNode *next;
}StackNode,*LinkStack;
//初始化
Status InitStack(LinkStack &S){
S=NULL;//栈顶指针置空
return OK;
}
//入栈
Status Push(LinkStack &S,SElemType e)
{
StackNode* p;
p=new StackNode;//生成新结点
p->data=e;//新结点的数据域为e
p->next=S;//新结点的指针域为前S(栈顶指针域)
S=p;//原栈顶的指针域变为p
return OK;
}
//出栈(删除栈顶元素)
Status Pop(LinkStack &S,SElemType &e)
{
LinkStack p;
if(S==NULL) return ERROR;//空栈
e=S->data;//存储删除的元素值
p=S;//用p暂时保存栈顶元素空间,以备释放
S=S->next;//因为栈顶的元素被删除,将S的指针域改为S的下一位的指针域
delete p;//释放p
return OK;
}
//取栈顶元素
SElemType GetTop(LinkStack S){
if(S==NULL) return ERROR;//空栈
return S->data;
}
//打印栈中元素
void ListDisplay_Stack(LinkStack S){
LinkStack p;
p=S;
while(p!=NULL)
{
cout<<p->data<<' ';
p=p->next;
}
cout <<endl;
}
int main()
{
LinkStack S;
int e;
InitStack(S);
Push(S,1);
Push(S,2);
Push(S,3);
Push(S,4);
Push(S,5);
cout<<"入栈后"<<endl;
ListDisplay_Stack(S);
Pop(S,e);
cout<<"出栈的元素为"<<e<<endl;
cout<<"取此时栈顶元素为"<<GetTop(S)<<endl;
return 0;
}
输出结果:
入栈后
5 4 3 2 1
出栈的元素为5
取此时栈顶元素为4
Press any key to continue
队列
循环队列
#include <iostream>
#include <iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int QElemType;
typedef struct{
QElemType *base;//基地址(我感觉也是值)
int front;//头指针
int rear;//尾指针
}SqQueue;
//初始化
Status InitQueue(SqQueue &Q){
Q.base=new QElemType[MAXSIZE];//构建空队列,并分配数组空间
if(!Q.base) exit(OVERFLOW);
Q.front=Q.rear=0;//头指针和尾指针置为0,队列为空
return OK;
}
//求循环队列长度
int QueueLength(SqQueue Q)
{
return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;//避免出现负数
}
//入队
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%MAXSIZE==Q.front)//尾指针+1=头指针的话,表明队满
return ERROR;
Q.base[Q.rear]=e;//新元素e插入duiwei
Q.rear=(Q.rear+1)%MAXSIZE;//尾指针+1
}
//出队(先进先出)
Status DeQueue(SqQueue &Q,QElemType &e)
{
if(Q.front==Q.rear) return ERROR;//队空
e=Q.base[Q.front];//e储存要出队的值
Q.front=(Q.front+1)%MAXSIZE;//队头指针+1
return OK;
}
//取队头元素
QElemType GetHead(SqQueue Q)
{
if(Q.front!=Q.rear)//队不空的话
return Q.base[Q.front];//返回队头元素的值,队头指针不变
}
//打印栈中元素
void ListDisplay_Queue(SqQueue Q){
int i=Q.front ;
while(i<Q.rear)
{
cout<<Q.base[i]<<' ';
i++;
}
cout <<endl;
}
/*
*/
int main()
{
SqQueue Q;
int e;
InitQueue(Q);
EnQueue(Q,2);
EnQueue(Q,4);
EnQueue(Q,6);
EnQueue(Q,8);
EnQueue(Q,10);
EnQueue(Q,12);
cout<<"入队后的循环队列为"<<endl;
ListDisplay_Queue(Q);
cout<<"现在循环队列长度"<<QueueLength(Q)<<endl;
cout<<"队列头元素为"<<GetHead(Q)<<endl;
cout<<endl;
// 出队
DeQueue(Q,e);
cout<<"出队的元素为"<<e<<endl;
cout<<"出队后的循环队列为"<<endl;
ListDisplay_Queue(Q);
cout<<"现在循环队列长度"<<QueueLength(Q)<<endl;
cout<<"队列头元素为"<<GetHead(Q)<<endl;
return 0;
}
链队
#include <iostream>
#include <iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int QElemType;
//队列的链式储存结构
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
//初始化
Status InitQueue(LinkQueue &Q){
Q.front=Q.rear=new QNode;//生成新结点作为头结点,队头和队尾指针指向此节点
Q.front->next=NULL;//头结点的指针域置空
return OK;
}
//入队
Status EnQueue(LinkQueue &Q,QElemType e)
{
QueuePtr p;
p=new QNode ;//为新入队元素分配空间,用指针p指向
p->data=e;//把新元素的值赋给p
p->next=NULL;
Q.rear->next=p;//将新结点插入队尾
Q.rear=p;//修改队尾指针
return OK;
}
//出队
Status DeQueue(LinkQueue &Q, QElemType &e)
{
QueuePtr p;
if(Q.front==Q.rear) return ERROR;//空队列
p=Q.front->next;//p指向队头元素
e=p->data;//e储存要出队的值
Q.front->next=p->next;//头结点的指针域指向,头结点的指针域的指针域
if(Q.rear==p) Q.rear=Q.front;//如果删的是最后一个元素
delete p;//释放p
return OK;
}
//取队头元素
QElemType GetHead(LinkQueue Q)
{
if(Q.front!=Q.rear)
return Q.front->next->data;
}
//打印栈中元素
///*
void ListDisplay_LinkQueue(LinkQueue Q){
QueuePtr p;
p=Q.front->next;
while(p->next!=NULL)
{
cout<<p->data<<' ';
p=p->next;
}
cout <<endl;
}
//*/
int main()
{
LinkQueue Q;
int e;
InitQueue(Q);
EnQueue(Q,11);
EnQueue(Q,22);
EnQueue(Q,33);
EnQueue(Q,44);
EnQueue(Q,55);
EnQueue(Q,66);
cout<<"入队后:"<<endl;
ListDisplay_LinkQueue(Q);
cout<<"取队头元素为"<<GetHead(Q)<<endl;
cout<<endl;
DeQueue(Q,e);
cout<<"出队元素为"<<e<<endl;
cout<<"出队后:"<<endl;
ListDisplay_LinkQueue(Q);
cout<<"取队头元素为"<<GetHead(Q)<<endl;
return 0;
}
运行结果
入队后:
11 22 33 44 55
取队头元素为11
出队元素为11
出队后:
22 33 44 55
取队头元素为22
Press any key to continue
邻接矩阵
无向图的邻接矩阵
如果存在i顶点到j顶点之间的边或弧,则邻接矩阵记作1
通过邻接矩阵可以知道某一个顶点的度为多少,度就是就是和某一个顶点相关联的边的个数
有向图的邻接矩阵
V1作为出度边,到V2和V3都有弧,标为1
V2没有发出任何弧,全为0
网(有向网)的邻接矩阵
网的边有权值
邻接矩阵创建无向网
LocateVex
LocateVex函数:得到当前输入的顶点在顶点表当中的位置(下标)
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <iostream>
using namespace std;
#define MaxInt 32767
#define MVNum 100
#define OK 1
#define ERROR 0
typedef int Status;
typedef char VerTexType;//假设顶点的数据类型为char型
typedef int ArcType;//假设边的权值为int型
typedef struct{
VerTexType vexs[MVNum];//顶点表
ArcType arcs[MVNum][MVNum];//邻接矩阵
int vexnum,arcnum;//图的当前点数和边数
}AMGraph;
//在图中查找顶点u,存在的话返回顶点表中的下标
int LocateVex(AMGraph G,VerTexType u)
{
int i;
for(i=0;i<G.vexnum;++i)
if(u==G.vexs[i])
return i;
return -1;
}
//用邻接矩阵创建无向网
Status CreateUDN(AMGraph &G)
{
char v1,v2;
int i,w,j;
cout<<"请输入总点数和边数(用空格隔开,比如:5 6)"<<endl;
cin>>G.vexnum>>G.arcnum;//总点数和边数
cout<<"依次输入顶点值(用空格隔开,比如:a b c d e)"<<endl;
for(i=0;i<G.vexnum;++i)//依次输入点
{
cin>>G.vexs[i];
cout<<"输入的点为"<<G.vexs[i]<<endl;
}
for(i=0;i<G.vexnum;++i)
for( j=0;j<G.vexnum;++j)
{
G.arcs[i][j]=0;//初始化为最大值
//cout<<"初始化完毕"<<endl;
}
//构造邻接矩阵
for(int k=0;k<G.arcnum;++k)
{
cout<<"输入一条边所依附的顶点和边的权值"<<endl;
cin>>v1>>v2>>w;//输入一条边所依附的顶点和边的权值
i=LocateVex(G,v1);
j=LocateVex(G,v2);
cout<<"在邻接矩阵的位置为"<<"["<<i<<","<<j<<"]"<<endl;
G.arcs[i][j]=w;
G.arcs[j][i]=G.arcs[i][j];//对称
}
return OK;
}
//输出邻接矩阵
int main()
{
int i,j;
AMGraph G;
CreateUDN(G);
for(i=0;i<G.vexnum;++i)
{
for( j=0;j<G.vexnum;++j)
cout<<G.arcs[i][j]<<" ";//初始化为最大值
cout<<endl;
}
return 0;
}
输入输出如下:
邻接表
无向图表示方式:
有向图表示方式:
邻接表创建无向网:
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <iostream>
using namespace std;
#define MaxInt 32767
#define MVNum 100
#define OK 1
#define ERROR 0
typedef int Status;
typedef int OtherInfo;
typedef char VerTexType;//假设顶点的数据类型为char型
typedef int ArcType;//假设边的权值为int型
//边结点
typedef struct ArcNode
{
int adjvex;//该边所指向的顶点的位置
struct ArcNode *nextarc;//指向下一条边的指针
OtherInfo info;//权值
}ArcNode;
//顶点信息
typedef struct VNode
{
VerTexType data;//顶点信息
ArcNode *firstarc;//指向边结点的一个指针
}VNode,AdjList[MVNum];
//邻接表
typedef struct
{
AdjList vertices;//存放了多个顶点
int vexnum,arcnum;
}ALGraph;
//在图中查找顶点u,存在的话返回顶点表中的下标
int LocateVex(ALGraph G,VerTexType u)
{
int i;
for(i=0;i<G.vexnum;++i)
if(u==G.vertices[i].data)//eg:第i顶点是b
return i;
return -1;
}
//创建无向网
Status CreateUDG(ALGraph &G)
{
int i,k,j;
char v1,v2;
ArcNode*p1;
ArcNode*p2;
cout<<"请输入总点数和边数(用空格隔开,比如:5 6)"<<endl;
cin>>G.vexnum>>G.arcnum;
cout<<"依次输入顶点值(用空格隔开,比如:a b c d e)"<<endl;
for(i=0;i<G.vexnum;++i)
{
cin>>G.vertices[i].data;
G.vertices[i].firstarc=NULL;//初始化表头结点的指针域
}
//建立若干个单链表
for(k=0;k<G.arcnum;++k)
{
cout<<"输入一条边依附的两个顶点(用空格隔开,比如:a b)"<<endl;
cin>>v1>>v2;
//找到下标
i=LocateVex(G,v1);
j=LocateVex(G,v2);
cout<<v1<<"的下标为"<<i<<endl;
cout<<v2<<"的下标为"<<j<<endl;
//如何把一条边插入到单链表当中
//比如我们输入b e 找到下标分别为i j
p1=new ArcNode;//生成一个新结点*p1
p1->adjvex=j;//邻结点e的下标
//G.vertices[i].firstarc:第i顶点(是b)的第一条边结点
//指针指向b的第一条边结点
p1->nextarc=G.vertices[i].firstarc;
G.vertices[i].firstarc=p1;
//如果想要有向网的话,把下面这点删了就行
//e->b的
p2=new ArcNode;
p2->adjvex=i;
p2->nextarc=G.vertices[j].firstarc;
G.vertices[j].firstarc=p2;
}
return OK;
}
int main()
{
int i,j;
ALGraph G;
CreateUDG(G);
for(i=0;i<G.vexnum;++i)
{
cout<<G.vertices[i].data<<" ";
while(G.vertices[i].firstarc)
{
cout<<"->"<<" ";
cout<<G.vertices[i].firstarc->adjvex<<"";
G.vertices[i].firstarc=G.vertices[i].firstarc->nextarc;
}
cout<<endl;
}
return 0;
}
输入和输出
深度优先搜索遍历算法
邻接矩阵深度优先搜索:
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <iostream>
using namespace std;
#define MaxInt 32767
#define MVNum 100
#define OK 1
#define ERROR 0
typedef int Status;
typedef char VerTexType;//假设顶点的数据类型为char型
typedef int ArcType;//假设边的权值为int型
typedef struct{
VerTexType vexs[MVNum];//顶点表
ArcType arcs[MVNum][MVNum];//邻接矩阵
int vexnum,arcnum;//图的当前点数和边数
}AMGraph;
int visited[100];
//在图中查找顶点u,存在的话返回顶点表中的下标
int LocateVex(AMGraph G,VerTexType u)
{
int i;
for(i=0;i<G.vexnum;++i)
if(u==G.vexs[i])
return i;
return -1;
}
//用邻接矩阵创建无向网
Status CreateUDN(AMGraph &G)
{
char v1,v2;
int i,w,j;
cout<<"请输入总点数和边数(用空格隔开,比如:5 6)"<<endl;
cin>>G.vexnum>>G.arcnum;//总点数和边数
cout<<"依次输入顶点值(用空格隔开,比如:a b c d e)"<<endl;
for(i=0;i<G.vexnum;++i)//依次输入点
{
cin>>G.vexs[i];
cout<<"输入的点为"<<G.vexs[i]<<endl;
}
for(i=0;i<G.vexnum;++i)
for( j=0;j<G.vexnum;++j)
{
G.arcs[i][j]=0;//初始化为最大值
//cout<<"初始化完毕"<<endl;
}
//构造邻接矩阵
for(int k=0;k<G.arcnum;++k)
{
cout<<"输入一条边所依附的顶点和边的权值"<<endl;
cin>>v1>>v2>>w;//输入一条边所依附的顶点和边的权值
i=LocateVex(G,v1);
j=LocateVex(G,v2);
cout<<"在邻接矩阵的位置为"<<"["<<i<<","<<j<<"]"<<endl;
G.arcs[i][j]=w;
G.arcs[j][i]=G.arcs[i][j];//对称
}
return OK;
}
//深度优先搜索遍历
//从第v个顶点出发深度优先遍历图G
void DFS_AM(AMGraph G,int v)
{
int w;
//访问起始顶点
// cout<<"起始顶点v为:"<<v<<endl;
//并记录它已经访问过了
// cout<<"实际在邻接矩阵中的行坐标为:"<<v<<endl;
visited[v]=1;
// cout<<"visited["<<v<<"]:"<<visited[v]<<endl;
for(w=0;w<G.vexnum;w++)//依次检查矩阵v所在的行
{
// cout<<"邻接矩阵G.arcs["<<v<<","<<w<<"]:"<<G.arcs[v][w]<<endl;
// cout<<"w:"<<w<<endl;
if((G.arcs[v][w]!=0)&&(!visited[w]))
{
cout<<"->"<<w+1<<" ";
//如果w未访问,则调用DFS
DFS_AM(G,w);
// G.arcs[v][w]!=0
}
}
}
int main()
{
int i,j;
int v;
AMGraph G;
CreateUDN(G);
for(i=0;i<G.vexnum;++i)
{
for( j=0;j<G.vexnum;++j)
cout<<G.arcs[i][j]<<" ";//初始化为最大值
cout<<endl;
}
cout<<"请输入需要深度优先遍历的顶点"<<endl;
cin>>v;
cout<<v<<" ";
v=v-1;
DFS_AM(G,v);
return 0;
}
输入输出:
邻接表深度优先搜索:
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <iostream>
using namespace std;
#define MaxInt 32767
#define MVNum 100
#define OK 1
#define ERROR 0
typedef int Status;
typedef int OtherInfo;
typedef char VerTexType;//假设顶点的数据类型为char型
typedef int ArcType;//假设边的权值为int型
int visited[100];
//边结点
typedef struct ArcNode
{
int adjvex;//该边所指向的顶点的位置
struct ArcNode *nextarc;//指向下一条边的指针
OtherInfo info;//权值
}ArcNode;
//顶点信息
typedef struct VNode
{
VerTexType data;//顶点信息
ArcNode *firstarc;//指向边结点的一个指针
}VNode,AdjList[MVNum];
//邻接表
typedef struct
{
AdjList vertices;//存放了多个顶点
int vexnum,arcnum;
}ALGraph;
//在图中查找顶点u,存在的话返回顶点表中的下标
int LocateVex(ALGraph G,VerTexType u)
{
int i;
for(i=0;i<G.vexnum;++i)
if(u==G.vertices[i].data)//eg:第i顶点是b
return i;
return -1;
}
//创建无向网
Status CreateUDG(ALGraph &G)
{
int i,k,j,num;
num=0;
char v1,v2;
ArcNode*p1;
ArcNode*p2;
cout<<"请输入总点数和边数(用空格隔开,比如:5 6)"<<endl;
//cin>>G.vexnum>>G.arcnum;
G.vexnum=5;
G.arcnum=6;
cout<<"依次输入顶点值(用空格隔开,比如:a b c d e)"<<endl;
for(i=0;i<G.vexnum;++i)
{
cin>>G.vertices[i].data;
//G.vertices[i].data=++num;
G.vertices[i].firstarc=NULL;//初始化表头结点的指针域
}
//建立若干个单链表
for(k=0;k<G.arcnum;++k)
{
cout<<"输入一条边依附的两个顶点(用空格隔开,比如:a b)"<<endl;
cin>>v1>>v2;
//找到下标
i=LocateVex(G,v1);
j=LocateVex(G,v2);
cout<<v1<<"的下标为"<<i<<endl;
cout<<v2<<"的下标为"<<j<<endl;
//如何把一条边插入到单链表当中
//比如我们输入b e 找到下标分别为i j
p1=new ArcNode;//生成一个新结点*p1
p1->adjvex=j;//邻结点e的下标
//G.vertices[i].firstarc:第i顶点(是b)的第一条边结点
//指针指向b的第一条边结点
p1->nextarc=G.vertices[i].firstarc;
G.vertices[i].firstarc=p1;
//如果想要有向网的话,把下面这点删了就行
//e->b的
p2=new ArcNode;
p2->adjvex=i;
p2->nextarc=G.vertices[j].firstarc;
G.vertices[j].firstarc=p2;
}
return OK;
}
//深度优先搜索
void DFS_AL(ALGraph& G,int v)
{
ArcNode*p;
// p=new ArcNode;
int w;
// cout<<"顶点实际位序:"<<v<<endl;
visited[v]=1;
// cout<<"G.vertices[v].data:"<<G.vertices[v].data<<endl;
//cout<<"G.vertices[v].firstarc:"<<G.vertices[v].firstarc<<endl;
p=G.vertices[v].firstarc;
//cout<<"顶点"<<v+1<<"相连的第一个边结点"<<p->adjvex<<endl;
while(p!=NULL)
{
w=p->adjvex;
//cout<<v+1<<"的邻结点为"<<w<<endl;
if(!visited[w])
{
DFS_AL(G,w);
cout<<"->"<<w+1<<" ";
}
p=p->nextarc;
}
}
int main()
{
int i,j,v;
ALGraph G;
CreateUDG(G);
/*
for(i=0;i<G.vexnum;++i)
{
cout<<G.vertices[i].data<<" ";
while(G.vertices[i].firstarc)
{
cout<<"->"<<" ";
cout<<G.vertices[i].firstarc->adjvex<<"";
G.vertices[i].firstarc=G.vertices[i].firstarc->nextarc;
}
cout<<endl;
}
*/
cout<<"请输入顶点v"<<endl;
cin>>v;
v=v-1;
DFS_AL(G,v);
return 0;
}
广度优先搜索遍历算法
输入输出