//LinkList.h
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef void LinkList;
typedef struct _tag_LinkListNode LinkListNode;
struct _tag_LinkListNode
{
LinkListNode* next;
};
LinkList* LinkList_Create();
void LinkList_Destroy(LinkList* list);
void LinkList_Clear(LinkList* list);
int LinkList_Length(LinkList* list);
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);
LinkListNode* LinkList_Get(LinkList* list, int pos);
LinkListNode* LinkList_Delete(LinkList* list, int pos);
#endif
//LinkList.c
#include <stdio.h>
#include <malloc.h>
#include "LinkList.h"
typedef struct _tag_LinkList
{
LinkListNode header;
int length;
} TLinkList;
LinkList* LinkList_Create() // O(1)
{
TLinkList* ret = (TLinkList*)malloc(sizeof(TLinkList));
if( ret != NULL )
{
ret->length = 0;
ret->header.next = NULL;
}
return ret;
}
void LinkList_Destroy(LinkList* list) // O(1)
{
free(list);
}
void LinkList_Clear(LinkList* list) // O(1)
{
TLinkList* sList = (TLinkList*)list;
if( sList != NULL )
{
sList->length = 0;
sList->header.next = NULL;
}
}
int LinkList_Length(LinkList* list) // O(1)
{
TLinkList* sList = (TLinkList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->length;
}
return ret;
}
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) // O(n)
{
TLinkList* sList = (TLinkList*)list;
int ret = (sList != NULL) && (pos >= 0) && (node != NULL);
int i = 0;
if( ret )
{
LinkListNode* current = (LinkListNode*)sList;
for(i=0; (i<pos) && (current->next != NULL); i++)
{
current = current->next;
}
node->next = current->next;
current->next = node;
sList->length++;
}
return ret;
}
LinkListNode* LinkList_Get(LinkList* list, int pos) // O(n)
{
TLinkList* sList = (TLinkList*)list;
LinkListNode* ret = NULL;
int i = 0;
if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
{
LinkListNode* current = (LinkListNode*)sList;
for(i=0; i<pos; i++)
{
current = current->next;
}
ret = current->next;
}
return ret;
}
LinkListNode* LinkList_Delete(LinkList* list, int pos) // O(n)
{
TLinkList* sList = (TLinkList*)list;
LinkListNode* ret = NULL;
int i = 0;
if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
{
LinkListNode* current = (LinkListNode*)sList;
for(i=0; i<pos; i++)
{
current = current->next;
}
ret = current->next;
current->next = ret->next;
sList->length--;
}
return ret;
}
//LGraph.h
#ifndef _LGRAPH_H_
#define _LGRAPH_H_
typedef void LGraph;
typedef void LVertex;
typedef void (LGraph_Print)(LVertex*);
typedef void (LGraph_Print)(LVertex*);
LGraph* LGraph_Create(LVertex** v,int n);
void LGraph_Destroy(LGraph* graph);
void LGraph_Clear(LGraph* graph);
int LGraph_AddEdge(LGraph* graph,int v1,int v2,int w);
int LGraph_RemoveEdge(LGraph* graph,int v1,int v2);
int LGraph_GetEdge(LGraph* graph,int v1,int v2);
int LGraph_TD(LGraph* graph,int v);
int LGraph_VertexCount(LGraph* graph);
int LGraph_EdgeCount(LGraph* graph);
void LGraph_Display(LGraph* graph,LGraph_Print* pFunc);
#endif
//LGraph.c
#include "LGraph.h"
#include "LinkList.h"
#include <malloc.h>
#include <stdio.h>
typedef struct _tag_TLGraph
{
int count;
LVertex** v;
LinkList** la;
} TLGraph;
typedef struct _tag_ListNode
{
LinkListNode header;
int v;
int w;
}TListNode;
LGraph* LGraph_Create(LVertex** v,int n)
{
TLGraph* ret = NULL;
int ok = 1;
if((v!=NULL)&&(n>0))
{
ret = (TLGraph*)malloc(sizeof(TLGraph));
if(ret!=NULL)
{
ret->count = n;
ret->v = (LVertex**)calloc(n,sizeof(LVertex*));
ret->la = (LinkList**)calloc(n,sizeof(LinkList*));
ok = (ret->v!=NULL)&&(ret->la!=NULL);
if(ok)
{
int i = 0;
for(i=0;i<n;i++)
{
ret->v[i] = v[i];
}
for(i=0;(i<n)&&ok;i++)
{
ok = ok && ((ret->la[i] = LinkList_Create()) != NULL);
}
}
if(!ok) // 创建链表失败时
{
if(ret->la!=NULL)
{
int i = 0;
for(i=0;i<n;i++)
{
LinkList_Destroy(ret->la[i]);
}
}
free(ret->la);
free(ret->v);
free(ret);
ret = NULL;
}
}
}
return ret;
}
void LGraph_Destroy(LGraph* graph)
{
TLGraph* tGraph = (TLGraph*)graph;
LGraph_Clear(tGraph);
if(tGraph!=NULL)
{
int i = 0;
for(i=0;i<tGraph->count;i++)
{
LinkList_Destroy(tGraph->la[i]); //这个地方,我没想到
}
free(tGraph->la);
free(tGraph->v);
free(tGraph);
}
}
void LGraph_Clear(LGraph* graph)
{
TLGraph* tGraph = (TLGraph*)graph;
if(tGraph!=NULL)
{
int i = 0;
for(i=0;i<tGraph->count;i++)
{
while(LinkList_Length(tGraph->la[i]) > 0)
{
free(LinkList_Delete(tGraph->la[i],0)); //很独特偶
}
}
}
}
int LGraph_AddEdge(LGraph* graph,int v1,int v2,int w)
{
int ret = 0;
TLGraph* tGraph = (TLGraph*)graph;
TListNode* node = (TListNode*)malloc(sizeof(TListNode)); //这里不知道为什么要申请空间,不申请也能编译通过
//TListNode* node = NULL;
ret = (tGraph!=NULL) && (node!=NULL);
ret = ret && (v1>=0) && (v1<tGraph->count);
ret = ret && (v2>=0) && (v2<tGraph->count);
ret = ret && (w>0);
if( ret )
{
node->v = v2;
node->w = w;
LinkList_Insert(tGraph->la[v1],(LinkListNode*)node,0);
}
return ret;
}
int LGraph_RemoveEdge(LGraph* graph,int v1,int v2)
{
int ret = 0;
TLGraph* tGraph = (TLGraph*)graph;
if((tGraph!=NULL)&&(v1>=0)&&(v1<tGraph->count)&&(v2>=0)&&(v2<tGraph->count))
{
TListNode* node = NULL; //这里为什么不申请空间
//TListNode* node = (TListNode*)malloc(sizeof(TListNode));
int i=0;
for(i=0;i<LinkList_Length(tGraph->la[v1]);i++)
{
node = (TListNode*)LinkList_Get(tGraph->la[v1],i);
if(node->v == v2)
{
ret = node->w;
LinkList_Delete(tGraph->la[v1],i);
free(node);
break;
}
}
}
return ret;
}
int LGraph_GetEdge(LGraph* graph,int v1,int v2)
{
TLGraph* tGraph = (TLGraph*)graph;
int ret = 0;
if((tGraph!=NULL)&&(v1>=0)&&(v1<tGraph->count)&&(v2>=0)&&(tGraph->count))
{
int i = 0;
TListNode* node = NULL;
for(i=0;i<LinkList_Length(tGraph->la[v1]);i++)
{
//TListNode* node = (TListNode*)malloc(sizeof(TListNode));
node = (TListNode*)LinkList_Get(tGraph->la[v1],i);
if(node->v == v2)
{
ret = node->w;
break;
}
// free(node);
// node = NULL;
}
}
return ret;
}
int LGraph_TD(LGraph* graph,int v)
{
int ret = 0;
TLGraph* tGraph = (TLGraph*)graph;
if((tGraph!=NULL)&&(v>=0)&&(v<tGraph->count))
{
ret = LinkList_Length(tGraph->la[v]); //出度
int i = 0;
int j = 0;
TListNode* node = NULL;
for(i=0;i<tGraph->count;i++)
{
for(j=0;j<LinkList_Length(tGraph->la[i]);j++)
{
//TListNode* node = (TListNode*)malloc(sizeof(TListNode));
node = (TListNode*)LinkList_Get(tGraph->la[i],j);
if(node->v == v) //入度
{
ret++;
}
// free(node);
// node = NULL;
}
}
}
return ret;
}
int LGraph_VertexCount(LGraph* graph)
{
int ret = 0;
TLGraph* tGraph = (TLGraph*)graph;
if(tGraph!=NULL)
{
ret = tGraph->count;
}
return ret;
}
int LGraph_EdgeCount(LGraph* graph)
{
int ret = 0;
TLGraph* tGraph = (TLGraph*)graph;
if(tGraph!=NULL)
{
int i = 0;
for(i=0;i<tGraph->count;i++)
{
ret = ret + LinkList_Length(tGraph->la[i]);
}
}
return ret;
}
void LGraph_Display(LGraph* graph,LGraph_Print* pFunc)
{
TLGraph* tGraph = (TLGraph*)graph;
if((tGraph!=NULL)&&(pFunc!=NULL))
{
int i = 0;
int j = 0;
for(i=0;i<tGraph->count;i++)
{
printf("%d: ",i);
pFunc(tGraph->v[i]);
printf(";");
}
printf("\n");
TListNode* node = NULL;//不申请空间也是可以的
for(i=0;i<tGraph->count;i++)
{
for(j=0;j<LinkList_Length(tGraph->la[i]);j++)
{
//TListNode* node = NULL;//放在这里有点浪费空间
//TListNode* node = (TListNode*)malloc(sizeof(TListNode));
node = (TListNode*)LinkList_Get(tGraph->la[i],j);
printf("< ");
pFunc(tGraph->v[i]);
printf(",");
pFunc(tGraph->v[node->v]);
printf(",w:%d",node->w);
printf(">");
printf(" ");
// free(node);
// node = NULL;
}
}
printf("\n");
}
}
//main.c
/*----------------
* time:2016-4-14
-----------------*/
#include <stdio.h>
#include <stdlib.h>
#include "LGraph.h"
#include "stdio.h"
void print_data(LVertex* v)
{
printf("%s",(char*)v);
}
int main(int argc, char *argv[])
{
LVertex* v[] = {"A","B","C","D","E","F"};
LGraph* graph = (LGraph*)LGraph_Create(v,6);
LGraph_AddEdge(graph,0,1,2);
LGraph_AddEdge(graph,1,0,2);
LGraph_AddEdge(graph,1,2,4);
LGraph_AddEdge(graph,2,3,6);
printf("A->B:W = %d\n",LGraph_GetEdge(graph,0,1));
printf("A Degree:%d\n",LGraph_TD(graph,0));
printf("Vertex Count : %d\n",LGraph_VertexCount(graph));
printf("Edge Count : %d\n",LGraph_EdgeCount(graph));
LGraph_Display(graph,print_data);
LGraph_Destroy(graph);
return 0;
}
1