인접표 1 (C 언어) (그림) (icoding)

38692 단어
//                   insert_vertex   insert_arc,      :

typedef int VertexType;

typedef enum{
    DG, UDG
}GraphType;

typedef struct ArcNode
{
    int adjvex;
    InfoPtr *info;
    struct ArcNode *nextarc;

}ArcNode;

typedef struct VNode
{
    VertexType data;
    ArcNode *firstarc;
}VNode;
typedef struct
{
    VNode vertex[MAX_VERTEX_NUM];
    int vexnum, arcnum;
    GraphType type;
}ListGraph;

int locate_vertex(ListGraph* G, VertexType v); //     v  vertex      ,  v   ,  -1
bool insert_vertex(ListGraph *G, VertexType v);
bool insert_arc(ListGraph *G, VertexType v, VertexType w);

//          ,    true,  (        、      v w   )  false。

답안
bool insert_vertex(ListGraph* G, VertexType v) {
	if (G->vexnum == MAX_VERTEX_NUM)//              
		return false;
	for (int i = 0;i<G->vexnum;i++) {
		if (G->vertex[i].data == v)//  v          
			return false;
	}
	G->vertex[G->vexnum].data = v;//    
	G->vertex[G->vexnum].firstarc = NULL;
	G->vexnum++;
	return true;
}
bool insert_arc(ListGraph* G, VertexType v, VertexType w) {//  v   ,w   
	int locv = locate_vertex(G, v), locw = locate_vertex(G, w);
	if (locv == -1 || locw == -1)//  v w     
		return false;
	ArcNode* p=G->vertex[locv].firstarc, * p_pre = NULL;
	if (p == NULL) {
		G->vertex[locv].firstarc = (ArcNode*)malloc(sizeof(ArcNode));
		G->vertex[locv].firstarc->adjvex = w;
		G->vertex[locv].firstarc->nextarc = NULL;
	}
	else {
		while (p != NULL) {
			if (p->adjvex == w)
				return false;//      ,           ,      false    (      
			else {
				p_pre = p;
				p = p->nextarc;
			}
		}
		p_pre->nextarc= (ArcNode*)malloc(sizeof(ArcNode));//        ,                 “  ”
		p_pre->nextarc->adjvex = w;
		p_pre->nextarc->nextarc = NULL;
	}

	if (G->type==DG) {
		++G->arcnum;
		return true;
	}
	else {
	     p = G->vertex[locw].firstarc,  p_pre = NULL;	
		if (p == NULL) {
			G->vertex[locw].firstarc = (ArcNode*)malloc(sizeof(ArcNode));
			G->vertex[locw].firstarc->adjvex = v;
			G->vertex[locw].firstarc->nextarc = NULL;
		}
		else {
			while (p != NULL) {
			if (p->adjvex == v)
				return false;
			else {
				p_pre = p;
				p = p->nextarc;
			}
		}
			p_pre->nextarc = (ArcNode*)malloc(sizeof(ArcNode));
			p_pre->nextarc->adjvex = v;
			p_pre->nextarc->nextarc = NULL;
		}
		G->arcnum += 2;
		return true;
	}
}

거두다
  • 디버깅을 위한 전체 코드
  • #include 
    #include 
    #include 
    #include 
    #define MAX_VERTEX_NUM 100
    
    typedef int VertexType;
    typedef enum {
    DG, UDG
    }GraphType;
    typedef struct ArcNode
    {
    int adjvex;
    //InfoPtr* info;
    struct ArcNode* nextarc;
    }ArcNode;
    typedef struct VNode {
    VertexType data;
    ArcNode* firstarc;//              
    }VNode;
    typedef struct {
    VNode vertex[MAX_VERTEX_NUM];
    int vexnum, arcnum;
    GraphType type;
    }ListGraph;
    
    int locate_vertex(ListGraph* G, VertexType v) {
    for (int i = 0;i < G->vexnum;i++) {
    if (G->vertex[i].data == v)
    return i;
    }
    return false;
    }
    bool insert_vertex(ListGraph* G, VertexType v) {
    if (G->vexnum == MAX_VERTEX_NUM)
    return false;
    for (int i = 0;i<G->vexnum;i++) {
    if (G->vertex[i].data == v)
    return false;
    }
    G->vertex[G->vexnum].data = v;
    G->vertex[G->vexnum].firstarc = NULL;
    G->vexnum++;
    return true;
    }
    bool insert_arc(ListGraph* G, VertexType v, VertexType w) {//  v   ,w   
    int locv = locate_vertex(G, v), locw = locate_vertex(G, w);
    if (locv == -1 || locw == -1)
    return false;
    ArcNode* p=G->vertex[locv].firstarc, * p_pre = NULL;
    if (p == NULL) {
    G->vertex[locv].firstarc = (ArcNode*)malloc(sizeof(ArcNode));
    G->vertex[locv].firstarc->adjvex = w;
    G->vertex[locv].firstarc->nextarc = NULL;
    }
    else {
    while (p != NULL) {
    if (p->adjvex == w)
    return false;
    else {
    p_pre = p;
    p = p->nextarc;
    }
    }
    p_pre->nextarc= (ArcNode*)malloc(sizeof(ArcNode));
    p_pre->nextarc->adjvex = w;
    p_pre->nextarc->nextarc = NULL;
    }
    
    if (G->type==DG) {
    ++G->arcnum;
    return true;
    }
    else {
         p = G->vertex[locw].firstarc,  p_pre = NULL;
    if (p == NULL) {
    G->vertex[locw].firstarc = (ArcNode*)malloc(sizeof(ArcNode));
    G->vertex[locw].firstarc->adjvex = v;
    G->vertex[locw].firstarc->nextarc = NULL;
    }
    else {
    while (p != NULL) {
    if (p->adjvex == v)
    return false;//
    else {
    p_pre = p;
    p = p->nextarc;
    }
    }
    p_pre->nextarc = (ArcNode*)malloc(sizeof(ArcNode));
    p_pre->nextarc->adjvex = v;
    p_pre->nextarc->nextarc = NULL;
    }
    G->arcnum += 2;
    return true;
    }
    }
    
    int main(void) {
    freopen("in.txt", "r", stdin);
    int vexnum = 0, arcnum = 0, i = 0,del=0;
    ListGraph* G;
    G = (ListGraph*)malloc(sizeof(ListGraph));
    memset(G, 0, sizeof(ListGraph));
    scanf("%d %d %d", &vexnum, &arcnum, &G->type);//     ,  ,    
    for (i = 0;i < vexnum;i++) {
    int v;
    scanf("%d", &v);
    insert_vertex(G, v);
    }
    for (i = 0;i < arcnum;i++) {
    int v, w;
    scanf("%d %d", &v, &w);
    insert_arc(G, v, w);
    }
    scanf("%d", &del);//        
    del_vertex(G, del);
    return 0;
    }
    
  • malloc 함수의 사용에 주의하여malloc 함수로 메모리 공간을 분배한 후 하나의 결점을 추가하여'가변'을 실현한다
  • 좋은 웹페이지 즐겨찾기