这个试图的邻接表存储和遍历
#include <malloc.h>
#include <string.h>
#define List_Max_Size 100
#define Add_Size 10
#define Null 0
#define Row 4
#define True 1
#define False 0
#include <stdio.h>
struct Element{
int Data,Addr,Length;
struct Element *Next,*Pre;
};//定义链式表数据结构体
typedef Element Chain_Save_Struct;
typedef Element Head_Chain;
int Create_Chain( Chain_Save_Struct &List )
{
List.Length = 0;
List.Next = Null;
List.Pre = Null;
return (1);
}//建立一个链式表
int Add_Chain ( Head_Chain &List,Chain_Save_Struct **Position)
{
Chain_Save_Struct *Position_1,*PreP;
if (List.Next==Null)
{
List.Next = (Chain_Save_Struct * )malloc ( sizeof(Chain_Save_Struct));
if (!List.Next) return(0);
List.Length=1;
(List.Next -> Next) = Null;
(List.Next ->Pre) = &List;
*Position = List.Next;
return(1);
}
else
{
Position_1 = *Position;
for (*Position = (List.Next);
*Position !=NULL; *Position=(*Position)->Next)PreP = *Position;
PreP->Next = (Chain_Save_Struct * )malloc ( sizeof(Chain_Save_Struct));
if (!List.Next) return(0);
List.Length++;
(PreP->Next->Next) = Null;
(PreP->Next->Pre)= PreP;
*Position = Position_1;
return(1);
}
}//为链表添加一个元素;
int Del_Chain (Chain_Save_Struct &List,Chain_Save_Struct **Position)
{ int a,b,c;
Chain_Save_Struct *PreP,*NPosition;
PreP = Null;
if (List.Next==Null||(*Position)->Pre==Null)
{
return 0;
}//if 链表为空表时
for (NPosition=&List;
NPosition != *Position; NPosition = NPosition->Next)PreP = NPosition;//找到当前删除节点的前驱
NPosition = (*Position)->Next;//当前节点的后继
free(*Position);
--List.Length;
//分为当前节点无后继,当前节点有后继两种情况
if (NPosition == Null)
{ *Position =PreP;
printf("无后继");
scanf("%d",&a);
(*Position)->Next = Null ;
return 1;// 当前节点无后继节点
}
else if (NPosition != Null)
{ printf("有后继");
scanf("%d",&a);
*Position = PreP;
PreP->Next = NPosition;
NPosition->Pre = PreP;
return 1;
}//当前节点有后继节点
}
int Chain_Next (Chain_Save_Struct **Position)
{ if (*Position==Null)return 0;
if ((*Position)->Next == Null) return 0;
*Position=((*Position)->Next);
return 1;
}//下移一个位置
int Chain_Pre (Chain_Save_Struct **Position)
{
if (*Position==Null)return 0;
if ((*Position)->Pre == Null) return 0 ;
*Position=((*Position)->Pre);
return 1;
}//上移一个位置
int Opera(Chain_Save_Struct Adj[Row],int Addr)
{
Adj[Addr].Data = False;
printf("Add=%d,",Addr);
return 1;
}//操作节点
int Deep_Order(Chain_Save_Struct Adj[Row],int Addr)
{
// Opera(Chain_Save_Struct Adj[Row],int Addr);
Element *Point;
Opera(Adj,Addr);
printf(",%d",Adj[Addr].Next);
for(Point = Adj[Addr].Next;Point.Next!=Null && Adj[Point->Addr].Data==False;Point=Point->Next);
Point =Point->Next
if (Point==Null)
{
for(Addr = 0;Addr<=Row-1;Addr++)
{
if (Adj[Addr].Data==True)break;
else if (Addr == Row-1)return 1;
}
}
Deep_Order(Adj,Point->Addr);
return 1;
}//图的邻接表的深度遍历
int main()
{ int Tragle[Row][Row] = {{0,0,0,0},{1,0,1,1},{0,0,0,1},{1,0,0,0}};
Chain_Save_Struct Adj_List[Row],*Point;
int Deep_Order(Chain_Save_Struct Adj[Row],int Addr);
int Addr;
int i,j,Col;
for (i=0;i<=(Row-1);i++)
{
Create_Chain(Adj_List[i]);
Adj_List[i].Data = True;
Point=&(Adj_List[i]);
for (j=0;j<=(Row-1);j++)
{
if (Tragle[i][j]==1)
{
Add_Chain(Adj_List[i],&Point);
if (Point->Next!=Null)Point=Point->Next;
Point->Addr =j;
}
}
}//根据给定的矩阵求图的逆邻接表
for (i=0;i<=(Row-1);i++)
{
printf("是否被访问过:%d,",Adj_List[i].Data);
printf("节点名=%d",i);
for (Point=Adj_List[i].Next;Point!=Null;Point=Point->Next)
{
printf("->%d",Point->Addr);
}
printf("\n");
}
//逆邻接表在此位置
//图的深度优先遍历
Addr =0;
Deep_Order(Adj_List, Addr);
//图的广度优先遍历
scanf("%d",Col);
return 1;
}