1 Project introduction
This project is a simple simulation of bus route information to complete the functions of building bus route information, modifying bus route information and deleting bus route information.
2 Design Ideas
The essence of this project is to complete the functions of building, finding, inserting, modifying and deleting bus route information. First, you can define the data structure of the project, then write each function as a function to complete the operation of the data, and finally complete the main function to verify each function and get the result of operation.
3 Data structure
The relationship between bus stops can be arbitrary, and any two stops can be related.In a graph structure, the relationship between nodes can be arbitrary, and any two data elements in the graph can be related.So we can use a graphical structure to represent the possible bus routes between n bus stops and between the stops, where the vertices of the network represent the bus stops, the edges represent the routes between the two stops, and the weights given to the edges represent the corresponding distances.
Since bus routes are continuous, if you want to output bus routes from a starting point to an ending point, you need to find the first and next adjacent points from a vertex.Because it is easy to find the first and next adjacent points of any vertex in the adjacency table, this project uses the adjacency table storage structure of the graph.Adjacency table is a chain storage structure of graph.In the adjacency table, a single-chain list is established for each vertex of the graph, and the nodes in the first single-chain table represent the edges that are attached to vertex vi (arcs that end with vertex vi for directed graphs).Each node consists of three domains, where the adjacent point domain (adjvex) indicates the location of the point adjacent to vertex vi in the graph, the chain domain (nextarc) indicates the node of the next edge or arc, and the data domain (info) stores information related to the edge or arc, such as weights.A header node is attached to each list. In addition to the first arc pointing to the first node in the list, there is also a data field (data) that stores the name of the vertex vi or other related information.These header nodes are usually stored in a sequential structure so that the chain list of any vertex can be accessed randomly.The program code is as follows:
[Complete Code]
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <malloc.h> #define OK 1 #define ERROR 0 #define MAX_VERTEX_NUM 20 typedef int Status; typedef char VerTexType; typedef struct bus { int num; //License number char time[30]; //Departure time char start[30]; //Starting Station char end[30]; //Terminus int score; //Number of stations struct bus *next; }Node,*Linklist; typedef bus ElemType; typedef struct LNode { ElemType data; // Data Domain struct LNode *next; //Pointer field }LNode,*LinkList; //Construct adjacency table typedef struct ArcNode //Structure of arcs { int adjvex; //The position of the vertex to which the arc points struct ArcNode *nextarc; //A pointer to the next arc int *info; //Pointer to information about the arc }ArcNode; typedef struct VNode //Vertex structure { VerTexType data; //Vertex information ArcNode *firstarc; //A pointer to the first arc that depends on that vertex }VNode,AdjList[MAX_VERTEX_NUM]; typedef struct //Construction of Graphs { AdjList vertices; int vexnum,arcnum; //Current number of vertices and arcs of a graph int kind; //Category flags for diagrams }ALGraph; Status LocateVex(ALGraph G,int v)//Find the location of vertices { int i; for(i=0;i<G.vexnum;i++) { if(i==G.vertices[i].data) return i; } } Linklist CreateUDG(ALGraph G) { ArcNode *p,*q; int i,j,k,v1,v2; printf("\n Please enter the total number of locations and routes:"); scanf("%d %d",&G.vexnum,&G.arcnum); printf("Please enter the name of each place:"); for(i=0;i<G.vexnum;i++) { scanf("%d",&G.vertices[i].data); G.vertices[i].firstarc=NULL; //Initialization } printf("Please enter the starting and ending stations;\n"); for(k=0;k<G.arcnum;k++) { scanf("%d %d",&v1,&v2); i=LocateVex(G,v1);j=LocateVex(G,v2); //Location p=(ArcNode*)malloc(sizeof(ArcNode)); //Request a Node p->adjvex=j;p->nextarc=NULL; //assignment p->nextarc=G.vertices[i].firstarc; //Connecting Nodes G.vertices[i].firstarc=p; //Connecting Nodes q=(ArcNode*)malloc(sizeof(ArcNode)); q->adjvex=i;q->nextarc=NULL; q->nextarc=G.vertices[j].firstarc; G.vertices[j].firstarc=q; } } Linklist CreateList(Linklist h) { Node *p; int n,i,s; char sch1[30],sch2[30],sch3[30]; p=(Linklist)malloc(sizeof(Node)); scanf("%d",&n); //License number scanf("%s%s%s",sch1,sch2,sch3); //Time, departure, end scanf("%d",&s); //Number of stations strcpy(p->time,sch1); strcpy(p->start,sch2); strcpy(p->end,sch3); p->num=n; p->score=s; p->next=h; h=p; return h; } Linklist GetElem(Linklist L,int i)//Find by value { while(L!=NULL) { if(L->num==i) break; L=L->next; } if(L!=NULL) return L; else return NULL; } Status InsertList(LinkList L,int i,ElemType e) // Insert information about a bus route at position i { LinkList p,s; p=L; int j=0; while(p&&j<i-1) { p=p->next; ++j; } if(!p||j>i-1) return ERROR; s=(struct LNode*)malloc(sizeof(LNode)); //Generating new nodes*s s->data=e; //Set the data field of node*s to e s->next=p->next; //Pointing the pointer field of node*e to node ai p->next=s; //Pointing the pointer field of node *p to node *s return OK; } Status PutPoint(Linklist L) //Inquired Number { if(L!=NULL) printf("%d",L->num); printf("-->%s",L->time); printf("-->%s",L->start); printf("-->%s",L->end); printf("-->%d",L->score); } void Revise(Linklist L,int i)//Modify bus information { int choose1,y,a,b; char sch[30]; Node *p=GetElem(L,i); printf("%d Bus No. 1 has the following information:\n",i); PutPoint(p); printf("\n Please enter what you want to modify:\n"); printf("1,License number\n"); printf("2,Departure time\n"); printf("3,Starting Station\n"); printf("4,Terminus\n"); printf("5,Number of stations\n"); scanf("%d",&choose1); while(choose1) { if(choose1==1) { printf("Please enter a new number\n"); scanf("%d",&y); p->num=y; printf("Information modification complete\n"); break; } if(choose1==2) { printf("Please enter a new time\n"); scanf("%s",sch); strcpy(p->time,sch); printf("Information modification complete\n"); break; } if(choose1==3) { printf("Please enter a new starting point\n"); scanf("%s",sch); strcpy(p->start,sch); printf("Information modification complete\n"); break; } if(choose1==4) { printf("Please enter a new terminal\n"); scanf("%s",sch); strcpy(p->end,sch); printf("Information modification complete\n"); break; } if(choose1==5) { printf("Please enter a new number of stations\n"); scanf("%d",&y); p->score=y; printf("Information modification complete\n"); break; } } } Linklist DeleteList(Linklist L,int n) //Delete bus number n { Linklist p,p1,L1,L2; p=p1=L1=L2=L; int k=0,x=0; while(p1!=NULL) { k++; p1=p1->next; } if(k==1) { p1=NULL; return p1; } else if(L->num==n) { L=L->next; return L; } while(L->next->num!=n) { x++; L=L->next; } if(x==k-1) { L->next=NULL; return p; } else { Node *q; while(L1->next->num!=n) L1=L1->next; q=L1->next; L1->next=q->next; free(q); return L2; } } Status Putlist(Linklist L) //Output Chain Table Content { if(L==NULL) { printf("No bus route!!\n"); return 0; } while(L) { printf("%d",L->num); printf("-->%s",L->time); printf("-->%s",L->start); printf("-->%s",L->end); printf("-->%d",L->score); printf("\n"); L=L->next; } } //User Interface int main(void) { int x,y,k; Linklist L1=NULL; Node *p; ALGraph G; CreateUDG(G); printf("\n******Welcome to Bus Information System******\n"); printf("******* 1,message creation*******\n"); printf("******* 2,Query Information*******\n"); printf("******* 3,Modify Information*******\n"); printf("******* 4,Delete Information*******\n"); printf("******* 5,display information*******\n"); printf("******* 0,Sign out*******\n"); int n,choose2=-1; while(choose2!=0) { printf("\n Enter the serial number before the feature you want to select:"); scanf("%d",&choose2); if(choose2==0) break; else if (choose2==1) { printf("\n Please enter the information you want to add as follows:->Departure time->Starting Station->Terminus->Sequential input of station number\n"); //for(k=0;k<G.arcnum;k++) L1=CreateList(L1); printf("Add Success!"); } else if(choose2==2) { printf("\n Please enter the number you want to query:"); scanf("%d",&y); p=GetElem(L1,y); PutPoint(p); } else if(choose2==3) { printf("\n Enter the number of the car with which you want to modify the information:"); scanf("%d",&y); Revise(L1,y); } else if(choose2==4) { printf("\n Please enter the number you want to delete:"); scanf("%d",&y); L1=DeleteList(L1,y); printf("Delete successful!\n"); } else if(choose2==5) { printf("The information for the bus route is:\n"); Putlist(L1); } } return 0; }
The program is not so concise, what need to guide me, welcome to harass oh!
Finally, I wish you all a Happy New Year!Happy and healthy!