Bus Line Management

1 Project Description
This project is a simple simulation of the bus line information to complete the establishment of bus route information, modify and delete information bus route information on bus routes and other functions.
2 design ideas
essence of this project is to establish a complete information on bus routes, search, insert, modify, delete and other functions, can first define the data structure of the project, and then written as a function of each function to complete the operation on the data, and finally complete the main function to verify the respective function-run and the results obtained.
3 data structure
relationship between the bus station can be any, are likely correlation between any two sites. In the graph structure, the relationship between the nodes may be arbitrary, may FIG correlation between any two data elements. Can be represented by a graph structure, and between the bus lines may be provided between the n sites of bus stops, the bus vertices represent network site, while showing a route between two sites, given edge weights represent respective distances .
Bus routes because there is a certain relationship of continuous, if you want to start output from a certain starting point to the end of the end of a bus route, you need to find the first starting from a point adjacent to the apex and the next neighbor. Because it is easy to find any point adjacent the first apex and a point adjacent the next adjacent table, the adjacency list of the item using the storage structure of FIG. Storing adjacency list is a chain structure of FIG. In the adjacent table, for each vertex of the establishment of a single list of FIG., The i-th node in the linked list represents a single vertex vi is attached to the edge (directed graph of vertices vi is the tail of the arc). Each node consists of three domains, wherein a lower edge or an arc (adjvex) indicating the location of a point adjacent to the vertex vi is adjacent points in FIG domain, chain domain (nextarc) indicated by node; data field (info) and storing information related to edges or arcs, such as the weight and the like. Each node attached to a header, the header node, in addition to pointing to the first node with the list chain domain (firstarc), there is also provided a storage vertices vi name or other information on the list data field (data). These tables are usually in the form of the first node structure storage order for random access to any vertex list. 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; //车号
	char time[30];	//发车时间
	char start[30];	//起始站
	char end[30];	//终点站
	int score;	//站数
	struct bus *next; 
 }Node,*Linklist;

typedef bus ElemType;
 
typedef struct LNode  
{  
    ElemType data; // 数据域  
    struct LNode *next; //指针域   
}LNode,*LinkList;  

//构造邻接表 
typedef struct ArcNode	//弧的结构 
{
	int adjvex;	//该弧所指向的顶点的位置
	struct ArcNode *nextarc;	//指向下一条弧的指针
	int *info;	//该弧相关信息的指针 
 }ArcNode;
 
typedef struct VNode	//顶点结构 
{
 	VerTexType data;	//顶点信息
	ArcNode *firstarc;	//指向第一条依附该顶点的弧的指针 
 }VNode,AdjList[MAX_VERTEX_NUM];
 
typedef struct	//图的构造 
{
	AdjList vertices;
	int vexnum,arcnum;  //图的当前顶点数和弧数
	int kind;	//图的种类标志 
}ALGraph;

Status LocateVex(ALGraph G,int v)//查找顶点的位置 
{
	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请输入总地点数和总路线数:");
	scanf("%d %d",&G.vexnum,&G.arcnum);
	printf("请输入各个地点名称:");
	for(i=0;i<G.vexnum;i++)
	{
	scanf("%d",&G.vertices[i].data);
	G.vertices[i].firstarc=NULL;	//初始化 
	} 
	printf("请输入起始站和终点站;\n");
	for(k=0;k<G.arcnum;k++)
	{	 
		scanf("%d %d",&v1,&v2);
		i=LocateVex(G,v1);j=LocateVex(G,v2);	//定位 
		p=(ArcNode*)malloc(sizeof(ArcNode));	//申请一个结点 
		p->adjvex=j;p->nextarc=NULL;	//赋值 
		p->nextarc=G.vertices[i].firstarc;	//连接结点 
		G.vertices[i].firstarc=p;	//连接结点 
		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);	//车号 
    scanf("%s%s%s",sch1,sch2,sch3);	//时间,出发站,终点站 
    scanf("%d",&s);		//站数 
    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)//按值查找
{
    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) 
