Sequential storage of pictures Hunan University

Source: Internet
Author: User

The classmate entrusted me to write the homework, very simple diagram realizes the question, uses the two-dimensional array.

Interface (header file):

typedef int DATATYPE;

typedef struct GRAPHCDT* Graph;

Graph newgraph ();

void AddNode (Graph graph,datatype data);

void Addedge (Graph graph,datatype src,datatype DST);

Long time no engineering documents, a long time did not write C code. But by writing this assignment, I figured out 2 questions.

1.sizeof can not calculate the J structure in C language, such as sizeof (GRAPHCDT). But C + + can.

2. The pointer defines the time to allocate space, otherwise the memory can not be written.

Also know a new idea of building a map: a one-dimensional array storage node, two-dimensional array storage edge. Because if the input node is not 1, 2, 3, 4 or a, B, C, D, it is not convenient to construct the data of the two-dimensional array to record the nodes and edges of the graph.

The following gives the C + + code to solve this problem, if there is a mistake, welcome treatise.

1#include <stdio.h>2#include <stdlib.h>3 #defineMaxSize 1004typedefintDataType;5typedefstructgraphcdt*Graph;6 structGRAPHCDT7 {8     intEdg[maxsize][maxsize];9 DataType ver[maxsize];Ten     intN; One }; A  - Graph newgraph () - { the graph graph; -Graph= (Graph)malloc(sizeof(structgraphcdt)); -     inti,j; -      for(i=0; i<maxsize;i++) +graph->ver[i]=0; -      for(i=0; i<maxsize;i++) +              for(j=0; j<maxsize;j++) Agraph->edg[i][j]=0; atgraph->n=0; -     returngraph; - } -  - voidAddNode (Graph graph,datatype data) - { ingraph->ver[data]=1; -graph->n++; toprintf"%n---d \ n",graph->n); + } - voidAddedge (Graph graph,datatype src,datatype DST) the { *graph->edg[src][dst]=1; $ }Panax Notoginseng voidPrint (graph graph) - { the     inti,j; +printf"junction: \ n"); A      for(i=0; i<maxsize;i++) the         if(graph->ver[i]!=0) +printf"%d", i); -printf"\ n"); $printf"side: \ n"); $      for(i=0; i<maxsize;i++) -              for(j=0; j<maxsize;j++) -                 if(graph->edg[i][j]!=0) theprintf"%d---%d", i,j); -printf"\ n");Wuyi } the intMain () - { Wu graph graph; -Graph= (Graph)malloc(sizeof(structgraphcdt)); About     intflag=1; $     intDATA,SRC,DST; -Graph=newgraph (); -      while(1) -     { Aprintf"Enter the new node you want to join (0-99): \ n"); +scanf"%d",&data); the AddNode (graph,data); -printf"Enter the source node and destination node (0-99) for the edge you want to join: \ n"); $scanf"%d%d",&src,&DST); the Addedge (GRAPH,SRC,DST); the print (graph); theprintf"Enter 0 To continue, then enter 1\n at the end"); thescanf"%d",&flag); -         if(flag) in              Break; the     } the     return 0; About}
View Code

"Blogging is more about words and expressions than writing a diary. 】

Sequential storage of pictures Hunan University

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.