邻接表的两种实现(链表和数组模拟)
生活随笔
收集整理的這篇文章主要介紹了
邻接表的两种实现(链表和数组模拟)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
struct node
{int v; //邊的結束頂點
int w; //邊的長度node* next; //指向以同一起點的下一條邊的指針
}*first[N]; //first[u]指向以u為起始點的第一條邊
void init()
{memset(first,NULL,sizeof(first));
}
void add(int u, int v, int w)//添加邊
{node* p =new node;p->v = v;p->w = w;p->next = fisrt;first[u] = p;
}
//使用的時候,找u的鄰接點
for(node* p = first[u]; p != NULL; p = p->next)
{
//在這里作相應的處理
}
數組模擬:
struct node {int u, v, w;int next; }graph[1000];int head[1000], t;void init() {t = 1;memset(head, 0, sizeof(memset)); }void add(int u, int v, int w) {graph[t].u = u;graph[t].v = v;graph[t].w = w;graph[t].next = head[u];head[u] = t;t++; }for(i = head[u]; i; i = graph[i].next) {... }?
?
轉載于:https://www.cnblogs.com/timeship/archive/2012/08/03/2622314.html
總結
以上是生活随笔為你收集整理的邻接表的两种实现(链表和数组模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小波同态滤波 matlab,简单的同态滤
- 下一篇: 计算机网络渗透技术(信安一班 李静)