Algorithm-Data Structure-Representation of the graph's adjacent linked list
Source: Internet
Author: User
Algorithm-Data Structure-graph's adjacent linked list representation-Linux general technology-Linux programming and kernel information. For more information, see the following. /*************************************** ******************
Title: graph-adjacentlist.c
Author:
Time:
Purpose: the representation of the graph's adjacent linked list
Thread:
Comment:
Usage:
**************************************** ******************/
# Include "stdio. h"
# Include "stdlib. h"
/* ================================ Variable declaration -- variable declaration ================ ===== */
Typedef struct _ graphnode {/* graphic vertex structure announcement */
Int vertex;/* vertex data */
Struct _ graphnode * nextnode;/* indicates the indicator of the next vertex */
} Graphnode;
Typedef struct _ graph {/* structure new state of the graph */
Struct _ graphnode * firstnode;
} Graph;
Struct _ graphnode head [6];/* graph vertex structure array */
/* ============================== Function declaration -- function declaration ================== ===== */
Void creategraph (int * node, int num );
/* = = */
/*--------------------------------------------------
Function: creategraph ()
Purpose: Create a graph
Arguments:
Returns:
-------------------------------------------------*/
Void creategraph (int * node, int num)
{
Graphnode * newnode;/* new vertex indicator */
Graph ptr;
Int from;/* the starting point of the edge */
Int to;/* Edge End */
Int I;
For (I = 0; I <num; I ++) {/* read the circuit of the edge */
From = node [I * 2];/* edge start point */
To = node [I * 2 + 1];/* Edge End */
/* Create a new vertex memory */
Newnode = (graphnode *) malloc (sizeof (struct _ graphnode ));
Newnode-> vertex = to;/* Create vertex content */
Newnode-> nextnode = NULL;/* set the initial value of the indicator */
Ptr. firstnode = & (head [from]);/* vertex position */
While (ptr. firstnode-> nextnode! = NULL)/* traverse to the end of the linked list */
Ptr. firstnode = ptr. firstnode-> nextnode;/* Next vertex */
Ptr. firstnode-> nextnode = newnode;/* Insert end */
}
}
/* ========= Main function: after creating a graph, print the adjacent linked list ============= */
Int main (int argc, char * argv [])
{
Graph ptr;
Int node [12] [2] = {1, 2}, {2, 1},/* edge array */
{1, 3}, {3, 1 },
{2, 3}, {3, 2 },
{2, 4}, {4, 2 },
{3, 5}, {5, 3 },
{4, 5 },{ 5, 4 }};
Int I;
For (I = 1; I <= 5; I ++ ){
Head . Vertex = I;/* set the vertex value */
Head. Nextnode = NULL;/* clear graphical indicators */
}
Creategraph (node, 12);/* Create a graph */
Printf ("graphic adjacent linked list content: \ n ");
For (I = 1; I <= 5; I ++ ){
Printf ("vertex % d =>", head. Vertex);/* vertex value */
Ptr. firstnode = head. Nextnode;/* vertex position */
While (ptr. firstnode! = NULL) {/* traverse to the end of the linked list */
Printf ("% d", ptr. firstnode-> vertex);/* print vertex content */
Ptr. firstnode = ptr. firstnode-> nextnode;/* Next vertex */
}
Printf ("\ n");/* line feed */
}
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