鄰接表產生模型實現檔案C語言

來源:互聯網
上載者:User

/*new_adjacenty_list.c -- 鄰接表實現檔案*/<br />#include <stdio.h><br />#include <stdlib.h><br />#include "new_adjacenty_list.h"</p><p>/*局部函式宣告*/</p><p>static Adjoin_To_Vertex * Make_Adjoin_To_Vertex (const Hash_Table * const pht, const Name name, const int cvw) ;</p><p>/*介面函數定義*/</p><p>int Initialize_A (Adjacenty_List * const padj, const int capacity)<br />{<br />if (capacity <= 0)<br />return 0 ;<br />*padj = (struct adjacenty_list *) malloc (sizeof (struct adjacenty_list)) ;<br />if (NULL == *padj)<br />return 0 ;<br />(*padj) -> list = (Vertex *) malloc (sizeof (Vertex) * capacity) ;<br />if (NULL == (*padj) -> list)<br />{<br />free (*padj) ;<br />return 0 ;<br />}<br />(*padj) -> indegree = (int *) malloc (sizeof (int) * capacity) ;<br />if (NULL == (*padj) -> indegree)<br />{<br />free ((*padj) -> list) ;<br />free (*padj) ;<br />return 0 ;<br />}<br />(*padj) -> capacity = capacity ;</p><p>return 1 ;<br />}</p><p>int InitializeALine_A (Adjacenty_List * const padj, const Hash_Table * const pht, const int index, const Name name, const int indegree, const int sub, ...)<br />{<br />va_list ap ;<br />Adjoin_To_Vertex * temp ;<br />Name others ;<br />int i, cvw ;</p><p>if (index > (*padj) -> capacity || indegree < 0 || sub < 0)<br />return 0 ;<br />if (FAILED == Insert_H (pht, &name, index))<br />return 0 ;<br />va_start (ap, sub) ;<br />/*Suppose the function will not generate a iterant return value*/<br />(*padj) -> list[index].hash_value = Hash (pht, &name) ;<br />(*padj) -> list[index].known = FALSE ;<br />(*padj) -> list[index].dist = INFINITY ;<br />(*padj) -> list[index].path = NUL ;<br />(*padj) -> indegree[index] = indegree ;<br />if (0 == sub)<br />{<br />(*padj) -> list[index].adjoin_to = NULL ;<br />va_end (ap) ;<br />return 1 ;<br />}<br />else if (sub % 2)<br />{<br />(*padj) -> list[index].adjoin_to = NULL ;<br />va_end (ap) ;<br />return 0 ;<br />}<br />else<br />{<br />others = va_arg (ap, Name) ;<br />cvw = va_arg (ap, int) ;<br />temp = (*padj) -> list[index].adjoin_to = Make_Adjoin_To_Vertex (pht, others, cvw) ;<br />if (NULL == temp)<br />{<br />va_end (ap) ;<br />return 0 ;<br />}<br />for (i = 2; i < sub; i += 2)<br />{<br />others = va_arg (ap, Name) ;<br />cvw = va_arg (ap, int) ;<br />temp -> next = Make_Adjoin_To_Vertex (pht, others, cvw) ;<br />temp = temp -> next ;<br />}<br />va_end (ap) ;<br />return 1 ;<br />}<br />}</p><p>void PrintAdjacenty_List_A (const Adjacenty_List * const padj, const Hash_Table * const pht)<br />{<br />int i, capacity ;</p><p>for (i = 0, capacity = (*padj) -> capacity; i < capacity; i++)<br />printf ("Vertex Name:%-3c Dist:%-2d Path:%-3c/n", (*pht) -> lists[(*padj) -> list[i].hash_value].name, (*padj) -> list[i].dist, (*padj) -> list[i].path) ;<br />}</p><p>void Release_A (const Adjacenty_List * const padj)<br />{<br />Adjoin_To_Vertex * scan, * temp;<br />int i, capacity ;</p><p>for (i = 0, capacity = (*padj) -> capacity; i < capacity; i++)<br />{<br />scan = (*padj) -> list[i].adjoin_to ;<br />while (scan)<br />{<br />temp = scan ;<br />scan = scan -> next ;<br />free (temp) ;<br />}<br />}<br />free ((*padj) -> list) ;<br />free ((*padj) -> indegree) ;<br />free (*padj) ;<br />}</p><p>/*局部函數定義*/</p><p>static Adjoin_To_Vertex * Make_Adjoin_To_Vertex (const Hash_Table * const pht, const Name name, const int cvw)<br />{<br />Adjoin_To_Vertex * new_vertex ;</p><p>new_vertex = (Adjoin_To_Vertex *) malloc (sizeof (Adjoin_To_Vertex)) ;<br />if (new_vertex)<br />{<br />/*Suppose the function will not generate a iterant return value*/<br />new_vertex -> hash_value = Hash (pht, &name) ;<br />new_vertex -> cvw = cvw ;<br />new_vertex -> next = NULL ;<br />}</p><p>return new_vertex ;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.