Algorithm-Data Structure-Multi-adjacent linked list representation of Graphs

Source: Internet
Author: User
Algorithm-Data Structure-Representation of multiple adjacent linked lists in the graph-Linux general technology-Linux programming and kernel information. For details, see the following. /*************************************** ******************
Title: graph-multiadjacentlist.c
Author:
Time:
Purpose: Representation of multiple adjacent linked lists of Graphs
Thread:
Comment:
Usage:
**************************************** ******************/
# Include "stdio. h"
# Include "stdlib. h"




/* ================================ Variable declaration -- variable declaration ================ ===== */
Struct edge/* graphic edge structure statement */
{
Int vertex1;/* vertex 1 Data */
Int vertex2;/* vertex 2 Data */
Struct edge * edge1;/* bottom line of vertex 1 */
Struct edge * edge2;/* bottom line of vertex 2 */
};
Typedef struct edge * nextedge;/* New State of the edge of the graph */

Struct node/* graphic vertex structure announcement */
{
Int vertex;/* vertex data */
Struct edge * edge;/* the bottom line of the vertex */
};
Typedef struct node * graph;/* New graph structure state */
Struct node head [6];/* array of graph vertex structures */



/* ============================== Function declaration -- function declaration ================== ===== */
Void creategraph (int * node, int num );



/* = = */
/*--------------------------------------------------
Function: void creategraph ()
Purpose:
Arguments:
Returns:
-------------------------------------------------*/
Void creategraph (int * node, int num)
{
Nextedge newnode;/* New Edge indicator */
Nextedge previous;/* front-side line indicator */
Nextedge ptr;/* Current edge indicator */
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 edge memory */
Newnode = (nextedge) malloc (sizeof (struct edge ));
Newnode-> vertex1 = from;/* Create vertex content */
Newnode-> vertex2 = to;/* Create vertex content */
Newnode-> edge1 = NULL;/* set the initial value of the indicator */
Newnode-> edge2 = NULL;/* set the initial value of the indicator */
Previous = NULL;/* frontend indicator */
Ptr = head [from]. edge;/* Current edge indicator */

While (ptr! = NULL) {/* traverse to the last edge */
Previous = ptr;/* retain the front line */
If (ptr-> vertex1 = from)/* edge line */
Ptr = ptr-> edge1;/* bottom line */
Else
Ptr = ptr-> edge2;/* bottom line */
}

If (previous = NULL)
Head [from]. edge = newnode;/* set the vertex edge indicator */
Else
If (previous-> vertex1 = from)/* determines the edge line */
Previous-> edge1 = newnode;/* connect to the bottom line */
Else
Previous-> edge2 = newnode;/* connect to the bottom line */
Previous = NULL;/* frontend indicator */
Ptr = head [to]. edge;/* Current edge indicator */

While (ptr! = NULL) {/* traverse to the last edge */
Previous = ptr;/* retain the front line */
If (ptr-> vertex1 = to)/* edge line */
Ptr = ptr-> edge1;/* bottom line */
Else
Ptr = ptr-> edge2;/* bottom line */
}

If (previous = NULL)
Head [to]. edge = newnode;/* set the vertex edge indicator */
Else
If (previous-> vertex1 = to)/* determines the edge line */
Previous-> edge1 = newnode;/* connect to the bottom line */
Else
Previous-> edge2 = newnode;/* connect to the bottom line */
}
}



/* =, Print the edge line ============================= */
Int main (int argc, char * argv [])
{
Nextedge ptr;
Int node [6] [2] = {1, 2},/* edge array */
{1, 3 },
{2, 3 },
{2, 4 },
{3, 5 },
{4, 5 },};
Int I;

For (I = 1; I <= 5; I ++ ){
Head . Vertex = I;/* set the vertex value */
Head. Edge = NULL;/* clear graphical indicators */
}

Creategraph (node, 6);/* Create a graph */
Printf ("graphic multi-adjacent linked list content: \ n ");

For (I = 1; I <= 5; I ++ ){
Printf ("vertex % d =>", head. Vertex);/* vertex value */
Ptr = head. Edge;
/* Edge position */
While (ptr! = NULL) {/* traverse to the end of the linked list */
/* Print the edge line */
Printf ("(% d, % d)", ptr-> vertex1, ptr-> vertex2 );
/* Determine the indicator of the next line */
If (head. Vertex = ptr-> vertex1)
Ptr = ptr-> edge1;/* next edge */
Else
Ptr = ptr-> edge2;/* next edge */
}

Printf ("\ n");/* line feed */
}

Return 1;
}

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.