之所以把这个代码称为一个程序而不叫一个项目,是因为鄙人还是个还没入行的新人,不敢妄自菲薄,只因一时兴起上来感慨吐槽一番,如果有什么不对的地方,请各位大牛高台贵手,如果能提点一番,小弟感激不尽
废话不多,拿到题目就开干。
刚学完内核链表,就拿它来开刀。内核给你写好封装一些链表操作的接口函数,拿来用多爽,不用自己写。这个写好的东西在哪呢,在内核目录的include/linux/下。
#include
#include
#include
#include "kernel_list.h"
struct flight
{
char flight_id[15]; //航班号 //航班号
char flight_place[15]; //航班起飞站->终点站 //买票者的名字
char flight_time[15]; //起飞时间 //买到的票的座位号
int flight_seat; //航班总座位数 //票的张数
float price; //机票价格 //花费的金额
int remain; //剩余座位数
char st[15]; //座位标志
struct list_head mylist;
};
// 内核链表的初始化
struct flight * list_init()
{
struct flight *head=malloc(sizeof(struct flight));
INIT_LIST_HEAD(&(head->mylist));
return head;
}
//判断链表是否为空
int kernellist_empty(struct flight *head)
{
return list_empty(&(head->mylist));
}
// 创建新的节点
struct flight * new_node(char *flight_id,char *flight_place,char *flight_time,int flight_seat,float price)
{
struct flight *new=malloc(sizeof(struct flight));
strcpy(new->flight_id,flight_id);
strcpy(new->flight_place,flight_place);
strcpy(new->flight_time,flight_time);
new->flight_seat=flight_seat;
new->remain=flight_seat;
new->price=price;
bzero(new->st,15);
INIT_LIST_HEAD(&(new->mylist));
return new;
}
// 添加新的节点到内核链表中 添加到head的前面
int kernellist_add(struct flight *new,struct flight *head)
{
list_add_tail(&(new->mylist), &(head->mylist));
return 0;
}
//添加到head的后面
int kernellist_add_tail(struct flight *new,struct flight *head)
{
list_add(&(new->mylist), &(head->mylist));
return 0;
}
//删除一个节点
int kernellist_del(struct flight *del)
{
list_del(&(del->mylist));
return 0;
}
// 打印航班信息
int show(struct flight *head)
{
if(kernellist_empty(head))
{
printf("no any flight exist!\n");
return 1;
}
printf("----------------------------------------------------------\n");
printf("flight_id flight_place flight_time flight_seat price\n");
struct flight *pos;
list_for_each_entry(pos,&(head->mylist),mylist) //该宏函数实际是个for循环
//for (pos = list_entry((head)->next, typeof(*pos), member); &pos->member != (head); pos = list_entry(pos->member.next, typeof(*pos), member))
{
printf("%-16s%-16s%-16s%-13d%.2f\n",pos->flight_id,pos->flight_place,pos->flight_time,pos->flight_seat,pos->price);
}
printf("----------------------------------------------------------\n");
return 0;
}
//打印卖票信息
int show_ticket(struct flight *head)
{
int i;
if(kernellist_empty(head))
{
printf("no bodey buy ticket!\n");
return 0;
}
printf("----------------------------------------------------------\n");
printf("flight_id name num price ticketnum\n");
struct flight *pos;
list_for_each_entry(pos,&(head->mylist),mylist) //该宏函数实际是个for循环
//for (pos = list_entry((head)->next, typeof(*pos), member); &pos->member != (head); pos = list_entry(pos->member.next, typeof(*pos), member))
{
printf("%-16s%-16s%-16d%-8.2f",pos->flight_id,pos->flight_place,
pos->flight_seat,pos->price);
for(i=0;i<15;i++)
{
if(pos->flight_time[i] == 1)
printf("%d ",i+1);
}
printf("\n");
}
printf("----------------------------------------------------------\n");
return 0;
}
//翻转航班信息
int reversal(struct flight *head)
{
struct list_head *pos;
pos = head->mylist.next;
while(pos->next != &(head->mylist))
list_move_tail(head->mylist.prev,pos);
return 0;
}
//航班信息排序
int flight_sort(struct flight *head)
{
int choose;
struct list_head *posi;
struct list_head *posj;
struct list_head *temp;
while(1)
{
printf( "====================================================================\n"
"[1]flight_id [2]flight_time [3]price [4]reversal [5]show [0]quit\n"
"====================================================================\n");
if(scanf("%d",&choose) != 1 || getchar()!='\n') //判断是否正确输入
{
choose=6;
while(getchar() != '\n');
}
switch(choose)
{
case 0:
return 0;
break;
case 1:
list_for_each(posi,&(head->mylist))
//for (posi = head->mylist.next; posi != &(head->mylist);posi = posi->next)
{
for (posj = posi->next; posj != &(head->mylist);posj = posj->next)
{
if(strcmp(list_entry(posi,struct flight,mylist)->flight_id,list_entry(posj,struct flight,mylist)->flight_id)==1)
{
temp = posi->prev;
list_move(posi,posj);
list_move(posj,temp);
temp=posi;
posi = posj;
posj = temp;
}
}
}
break;
case 2:
list_for_each(posi,&(head->mylist))
//for (posi = head->mylist.next; posi != &(head->mylist);posi = posi->next)
{
f