_datastructure_c_impl: Finding a simple path from vertex u to vertex v in Figure g

Source: Internet
Author: User

#pragma once#include<stdio.h> #include <stdlib.h> #define StackSize 100typedef int datatype;// stack element type definition typedef struct{datatype STACK[STACKSIZE];INT top;} seqstack;//the stack is initialized to empty stack only need to put the top pointer to void Initstack (Seqstack *s) {s->top=0;//The stack top pointer to 0}//to determine whether the stack is empty, the stack is empty return 1, otherwise return 0int Stackempty (Seqstack S) {if (s.top==0) return 1;elsereturn 0;} Take the top element of the stack. Returns the value of the top element of the stack to E and returns 1 for success; otherwise, 0 indicates failure. int GetTop (Seqstack s,datatype *e) {if (s.top<=0) {///before fetching the top element of the stack, determine if the stack is empty printf ("Stack is empty!\n"); return 0;} else{*e=s.stack[s.top-1];//the top element of the stack return 1;}} Put the element e into the stack, the element into the stack successfully returned 1, otherwise return 0int Pushstack (Seqstack *s,datatype e) {if (s->top>=stacksize) {//before the element into the stack, determine whether the stack is full of printf ("Stack is full, can't go into the stack!") \ n "); return 0;} else{s->stack[s->top]=e;//element e into the stack s->top++;//modify the stack top pointer return 1;}} The stack operation. Stack the top element of the stack and assign it to E. The stack successfully returns 1, otherwise returns 0int Popstack (Seqstack *s,datatype *e) {if (s->top<=0) {//element before the stack, determine whether the stack is empty printf ("stack has no elements, cannot be out of the stack!\n"); return 0;} else{s->top--;//first modify the stack top pointer, that is, the stack *e=s->stack[s->top];//The stack element is assigned to Ereturn 1;}} Calculate the length of the stack, that is, the number of elements in the stack, the value of the top pointer is equal to the number of elements in the stack int stacklength (Seqstack S) {RETurn s.top;} Empty stack operation void Clearstack (Seqstack *s) {s->top=0;}
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "SeqStack.h" typedef char vertextype[4];typedef Char infoptr;typedef int vrtype, #define MaxSize 50//maximum vertex number typedef enum{dg,dn,ug,un}graphkind;// Types of graphs: directed, directed, non-directed, and non-directed/edge-node types define the position of the vertex that the typedef struct ARCNODE{INT adjvex;//arc points to infoptr *info;//Arc's weight struct Arcnode * nextarc;//indicates the type definition of the next vertex}arcnode;//header node adjacent to the vertex the typedef struct VNODE{VERTEXTYPE data;//is used to store vertices arcnode *firstarc;// Indicates the type definition of the first vertex adjacent to the vertex}vnode,adjlist[maxsize];//graph typedef struct{adjlist vertex;//head node int vexnum,arcnum;// The number of vertices of the graph and the number of arcs Graphkind the type of kind;//graph}adjgraph;//A simple path from vertex u to vertex v in Figure G, void Briefpath (adjgraph g,int u,int v) {int k,i; Seqstack S; Arcnode *p;int Visited[maxsize];int parent[maxsize];//stores the predecessor Vertex Initstack (&s) of the vertex that has been accessed; for (k=0;k<g.vexnum;k++) The visited[k]=0;//access flag initializes the Pushstack (&s,u);//start vertex into the stack visited[u]=1;//access flag is set to 1while (! Stackempty (S)) {//breadth-first traversal graph, access path with parent storage popstack (&s,&k);p =g.vertex[k].firstarc;while (p!=null) {if (p-> ADJVEX==V) {//If vertex vparent[p->adjv is foundThe predecessor vertex sequence of the ex]=k;//Vertex v is kprintf (the path to vertex%s to vertex%s is: ", g.vertex[u].data,g.vertex[v].data); i=v;do{// Starting from Vertex V, the vertices in the path are sequentially into the stack pushstack (&s,i); i=parent[i];} while (I!=U); Pushstack (&s,u); while (! Stackempty (S)) {//the vertex popstack (&s,&i);p rintf ("%s", g.vertex[i].data) that outputs the path from U to V from vertex u;} printf ("\ n");} else if (visited[p->adjvex]==0) {//If the vertex v is not found and the adjacency point has not been accessed, continue looking for visited[p->adjvex]=1;parent[p->adjvex]=k; Pushstack (&s,p->adjvex);} P=p->nextarc;}}} Returns the position of the vertex in the graph int Locatevertex (adjgraph G,vertextype v) {int i;for (i=0;i<g.vexnum;i++) if (strcmp (G.vertex[i].data), V) ==0) return i;return-1;} The adjacency table storage structure is used to create the nvoid graph Creategraph (adjgraph *g) {int i,j,k,w; Vertextype v1,v2;/* defines two vertices v1 and v2*/arcnode *p;printf ("Enter the number of vertices of the graph, the number of sides (separated by commas):"), scanf ("%d,%d",& (*g) .vexnum,& (*g ). Arcnum);p rintf ("Enter values for%d vertices:", g->vexnum), for (i=0;i<g->vexnum;i++)/* To store vertices in the header node */{scanf ("%s",g-> Vertex[i].data); g->vertex[i].firstarc=null;/* The associated vertex to an empty */}printf ("Please enter two vertices of the edge (separated by a space): \ n"); for (k=0;k<g->arcnum;k++)/* Set up edge linked list */{scanf ("%s%s ", V1,v2), I=locatevertex (*G,V1), J=locatevertex (*G,V2),/*j for edge I create adjacency table for Out Edge */p= (arcnode*) malloc (sizeof (Arcnode));p- >adjvex=j;p->info= (infoptr*) malloc (sizeof (INFOPTR));/* Inserts the node p points into the side table */p->nextarc=g->vertex[i]. Firstarc; G->vertex[i].firstarc=p;/*i create adjacency table for Edge J for Out side */p= (arcnode*) malloc (sizeof (Arcnode));p->adjvex=i;p->info=null ;p->nextarc=g->vertex[j].firstarc; G->vertex[j].firstarc=p;} (*g). Kind=ug;} Destroy the Gvoid destroygraph (adjgraph *g) {int i; Arcnode *p,*q;for (i=0;i<g->vexnum;++i)/* Release Benzi node in diagram */{p=g->vertex[i].firstarc;/*p point to the first node of the edge table */if (P!=NULL) /* If the edge table is not empty, release the node of the Edge table */{q=p->nextarc;free (p);p =q;}} (*g). vexnum=0;/* the vertex count to 0*/(*g). arcnum=0;/* the number of edges is set to the output void 0*/}//(Displaygraph g) of the adjacency table of the adjgraph graph G {int i; Arcnode *p;printf ("There are%d vertices in the graph:", g.vexnum), for (i=0;i<g.vexnum;i++) printf ("%s", G.vertex[i].data);p rintf ("\ n %d edges: \ n ", 2*g.arcnum), for (i=0;i<g.vexnum;i++) {P=g.vertex[i].firstarc;while (p) {printf (" (%s,%s) ", G.vertex[i]. Data,g.vertex[p->adjvex].data);p =p->nextarc;} PrintF ("\ n");}} void Main () {adjgraph G; Creategraph (&AMP;G);/* The adjacency table storage structure is used to create the diagram G*/displaygraph (G);/* Output No Direction graph G*/briefpath (g,0,4);/* Find a simple path from vertex A to vertex e in Figure G */ Destroygraph (&AMP;G);/* Destroy Diagram G*/system ("pause");}



Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint |copyright©2011-2015,supernatural, all rights Reserved.

_datastructure_c_impl: Finding a simple path from vertex u to vertex v in Figure g

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.