邻接表

  1 #include<stdio.h>

  2 #include<stdlib.h>

  3 #include<iostream.h>

  4 #define OK 1

  5 #define TRUE 1

  6 #define FALSE 0

  7 #define ERROR -1

  8 #define OVERFLOW -2

  9 #define MAX_VEX_NUM 20

 10 using namespace std;

 11 typedef int Status;

 12 //定义邻接表存储类型 

 13 typedef char VexType;

 14 typedef struct EdgeNode//定义边表结点类型 

 15 {

 16     int adjvex;

 17     EdgeNode *next;

 18 }EdgeNode,*EdgeLink;

 19 typedef struct VexNode

 20 {

 21     VexType data;

 22     EdgeNode *firstedge;

 23 }VexNode,AdjList[MAX_VEX_NUM];

 24 typedef struct//定义图抽象数据类型 

 25 {

 26     AdjList adjList;

 27     int vexnum,edgenum;

 28 }GraphAdjList;

 29 //建立队列数据结构 

 30 typedef struct QNode //定义一个链表抽象数据类型 

 31 {

 32     int data;

 33     QNode *next;

 34 }*Linklist;

 35 typedef struct Queue

 36 {

 37     Linklist front;

 38     Linklist rear; 

 39 }QList;

 40 Status InitQueue(QList &Q)//初始化队列 

 41 {

 42     Q.front=Q.rear=new QNode;

 43     if(!Q.front) exit(OVERFLOW);

 44     Q.front->next=NULL;

 45     Q.front->data=0; 

 46     return OK;

 47 }

 48 Status EnQueue(QList &Q,int e)//插入函数,用于在队尾插入数据e 

 49 {

 50     Linklist s;

 51     if(!(s=new QNode))

 52     exit(ERROR);

 53     s->data=e;

 54     s->next=NULL; 

 55     Q.rear->next=s;

 56     Q.rear=s;

 57     Q.front->data++; 

 58     return OK;

 59 }

 60 Status DeQueue(QList &Q,int &e)//删除函数,删除队头元素,并把值赋予e

 61 {

 62       Linklist q;

 63       if(Q.front==Q.rear) exit(ERROR);

 64       q=Q.front->next;

 65       Q.front->next=q->next;

 66        e=q->data;

 67        if(q==Q.rear) Q.rear=Q.front;

 68        Q.front->data--;

 69      free(q);

 70        return OK;

 71 }

 72 Status QueueEmpty(QList Q)

 73 {

 74     if(Q.front==Q.rear)

 75         return TRUE;

 76     return FALSE;

 77 }

 78 //创建图的邻接表结构

 79 Status CreateGraph(GraphAdjList &G)

 80 {

 81     int i,j,k;

 82     EdgeLink e;

 83     puts("请输入顶点数目和边数:");

 84     cin>>G.vexnum>>G.edgenum;

 85     puts("请输入顶点数值:"); 

 86     getchar(); 

 87     for(i=0;i<G.vexnum;i++)

 88     {

 89         cin>>G.adjList[i].data;

 90         G.adjList[i].firstedge=NULL;

 91     }

 92     puts("请依次输入边(Vi,Vj)的顶点序号:");

 93     for(k=0;k<G.edgenum;k++)//添加边表结点 

 94     {

 95         cin>>i>>j;

 96         e=new EdgeNode;

 97         e->adjvex=j;

 98         e->next=G.adjList[i].firstedge;

 99         G.adjList[i].firstedge=e;

100         e=new EdgeNode;

101         e->adjvex=i;

102         e->next=G.adjList[j].firstedge;

103         G.adjList[j].firstedge=e;

104     }

105     return OK;

106 }

107 //邻接表的深度优先遍历

108 bool visited[MAX_VEX_NUM]; //定义一个全局数组,用于记录遍历状态 

109 void DFS(GraphAdjList G,int i)//递归从第I个结点深度优先遍历图 

110 {

111     EdgeLink p;

112     visited[i]=TRUE;

113     cout<<G.adjList[i].data;

114     p=G.adjList[i].firstedge;

115     while(p)

116     {

117         if(!visited[p->adjvex])//判断是否已访问 

118             DFS(G,p->adjvex);

119         p=p->next; 

120     }

121 }

122 Status DFSTraverse(GraphAdjList G)

123 {

124     int i;

125     memset(visited,0,sizeof(visited));//Visited置零 

126     for(i=0;i<G.vexnum;i++)//依次访问所有结点,防止出现对非连通图的不完全访问 

127         if(!visited[i])

128             DFS(G,i);

129     return OK;

130 }

131 //邻接表的广度优先遍历

132 Status BFSTraverse(GraphAdjList G)

133 {

134     int i;

135     EdgeLink p;

136     QList Q;

137     InitQueue(Q);

138     memset(visited,0,sizeof(visited));//Visited置零 

139     for(i=0;i<G.vexnum;i++)

140     {

141         if(!visited[i])

142         {

143             visited[i]=TRUE;

144             cout<<G.adjList[i].data;

145             EnQueue(Q,i);

146             while(!QueueEmpty(Q))

147             {

148                 DeQueue(Q,i);

149                 p=G.adjList[i].firstedge;

150                 while(p)

151                 {

152                     if(!visited[p->adjvex])

153                     {

154                         visited[p->adjvex]=TRUE;

155                         cout<<G.adjList[p->adjvex].data;

156                         EnQueue(Q,p->adjvex);

157                     }

158                     p=p->next;

159                 }

160             }

161         }

162     }

163     return OK;

164 }

165 Status main()

166 {

167     GraphAdjList G;

168     CreateGraph(G);

169     puts("邻接表的深度优先遍历为:");

170     DFSTraverse(G);

171     puts("\n邻接表的广度优先遍历为:");

172     BFSTraverse(G);

173     system("pause");

174     return OK;

175 }

你可能感兴趣的:(表)