火车票订票系统(链表结构化设计)

train.h文件

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct train {//车次的属性
	int id;
	char name[50];
	int remainTickets;
};

struct node {//普通节点的属性
	struct node *next;
	struct train * train_inform;	
};

struct head {//头节点属性
	struct node * nd;
	int fg;
};

struct tail {//尾节点属性
	struct node * nd;
	int fg;
};

struct list{//链表的属性
	struct head * listhead;
	int len;
};

struct list * initList();

struct list * createList(struct list * l);

struct node * searchTrain(struct list * l,int id);

int printNode(struct node * train);

int bookTrain(struct list * l,int id);

int returnTrain(struct list * l,int id);

int menu(struct list * l);

int usage(void);

/

train.c文件


#include "train.h"

int main()
{
	usage();
	return 0;
}

struct list * initList(){
	struct list * l;
	l = (struct list *)malloc(sizeof(struct list));
	l->listhead = (struct head *)malloc(sizeof(struct head));
	l->listhead->nd = (struct node *)malloc(sizeof(struct node));
	l->listhead->nd->train_inform = (struct train *)malloc(sizeof(struct train));
	l->listhead->nd->next=NULL;
	l->len=0;
	return l;
}

struct list * createList(struct list * l){
	int id;
	char name[50];
	int remainTickets;
	struct node * p, * last;
	last=l->listhead->nd;
	printf("依次输入:列车id号,名称,剩余票数(id==-1终止!)\n");
	scanf("%d%s%d",&id,&name,&remainTickets);
	while(id!=-1){
		p=(struct node *)malloc(sizeof(struct node));
		p->train_inform=(struct train *)malloc(sizeof(struct train));
		p->train_inform->id=id;
		strcpy(p->train_inform->name,name);
		p->train_inform->remainTickets=remainTickets;
		l->len++;
		last->next=p;  
		last=p;  
		p->next=NULL;
		scanf("%d%s%d",&id,&name,&remainTickets);
	}
	return l;
}

struct node * searchTrain(struct list * l,int id){//查询,返回节点信息
	struct node * p;
	p = l->listhead->nd;
	while(p!=NULL){
		if(p->train_inform->id==id){
			break;
		}
		p=p->next;
	}
	return p;
}

int printNode(struct node * train){//打印节点信息
	printf("列车id: %d\n",train->train_inform->id);
	printf("列车名称:%s\n",train->train_inform->name);
	printf("剩余票数:%d\n",train->train_inform->remainTickets);
	return 0;
}

int bookTrain(struct list * l,int id){//订票
	struct node * p;
	p = l->listhead->nd;
	while(p!=NULL){
		if(p->train_inform->id==id){
			p->train_inform->remainTickets--;
			break;
		}
		p=p->next;
	}
	printNode(p);
	return 0;
}
	
int returnTrain(struct list * l,int id){//退票
	struct node * p;
	p = l->listhead->nd;
	while(p!=NULL){
		if(p->train_inform->id==id){
			p->train_inform->remainTickets++;
			break;
		}
		p=p->next;
	}
	printNode(p);
	return 0;
}
int menu(struct list * l){//具体实现usage
	int x,id;
	int k=1;
	struct node * p;
	while(k){
		printf("请输入序列号:");
		scanf("%d",&x);
		switch(x){
		case 1:
			printf("输入所要查询的列车的id号:");
			scanf("%d",&id);
			p = searchTrain(l,id);
			printNode(p);
			break;
		case 2:
			printf("输入所要订票的列车的id号:");
			scanf("%d",&id);
			bookTrain(l,id);
			break;
		case 3:
			printf("输入所要订票的列车的id号:");
			scanf("%d",&id);
			bookTrain(l,id);
			break;
		case 4:k=0;break;
		default:return 0;
		}
	}
	return 0;
}

int usage(void){//打印提示界面
	struct list * l;
	l=initList();
	printf("请输入列车原始信息!\n");
	l = createList(l);
	printf("请按提示输入完成操作!\n");
	printf("1.查询车次信息\n");
	printf("2.订票\n");
	printf("3.退票\n");
	printf("4.退出系统\n");
	menu(l);
	return 0;
}

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值