資源簡介
圖的鄰接矩陣存儲和鄰接表存儲 代碼完整 有注釋
代碼片段和文件信息
#include?
#include?
#define?MAXVEX?100
typedef?char?VertexType[3];
typedef?struct?edgenode
{
int?adjvex;?????? /*鄰接點序號*/
int?value;?? /*邊的權值*/
struct?edgenode?*next; /*下一條邊的頂點*/
}?ArcNode; /*每個頂點建立的單鏈表中結點的類型*/
typedef?struct?vexnode
{
VertexType?data;??????? /*結點信息*/
ArcNode?*firstarc;? /*指向第一條邊結點*/
}?VHeadNode; /*單鏈表的頭結點類型*/
typedef?struct?
{
int?ne; /*n為實際頂點數e為實際邊數*/
VHeadNode?adjlist[MAXVEX]; /*單鏈表頭結點數組*/
}?AdjList;? /*圖的鄰接表類型*/
int?CreateAdjList(AdjList?*&G) /*建立有向圖的鄰接表*/
{
int?ibtw;
ArcNode?*p;
G=(AdjList?*)malloc(sizeof(AdjList));
printf(“頂點數(n)邊數(e):“);
scanf(“%d%d“&G->n&G->e);
for?(i=0;in;i++)
{
printf(“???序號為%d的頂點信息:“?i);
scanf(“%s“G->adjlist[i].data);
G->adjlist[i].firstarc=NULL;
}
for?(i=0;ie;i++)
{
printf(“???序號為邊=>“i);
printf(“?起點號?終點號?權值:“);
scanf(“%d%d%d“&b&t&w);
if?(bn?&&?tn?&&?w>0)
{
p=(ArcNode?*)malloc(sizeof(ArcNode)); /*建立*p結點*/
p->value=w;p->adjvex=t;
p->next=G->adjlist[b].firstarc; ?/**p插入到adjlist[b]的單鏈表之首*/
G->adjlist[b].firstarc=p;
}
else
{
printf(“輸入錯誤!\n“);
return(0);
}
}
return(1);
}
void?DispAdjList(AdjList?*G)
{
int?i;
ArcNode?*p;
printf(“圖的鄰接表表示如下:\n“);
for?(i=0;in;i++)
{
printf(“??[%d%3s]=>“iG->adjlist[i].data);
p=G->adjlist[i].firstarc;
while?(p!=NULL)
{
printf(“(%d%d)->“p->adjvexp->value);
p=p->next;
}
printf(“∧\n“);
}
}
void?main()
{
AdjList?*G;
CreateAdjList(G);
DispAdjList(G);
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1249??2006-07-16?15:01??creatematix.cpp
?????文件???????1732??2006-09-07?14:25??createadjlist.cpp
-----------?---------??----------?-----??----
?????????????????2981????????????????????2
- 上一篇:摩托羅拉芯片詳細介紹大全
- 下一篇:楊素行第三版模電答案
評論
共有 條評論