// 在 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));  //生成新结点*s 
    s->data=e;  		//将结点*s的数据域置为e 
    s->next=p->next;  		//将结点*e的指针域指向结点ai 
    p->next=s;  	//将结点*p的指针域指向结点*s 
    return OK;  
}

Status PutPoint(Linklist L)	//查询的车号 
{
    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)//修改公交信息
{
    int choose1,y,a,b;
    char sch[30];
    Node *p=GetElem(L,i);
    printf("%d号公交车的信息如下:\n",i);
    PutPoint(p);
    printf("\n请输入要修改的内容:\n");
    printf("1、车号\n");
    printf("2、发车时间\n");
    printf("3、起始站\n");
    printf("4、终点站\n");
    printf("5、站数\n");
    scanf("%d",&choose1);
    while(choose1)
    {
    	if(choose1==1)
    	{
        	printf("请输入新的车号\n");
        	scanf("%d",&y);
        	p->num=y;
        	printf("信息修改完成\n");
        	break;
	    }
    	if(choose1==2)
    	{
        	printf("请输入新的时间\n");
        	scanf("%s",sch);
        	strcpy(p->time,sch);
        	printf("信息修改完成\n");
        	break;
    	}
    	if(choose1==3)
    	{
        	printf("请输入新的出发点\n");
        	scanf("%s",sch);
        	strcpy(p->start,sch);
        	printf("信息修改完成\n");
        	break;
    	}
    	if(choose1==4)
    	{
        	printf("请输入新的终点站\n");
        	scanf("%s",sch);
        	strcpy(p->end,sch);
        	printf("信息修改完成\n");
        	break;
   		}
    	if(choose1==5)
    	{
        	printf("请输入新的站数\n");
        	scanf("%d",&y);
        	p->score=y;
        	printf("信息修改完成\n");
        	break;
    	}
    }
}

Linklist DeleteList(Linklist L,int n)	//删除第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)	//输出链表内容
{
	if(L==NULL)
	{
		printf("没有公交路线!!\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;	
		
	}
 } 

//使用界面 
int main(void)
{
	int x,y,k;
    Linklist L1=NULL;
    Node *p;
	ALGraph G;
	CreateUDG(G);
	printf("\n******欢迎使用公交信息系统******\n");
	printf("******* 1、建立信息*******\n"); 
	printf("******* 2、查询信息*******\n");
	printf("******* 3、修改信息*******\n");
	printf("******* 4、删除信息*******\n");
	printf("******* 5、显示信息*******\n");
	printf("******* 0、退出*******\n");
	int n,choose2=-1;
	while(choose2!=0)
	{
		printf("\n请输入你要选择的功能前的序号:");  
        scanf("%d",&choose2); 
        if(choose2==0) 
            break;
        else if (choose2==1)
        {		
			printf("\n请输入要添加的信息,按照:车号->发车时间->起始站->终点站->站数 的顺序输入\n");
        	//for(k=0;k<G.arcnum;k++)
			L1=CreateList(L1);
			printf("添加成功!"); 
		}
		else if(choose2==2)
		{
			printf("\n请输入要查询的车号:");
            scanf("%d",&y);
        	p=GetElem(L1,y);
        	PutPoint(p);
		}
		else if(choose2==3)
		{
			printf("\n请输入要修改信息的车号:");
			scanf("%d",&y); 
			Revise(L1,y);
		 }
		else if(choose2==4)
		{
			printf("\n请输入要删除的车号:");
			scanf("%d",&y);
			L1=DeleteList(L1,y);
			printf("删除成功!\n");
		}
		else if(choose2==5)
		{
			printf("公交路线的信息为:\n");
			Putlist(L1);
		}
	}
	return 0;
 }

Written procedures not so simple, what I need guidance, and welcome to harassment Oh!
Finally, I wish you all a Happy New Year! Well-being!

Published 35 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43873385/article/details/104079414