Two Adjacent table implementations (linked list and array simulation)

Source: Internet
Author: User

Linked List Implementation:

Struct Node
{
Int V; // The ending vertex of an edge.
Int W; // edge Length
Node * Next; // pointer to the next edge at the same starting point
} * First [N]; // first [u] points to the first edge starting from U.
Void Init ()
{
Memset (first, null, sizeof (first ));
}
Void add (int u, int V, int W) // Add an edge
{
Node * P = new node;
P-> V = V;
P-> W = W;
P-> next = Fisrt;
First [u] = P;
}
// When used, find the adjacent contact of u
For (node * P = first [u]; P! = NULL; P = p-> next)
{
// Handle the problem here
}

Array simulation:

 

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)
{
...
}

Array Implementation of an Application: http://poj.org/problem? Id = 3321

Problem report: http://acm.sdut.edu.cn/bbs/read.php? Tid = 3170 & page = 1 #4303.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.