基于单向循环链表及内核链表的航班管理系统

  该项目使用单向循环链表存放客户端登录信息,用内核链表存放航班信息。通过在单链表中定义指针数组成员来关联两个链表的信息;

        功能:有登录及注册界面,如果没有账号则需要注册。登录模块分为普通用户登录以及管理员登录,管理员界面权限更高,功能更丰富,可以强行操作普通用户的所有消息,包括添加新用户,删除用户,修改用户购票信息,查询用户账号密码,查询用户购票信息,查询当前所有航班,添加新的航班,删除航班等。由于项目使用的是纯链表,没有使用到数据库,因此特地初始化了六张航票信息。如需体验更多行班票,可自行在初始化那一块添加。

         

        作为入门的第一个小项目,代码能力肯定没有那些大神牛逼,并且由于时间有限,这个项目我一共做了两天,还有一些功能没写全,比如链表的排序,还有一些输入限制我没加上,比如int类型不允许输入字符,界面也没花时间去做,就将就看看,其他功能基本上没什么问题。下面是项目界面演示及源码展示。

                                                                   测试环境:    Ubuntu18.04                                            

                              写代码不易           转载请标明作者

                                                                        

登录界面

普通用户界面

管理员界面 

 航班信息

                

makefile

main:*.c *.h

        gcc -o $@ $^

main.c

#include"double_list.h"
#include"single_list.h"


void menu_print(void);
int usr_login(sg_list* head);
int root_login(void);
void jiemian1(sg_list* head,link* flight_head);
void jiemian2(sg_list* head,link* flight_head);
void jiemian3(sg_list* head);
void single_list_del(sg_list* head);

char cur_number[20]="-1";
void menu_print(void)
{
//printf("\033[1m\033[45;33m             HELLO_WORLD             \033[0m\n");

    printf("\033[1m\033[42;33m*************航班管理系统******************\033[0m\n");
    printf("****************请输入*********************\n");
    printf("*******************************************\n");
    printf("************1.普通用户登录*****************\n");
    printf("************2.航班管理员登录***************\n");
    printf("\033[5m\033[43;34m************3.注册账号*********************\033[0m\n");//
    printf("************4.退出系统*********************\n");//
    printf("*******************************************\n");
    
}


int usr_login(sg_list* head)
{    
    sg_list* p=head->next;
    struct usr_data temp;
    printf("请输入账号\n");
    scanf("%s",temp.num);
    while(getchar()!='\n');
    printf("请输入密码\n");
    scanf("%s",temp.password);
    while(getchar()!='\n');
    int flag=0;   //账号查询成功标志位,1为成功;
    while(p!=head)
    {
        if(strcmp(p->data.num,temp.num)==0)
        {
            flag=1;
            break;
        }
        
        p=p->next;
    }
    if(flag==0)
    {
        return -1;
//        printf("当前账号不存在\n");
    }
    else if(flag==1)
    {
        if(strcmp(p->data.password,temp.password)!=0)
        {
            return -2;
//            printf("密码错误\n");

        }else 
            strcpy(cur_number,p->data.num);

            return 1;    //登录成功    
    
    }
    
}

int root_login(void)
{
    int password=1;
    int temp=0;
    printf("请输入管理员密码,密码为1\n");
    scanf("%d",&temp);
    while(getchar()!='\n');
    if(password==temp)
        return 1;
    else
        return -1;
}
void jiemian1(sg_list* head,link* flight_head)  //普通用户
{
    link* flight_temp;
    sg_list* list_temp;
    int del_flag=0,fflag=0;
    while(1)
    {
        int select;
        printf("**************当前为普通用户***************\n");
        printf("****************请输入*********************\n");
        printf("*******************************************\n");
        printf("**************1.购票***********************\n");
        printf("**************2.退票***********************\n");
        printf("**************3.改签***********************\n");
        printf("**************4.查票***********************\n");
        printf("**************5.退出登录*******************\n");
        printf("*******************************************\n");
        scanf("%d",&select);
        switch(select)
        {
            case 1:
                list_head_print(flight_head);
                
                
                printf("请输入你要购票的航班号\n");    
                int num;
                scanf("%d",&num);
                while(getchar()!='\n');
                
                printf("\n");
                list_head_print(flight_head);
                
                flight_temp=flight_find_num_data(flight_head,num);//航班的节点
                if(flight_temp==NULL)
                {
                    printf("没有找到该航班请重新购票\n");
                    break;
                }
                
                list_temp=single_list_num_find(head,cur_number);//找到当前登录的用户链表节点
                if(list_temp==NULL)
                {
                    printf("用户名错误请即使联系程序员修改全局cur_number[20]\n");
                    printf("%s\n",cur_number);
                    break;
                }
//                link* qqq;
                list_temp->data.data[list_temp->data.ticket_number]=flight_temp;
                flight_temp->data.flag++;
//                *(list_temp->data.data+list_temp->data.ticket_number)=qqq;
//                *((list_temp->data).data+0)=qqq;
                (list_temp->data.ticket_number)++;
                
                printf("购票成功当前购票信息为:\n");
                list_node_print(flight_temp);
                
                
                
                
                    break;
            case 2: 
                    list_temp=single_list_num_find(head,cur_number);
                    if(list_temp->data.ticket_number==0)
                    {
                        printf("当前没买任何票,无法退票\n");
                        break;
                    }
                    printf("请输入你要退票票的航班号\n");    
                    char num1[18];
                    int ii;
                    scanf("%s",num1);
                    while(getchar()!='\n');
                        
                    del_flag=0;
                    //printf("list_temp->data.ticket_number=%d\n",list_temp->data.ticket_number);
                    for(ii=0;ii<(list_temp->data.ticket_number);ii++)
                    {
                        if((del_flag==0)&&strcmp((list_temp->data.data[ii])->data.number,num1)==0)
                        {
                            list_temp->data.data[ii]->data.flag--;
                            printf("退票成功,支付宝到账%d元\n",list_temp->data.data[ii]->data.value);
                    //        list_temp->data.data[ii]=NULL;
                    //        list_node_print(list_temp->data.data[ii]);                            
                    //        printf("list_temp->data.data[ii]->data->number=%s\n",list_temp->data.data[ii]->data.number);                            
                    //        list_node_print(list_temp->data.data[ii]);
                            del_flag=1;
                            
                        }
                        if(del_flag==1&&ii!=list_temp->data.ticket_number-1)
                        {
                            list_temp->data.data[ii]=list_temp->data.data[ii+1];
                            
                        }
                        
                    }
                    if(del_flag==1)
                    {
    //                    printf("退票成功\n");
                        list_temp->data.ticket_number--;
                    }
                    else 
                        printf("没有找到该票\n");
                    
           
                    del_flag=0;
                    
                        
                    
                    break;//退票
            case 3: 
                    list_temp=single_list_num_find(head,cur_number);//得到用户节点
                    if(list_temp->data.ticket_number==0)
                    {
                        printf("当前没买任何票,无法退票\n");
                        break;
                    }
                    printf("请输入你要更换的航班号\n");    
                    char num2[18];
                    int iii;
                    scanf("%s",num2);
                    while(getchar()!='\n');
                    
                    printf("请输入你更换后的航班号\n");    
                    char num3[18];
                    scanf("%s",num3);
                    while(getchar()!='\n');
                    fflag=0;
                    for(iii=0;iii<(list_temp->data.ticket_number);iii++)
                    {
                        if(strcmp((list_temp->data.data[iii])->data.number,num2)==0)
                        {
                            if(flight_find_num_data(flight_head,atoi(num3))==NULL)
                            {
                                printf("输入的飞机票不存在\n");
                                break;
                            }
                            list_temp->data.data[iii]->data.flag--;
                            list_temp->data.data[iii]=flight_find_num_data(flight_head,atoi(num3));
                            list_temp->data.data[iii]->data.flag++;
                            printf("换票成功,请自觉补差价\n");
                            fflag=1;
                            break;

                        }
                        
                    }
                    if(fflag==0)
                    {
                        
                        printf("改签失败,当前账户并没有该飞机票,无法换票\n");
                    }
                    
                    
                    break;
            case 4: 
                    printf("当前购票信息如下\n");
                    list_temp=single_list_num_find(head,cur_number);
                    if(list_temp==NULL)
                    {
                        printf("sorry,没找到cur_number:%s的用户\n",cur_number);
                        break;
                    }
                    
                    if(list_temp->data.ticket_number==0)
                    {
                        printf("sorry,当前没有买任何飞机票\n");
                    }
                    for(int i=0;i<list_temp->data.ticket_number;i++)
                    {
                        list_node_print(list_temp->data.data[i]);
                    }
                    break;//查票
            case 5:   
                strcpy(cur_number,"-1");
            return;//返回上一级
        }
    }
    
    
}

void jiemian2_menu(void)
{
    printf("**************当前为管理员*****************\n");
    printf("****************请输入*********************\n");
    printf("**************1.查看所有注册成员信息*******\n");
    printf("**************2.添加用户成员***************\n");
    printf("**************3.删除用户成员***************\n");
    printf("**************4.显示当前航班信息***********\n");
    printf("**************5.添加新的航班信息***********\n");
    printf("**************6.删除旧的航班信息***********\n");
    printf("**************7.退出登录*******************\n");
    printf("*******************************************\n");
}

void jiemian2(sg_list* head,link* flight_head)   //管理员功能    
{

    jiemian2_menu();
    int select=0;
    char num_temp[18];
    while(1)
    {
        scanf("%d",&select);
        switch(select)
        {
            case 1: single_list_print(head);    break;
            case 2: jiemian3(head);    break;
            case 3: 
                jiemian2_menu();
                single_list_del(head);   break;
            case 4:   
                    list_head_print(flight_head);
                    break;    //显示当前航班信息
            case 5:
                    flight_data_insert(flight_head);
            
            break;//添加航班信息
            case 6:
                    list_head_print(flight_head);
                    printf("请输入要删除的航班号\n");
                    scanf("%s",num_temp);
                    while(getchar()!='\n');
                    list_del_data(flight_head,num_temp);  
                
            break;//删除航班信息
            case 7:     
            return;
        }
        jiemian2_menu();
        
    }
    

}


void jiemian3(sg_list* head)
{
    printf("************当前为注册账号界面*************\n");
    printf("**************请输入用户名*****************\n");
    struct usr_data data;
    data.key=1;
    data.ticket_number=0;
    scanf("%s",data.name);
    while(getchar()!='\n');
    printf("**************请输入账号*******************\n");
    scanf("%s",data.num);
    while(getchar()!='\n');
    printf("**************请输入密码*******************\n");
    scanf("%s",data.password);
    while(getchar()!='\n');
    single_list_head_insert(head,data);                    
    printf("*******************************************\n");
    printf("********注册成功你的账号信息如下***********\n");
    printf("usr->name=%-10s\tusr->num=%s\tusr->password=%s\tkey=%d\tticket_number=%d\t\n",data.name,data.num,data.password,data.key,data.ticket_number);    
}


void single_list_del(sg_list* head)
{
    printf("***请选择你要删除的用户名或账号***\n******1.用户名******\n******2.账号******\n");
    int select=0;
    char name_num_data[20];
    scanf("%d",&select);
    while(getchar()!='\n');
    if(select!=1&&select!=2)
    {
        printf("输入范围错误\n");
        return;
    }
    printf("请输入你要删除的内容\n");
    scanf("%s",name_num_data);
    while(getchar()!='\n');
    sg_list* p=head->next;
    sg_list* prev=head;
    while(p!=head)
    {
        printf("usr->name=%-10s\tusr->num=%s\tusr->password=%s\tkey==%d\t\n",p->data.name,p->data.num,p->data.password,p->data.key);
#if 1 
        if(select==1)
        {
            if(strcmp(p->data.name,name_num_data)==0)
            {
                prev->next=p->next;
                free(p);
                p=NULL;
                printf("用户名为%s的用户已删除\n",name_num_data);
                return;
            }
            if(p==head)
            {
                printf("当前用户不存在\n");return;
            }
        }
        else if(select==2)
        {
            if(strcmp(p->data.num,name_num_data)==0)
            {
                prev->next=p->next;
                free(p);
                p=NULL;
                printf("账号为%s的用户已删除\n",name_num_data);
                return;
            }    
            if(p==head)
            {
                printf("当前账号不存在\n");return;
            }
        }
        else 
        {
            printf("输入范围错误\n");
            return;
        }
#endif        
        p=p->next;
        prev=prev->next;
    }

}


int main(void)
{
    int jiemian;//1为普通用户登录界面  2为航班管理员登录界面    3为注册账号   4.为退出系统
    int ret=0;
    sg_list* head;
    single_list_init(&head);
    
    link* flight_head;
    link_init(&flight_head);
    flight_datainit(flight_head);


    while(1)
    {
#if 1    
        menu_print();
        scanf("%d",&jiemian);
        while(getchar()!='\n');
        switch(jiemian)
        {
            case 1:
                ret=usr_login(head);
                if(ret==-1)
                {
                    printf("该账号不存在\n");
                    break;
                }
                else if(ret==-2)
                {
                    printf("密码错误\n");
                    break;
                }
                else if(ret==1);
                {
                    printf("登录成功\n");
                   
                    jiemian1(head,flight_head);
                }
                break;//普通用户

            case 2:
                ret=root_login();
                if(ret==1)
                {
                    printf("登录成功\n");
                    jiemian2(head,flight_head);
                }
                else if(ret==-1)
                {
                    printf("密码错误\n");
                    break;
                }
                break;//管理员
            case 3:jiemian3(head);break;//注册账号
            case 4:return 0;break;//退出系统
            
        }
    
        single_list_print(head);
        
#endif

    }

single_list.c    

#include"single_list.h"


void single_list_init(sg_list** head)
{
    *head=(sg_list*)malloc(sizeof(sg_list));
    (*head)->next=*head;

}

void single_list_head_insert(sg_list* head,struct usr_data data)
{
    sg_list* node=(sg_list*)malloc(sizeof(sg_list));
    node->data=data;
    node->next=head->next;
    head->next=node;

}


void single_list_print(sg_list* head)
{
    sg_list* temp=head->next;
    while(temp!=head)
    {
        printf("usr->name=%-10s\tusr->num=%s\tusr->password=%s\tkey==%d\t",(temp->data.name),(temp->data.num),(temp->data.password),temp->data.key);
        printf("\n");
        temp=temp->next;
    }
    printf("\n");

}

sg_list* single_list_num_find(sg_list* head,char num[20])
{
    sg_list* temp=head->next;
    while(temp!=head)
    {
        if(strcmp(num,temp->data.num)==0)
        {
            return temp;
        }
        temp=temp->next;
    }
    return NULL;
}




}


single_list.h

#ifndef _SINGLE_LIST_H
#define _SINGLE_LIST_H

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"double_list.h"

struct usr_data                       //用户信息
{
    char name[20];
    char num[20];
    char password[20];
    char key;     //0为管理员权限,1为普通用户
    link* data[5];//节点地址
    int ticket_number;  //当前用户票数
};

typedef struct SINGLE_LIST             //用户信息的单向循环链表
{
    struct usr_data data;
    struct SINGLE_LIST* next;
}sg_list;


void single_list_init(sg_list** head);
void single_list_head_insert(sg_list* head,struct usr_data data);
void single_list_print(sg_list* head);
sg_list* single_list_num_find(sg_list* head,char num[20]);


#endif

double_list.c

//内核双向循环链表

#include"double_list.h"


void link_init(link** head)     //初始化表头
{
    *head =(link*)malloc(sizeof(link));
    INIT_LIST_HEAD(&((*head)->list));
}

void list_head_insert(link* head,struct flight_data data)   //头插
{
    link* node=(link*)malloc(sizeof(link));
    node->data=data;
    list_add(&node->list, &head->list);
}

void list_end_insert(link* head,struct flight_data data)    //尾插
{
    link* node=(link*)malloc(sizeof(link));
    node->data=data;
    list_add_tail(&node->list,&head->list);
}

void list_head_print(link* head)              //从头打印
{
    link* pos;
    list_for_each_entry(pos, &head->list,list)
    {
        printf("航班号:%s\t起点站:%s\t终点站:%s\t班期:%s\t机型:%s\t起飞时间:%s\t到达时间:%s\t票价:%d\t",pos->data.number,pos->data.staddress,pos->data.arraddress,pos->data.DATE,pos->data.TYPE,pos->data.stime,pos->data.atime,pos->data.value);
        printf("\n");
    }
    
}

void list_node_print(link* node)
{
        link* pos=node;
        printf("航班号:%s\t起点站:%s\t终点站:%s\t班期:%s\t机型:%s\t起飞时间:%s\t到达时间:%s\t票价:%d\t",pos->data.number,pos->data.staddress,pos->data.arraddress,pos->data.DATE,pos->data.TYPE,pos->data.stime,pos->data.atime,pos->data.value);
        printf("\n");
}


void flight_data_insert(link* head)
{
    struct flight_data data;
    data.flag=0;
    
    printf("请输入航班号\n");
    scanf("%s",data.number);
    while(getchar()!='\n');
    
    printf("请输入起点站\n");
    scanf("%s",data.staddress);
    while(getchar()!='\n');
    
    printf("请输入终点站\n");
    scanf("%s",data.arraddress);
    while(getchar()!='\n');
    
    printf("请输入班期\n");
    scanf("%s",data.DATE);
    while(getchar()!='\n');
    
    printf("请输入机型 \n");
    scanf("%s",data.TYPE);
    while(getchar()!='\n');
    
    printf("请输入起飞时间\n");
    scanf("%s",data.stime);
    while(getchar()!='\n');
    
    printf("请输入到达时间\n");
    scanf("%s",data.atime);
    while(getchar()!='\n');
    
    printf("请输入票价\n");
    scanf("%d",&data.value);
    while(getchar()!='\n');
    
    list_end_insert(head,data);
//    printf("航班号:%s\t起点站:%s\t终点站:%s\t班期:%s\t机型:%s\t起飞时间:%s\t到达时间:%s\t票价:%d\t",data.number,data.staddress,data.arraddress,data.DATE,data.TYPE,data.stime,data.atime,data.value);
    printf("\n");
    
}

void list_del_data(link* head,char number[18])     //查找内容并删除
{
    link* pos;
    list_for_each_entry(pos, &head->list,list)
    {
        if(!strcmp(pos->data.number,number))     用strcmp判断
        {    
            if(pos->data.flag!=0)
            {
                printf("请先把用户购买的飞机票退票后再删除\n");
                return;
            }
            list_del_init(&pos->list); 
            printf("删除成功\n");
            list_head_print(head);
            
            return;     //只能删除一个,就把pos初始化
//            __list_del_entry(&pos->list);                //全部删除
            
        }
    }
}

void flight_data_del(link* head)
{
    char number[18];
    printf("请输入要删除的航班号\n");
    scanf("%s",number);
    while(getchar()!='\n');
    list_del_data(head,number);
    
}

void flight_datainit(link* head)
{
    struct flight_data data;
    data.flag=0;
    
    strcpy(data.number,"1001");strcpy(data.staddress,"广州");
    strcpy(data.arraddress,"北京");strcpy(data.DATE,"2022/05/06");
    strcpy(data.TYPE,"BY737");strcpy(data.stime,"8:00");
    strcpy(data.atime,"12:00");data.value=1156;
    list_end_insert(head,data);
    
    strcpy(data.number,"1002");strcpy(data.staddress,"广州");
    strcpy(data.arraddress,"深圳");strcpy(data.DATE,"2022/05/08");
    strcpy(data.TYPE,"BY737");strcpy(data.stime,"14:00");
    strcpy(data.atime,"14:20");data.value=203;
    list_end_insert(head,data);
    
    strcpy(data.number,"1003");strcpy(data.staddress,"广州");
    strcpy(data.arraddress,"长沙");strcpy(data.DATE,"2022/05/04");
    strcpy(data.TYPE,"BY747");strcpy(data.stime,"12:40");
    strcpy(data.atime,"13:20");data.value=368;
    list_end_insert(head,data);
    
    strcpy(data.number,"1004");strcpy(data.staddress,"广州");
    strcpy(data.arraddress,"上海");strcpy(data.DATE,"2022/05/10");
    strcpy(data.TYPE,"BY747");strcpy(data.stime,"8:00");
    strcpy(data.atime,"10:00");data.value=984;
    list_end_insert(head,data);
    
    strcpy(data.number,"1005");strcpy(data.staddress,"深圳");
    strcpy(data.arraddress,"潍坊");strcpy(data.DATE,"2022/05/06");
    strcpy(data.TYPE,"BY747");strcpy(data.stime,"9:00");
    strcpy(data.atime,"13:00");data.value=1985;
    list_end_insert(head,data);
    
    strcpy(data.number,"1006");strcpy(data.staddress,"深圳");
    strcpy(data.arraddress,"哈尔滨");strcpy(data.DATE,"2022/05/06");
    strcpy(data.TYPE,"BY737");strcpy(data.stime,"8:00");
    strcpy(data.atime,"14:00");data.value=2667;
    list_end_insert(head,data);
    
    
}

link* flight_find_num_data(link* head,int num)
{
    link* pos;
    
    list_for_each_entry(pos, &head->list,list)
    {
        if(num==atoi(pos->data.number))
        {
            return pos;
        }
    }
    return NULL;
}

void flight_double_list(void)
{
    link* head =(link*)malloc(sizeof(link));
    link_init(&head);
    
    
//    data.number="1001";data.staddress="广州";data.arraddress="北京";
//    data.DATE="2022/05/06";data.TYPE="BY737";data.stime="8:00";
//    data.atime="12:00";
    flight_datainit(head);
    list_head_print(head);
    printf("********************\n");
    while(1)
    {
        flight_data_insert(head);
        list_head_print(head);
        flight_data_insert(head);
        list_head_print(head);
        flight_data_del(head);
        list_head_print(head);
    }
   
}

double_list.h

#ifndef _DOUBLE_LIST_H
#define _DOUBLE_LIST_H

#include<stdio.h>
#include"list.h"
#include<stdlib.h>
#include<string.h>
struct flight_data       //航班信息数据结构
{   
    char number[18];    //航班号    
    char staddress[18];   //起点站    
    char arraddress[18];   //终点站    
    char DATE[12];       //班期    
    char TYPE[8];       //机型    
    char stime[10];          //起飞时间    
    char atime[10];          //到达时间    
    int value;           //票价   
    int flag;    //买票标志位
};
/**
 *从尾查找
 *
 */
#define list_end_for_each_entry(pos, head,member)   \
    for (pos = list_entry((head)->prev, typeof(*pos), list);\
            &pos->member != (head);\
            pos = list_entry(pos->member.prev, typeof(*pos), member))

typedef struct LINK
{
    struct flight_data data;
    struct list_head list;
}link;

void link_init(link** head);                    //初始化表头
void list_head_insert(link* head,struct flight_data data);   //头插
void list_end_insert(link* head,struct flight_data data);    //尾插
void list_head_print(link* head);               //从头打印
void list_final_print(link* head);              //从尾打印
void list_del_data(link* head,char number[18]);        //查找内容并删除
void list_change_data(link* head,int new_data,int old_data,int mode);//查找替换,mode=1为单个替换,mode=2为全部替换

void flight_datainit(link* head);
void flight_double_list(void);
void flight_data_insert(link* head);
link* flight_find_num_data(link* head,int num);
void list_node_print(link* node);//打印单个结点信息
#endif

list.h

#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H

/*
 * Simple doubly linked list implementation.
 *
 * Some of the internal functions ("__xxx") are useful when
 * manipulating whole lists rather than single entries, as
 * sometimes we already know the next/prev entries and we can
 * generate better code by using them directly rather than
 * using the generic single-entry routines.
 */

struct list_head{
    struct list_head *next,*prev;
};

#define LIST_POISON1  ((void *) 0x00100100 + 0)
#define LIST_POISON2  ((void *) 0x00200200 + 0)

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({            \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

/*
 * Insert a new entry between two known consecutive entries.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
#ifndef CONFIG_DEBUG_LIST
static inline void __list_add(struct list_head *new,
                  struct list_head *prev,
                  struct list_head *next)
{
    next->prev = new;
    new->next = next;
    new->prev = prev;
    prev->next = new;
}
#else
extern void __list_add(struct list_head *new,
                  struct list_head *prev,
                  struct list_head *next);
#endif

/**
 * list_add - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}


/**
 * list_add_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 *
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 */
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}

/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}

/**
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 * Note: list_empty() on entry does not return true after this, the entry is
 * in an undefined state.
 */
#ifndef CONFIG_DEBUG_LIST
static inline void __list_del_entry(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
}

static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = LIST_POISON1;
    entry->prev = LIST_POISON2;
}
#else
extern void __list_del_entry(struct list_head *entry);
extern void list_del(struct list_head *entry);
#endif

/**
 * list_replace - replace old entry by new one
 * @old : the element to be replaced
 * @new : the new element to insert
 *
 * If @old was empty, it will be overwritten.
 */
static inline void list_replace(struct list_head *old,
                struct list_head *new)
{
    new->next = old->next;
    new->next->prev = new;
    new->prev = old->prev;
    new->prev->next = new;
}

static inline void list_replace_init(struct list_head *old,
                    struct list_head *new)
{
    list_replace(old, new);
    INIT_LIST_HEAD(old);
}

/**
 * list_del_init - deletes entry from list and reinitialize it.
 * @entry: the element to delete from the list.
 */
static inline void list_del_init(struct list_head *entry)
{
    __list_del_entry(entry);
    INIT_LIST_HEAD(entry);
}

/**
 * list_move - delete from one list and add as another's head
 * @list: the entry to move
 * @head: the head that will precede our entry
 */
static inline void list_move(struct list_head *list, struct list_head *head)
{
    __list_del_entry(list);
    list_add(list, head);
}

/**
 * list_move_tail - delete from one list and add as another's tail
 * @list: the entry to move
 * @head: the head that will follow our entry
 */
static inline void list_move_tail(struct list_head *list,
                  struct list_head *head)
{
    __list_del_entry(list);
    list_add_tail(list, head);
}

/**
 * list_is_last - tests whether @list is the last entry in list @head
 * @list: the entry to test
 * @head: the head of the list
 */
static inline int list_is_last(const struct list_head *list,
                const struct list_head *head)
{
    return list->next == head;
}

/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}

/**
 * list_empty_careful - tests whether a list is empty and not being modified
 * @head: the list to test
 *
 * Description:
 * tests whether a list is empty _and_ checks that no other CPU might be
 * in the process of modifying either member (next or prev)
 *
 * NOTE: using list_empty_careful() without synchronization
 * can only be safe if the only activity that can happen
 * to the list entry is list_del_init(). Eg. it cannot be used
 * if another CPU could re-list_add() it.
 */
static inline int list_empty_careful(const struct list_head *head)
{
    struct list_head *next = head->next;
    return (next == head) && (next == head->prev);
}

/**
 * list_rotate_left - rotate the list to the left
 * @head: the head of the list
 */
static inline void list_rotate_left(struct list_head *head)
{
    struct list_head *first;

    if (!list_empty(head)) {
        first = head->next;
        list_move_tail(first, head);
    }
}

/**
 * list_is_singular - tests whether a list has just one entry.
 * @head: the list to test.
 */
static inline int list_is_singular(const struct list_head *head)
{
    return !list_empty(head) && (head->next == head->prev);
}

static inline void __list_cut_position(struct list_head *list,
        struct list_head *head, struct list_head *entry)
{
    struct list_head *new_first = entry->next;
    list->next = head->next;
    list->next->prev = list;
    list->prev = entry;
    entry->next = list;
    head->next = new_first;
    new_first->prev = head;
}

/**
 * list_cut_position - cut a list into two
 * @list: a new list to add all removed entries
 * @head: a list with entries
 * @entry: an entry within head, could be the head itself
 *    and if so we won't cut the list
 *
 * This helper moves the initial part of @head, up to and
 * including @entry, from @head to @list. You should
 * pass on @entry an element you know is on @head. @list
 * should be an empty list or a list you do not care about
 * losing its data.
 *
 */
static inline void list_cut_position(struct list_head *list,
        struct list_head *head, struct list_head *entry)
{
    if (list_empty(head))
        return;
    if (list_is_singular(head) &&
        (head->next != entry && head != entry))
        return;
    if (entry == head)
        INIT_LIST_HEAD(list);
    else
        __list_cut_position(list, head, entry);
}

static inline void __list_splice(const struct list_head *list,
                 struct list_head *prev,
                 struct list_head *next)
{
    struct list_head *first = list->next;
    struct list_head *last = list->prev;

    first->prev = prev;
    prev->next = first;

    last->next = next;
    next->prev = last;
}

/**
 * list_splice - join two lists, this is designed for stacks
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 */
static inline void list_splice(const struct list_head *list,
                struct list_head *head)
{
    if (!list_empty(list))
        __list_splice(list, head, head->next);
}

/**
 * list_splice_tail - join two lists, each list being a queue
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 */
static inline void list_splice_tail(struct list_head *list,
                struct list_head *head)
{
    if (!list_empty(list))
        __list_splice(list, head->prev, head);
}

/**
 * list_splice_init - join two lists and reinitialise the emptied list.
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 *
 * The list at @list is reinitialised
 */
static inline void list_splice_init(struct list_head *list,
                    struct list_head *head)
{
    if (!list_empty(list)) {
        __list_splice(list, head, head->next);
        INIT_LIST_HEAD(list);
    }
}

/**
 * list_splice_tail_init - join two lists and reinitialise the emptied list
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 *
 * Each of the lists is a queue.
 * The list at @list is reinitialised
 */
static inline void list_splice_tail_init(struct list_head *list,
                     struct list_head *head)
{
    if (!list_empty(list)) {
        __list_splice(list, head->prev, head);
        INIT_LIST_HEAD(list);
    }
}

/**
 * list_entry - get the struct for this entry
 * @ptr:    the &struct list_head pointer.
 * @type:    the type of the struct this is embedded in.
 * @member:    the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)

/**
 * list_first_entry - get the first element from a list
 * @ptr:    the list head to take the element from.
 * @type:    the type of the struct this is embedded in.
 * @member:    the name of the list_struct within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define list_first_entry(ptr, type, member) \
    list_entry((ptr)->next, type, member)

/**
 * list_for_each    -    iterate over a list
 * @pos:    the &struct list_head to use as a loop cursor.
 * @head:    the head for your list.
 */
#define list_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)

/**
 * __list_for_each    -    iterate over a list
 * @pos:    the &struct list_head to use as a loop cursor.
 * @head:    the head for your list.
 *
 * This variant doesn't differ from list_for_each() any more.
 * We don't do prefetching in either case.
 */
#define __list_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)

/**
 * list_for_each_prev    -    iterate over a list backwards
 * @pos:    the &struct list_head to use as a loop cursor.
 * @head:    the head for your list.
 */
#define list_for_each_prev(pos, head) \
    for (pos = (head)->prev; pos != (head); pos = pos->prev)

/**
 * list_for_each_safe - iterate over a list safe against removal of list entry
 * @pos:    the &struct list_head to use as a loop cursor.
 * @n:        another &struct list_head to use as temporary storage
 * @head:    the head for your list.
 */
#define list_for_each_safe(pos, n, head) \
    for (pos = (head)->next, n = pos->next; pos != (head); \
        pos = n, n = pos->next)

/**
 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
 * @pos:    the &struct list_head to use as a loop cursor.
 * @n:        another &struct list_head to use as temporary storage
 * @head:    the head for your list.
 */
#define list_for_each_prev_safe(pos, n, head) \
    for (pos = (head)->prev, n = pos->prev; \
         pos != (head); \
         pos = n, n = pos->prev)

/**
 * list_for_each_entry    -    iterate over list of given type
 * @pos:    the type * to use as a loop cursor.
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 */
#define list_for_each_entry(pos, head, member)                \
    for (pos = list_entry((head)->next, typeof(*pos), member);    \
         &pos->member != (head);     \
         pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * list_for_each_entry_reverse - iterate backwards over list of given type.
 * @pos:    the type * to use as a loop cursor.
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 */
#define list_for_each_entry_reverse(pos, head, member)            \
    for (pos = list_entry((head)->prev, typeof(*pos), member);    \
         &pos->member != (head);     \
         pos = list_entry(pos->member.prev, typeof(*pos), member))

/**
 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
 * @pos:    the type * to use as a start point
 * @head:    the head of the list
 * @member:    the name of the list_struct within the struct.
 *
 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
 */
#define list_prepare_entry(pos, head, member) \
    ((pos) ? : list_entry(head, typeof(*pos), member))

/**
 * list_for_each_entry_continue - continue iteration over list of given type
 * @pos:    the type * to use as a loop cursor.
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 *
 * Continue to iterate over list of given type, continuing after
 * the current position.
 */
#define list_for_each_entry_continue(pos, head, member)         \
    for (pos = list_entry(pos->member.next, typeof(*pos), member);    \
         &pos->member != (head);    \
         pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * list_for_each_entry_continue_reverse - iterate backwards from the given point
 * @pos:    the type * to use as a loop cursor.
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 *
 * Start to iterate over list of given type backwards, continuing after
 * the current position.
 */
#define list_for_each_entry_continue_reverse(pos, head, member)        \
    for (pos = list_entry(pos->member.prev, typeof(*pos), member);    \
         &pos->member != (head);    \
         pos = list_entry(pos->member.prev, typeof(*pos), member))

/**
 * list_for_each_entry_from - iterate over list of given type from the current point
 * @pos:    the type * to use as a loop cursor.
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 *
 * Iterate over list of given type, continuing from current position.
 */
#define list_for_each_entry_from(pos, head, member)             \
    for (; &pos->member != (head);    \
         pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:    the type * to use as a loop cursor.
 * @n:        another type * to use as temporary storage
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 */
#define list_for_each_entry_safe(pos, n, head, member)            \
    for (pos = list_entry((head)->next, typeof(*pos), member),    \
        n = list_entry(pos->member.next, typeof(*pos), member);    \
         &pos->member != (head);                     \
         pos = n, n = list_entry(n->member.next, typeof(*n), member))

/**
 * list_for_each_entry_safe_continue - continue list iteration safe against removal
 * @pos:    the type * to use as a loop cursor.
 * @n:        another type * to use as temporary storage
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 *
 * Iterate over list of given type, continuing after current point,
 * safe against removal of list entry.
 */
#define list_for_each_entry_safe_continue(pos, n, head, member)         \
    for (pos = list_entry(pos->member.next, typeof(*pos), member),         \
        n = list_entry(pos->member.next, typeof(*pos), member);        \
         &pos->member != (head);                        \
         pos = n, n = list_entry(n->member.next, typeof(*n), member))

/**
 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
 * @pos:    the type * to use as a loop cursor.
 * @n:        another type * to use as temporary storage
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 *
 * Iterate over list of given type from current point, safe against
 * removal of list entry.
 */
#define list_for_each_entry_safe_from(pos, n, head, member)             \
    for (n = list_entry(pos->member.next, typeof(*pos), member);        \
         &pos->member != (head);                        \
         pos = n, n = list_entry(n->member.next, typeof(*n), member))

/**
 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
 * @pos:    the type * to use as a loop cursor.
 * @n:        another type * to use as temporary storage
 * @head:    the head for your list.
 * @member:    the name of the list_struct within the struct.
 *
 * Iterate backwards over list of given type, safe against removal
 * of list entry.
 */
#define list_for_each_entry_safe_reverse(pos, n, head, member)        \
    for (pos = list_entry((head)->prev, typeof(*pos), member),    \
        n = list_entry(pos->member.prev, typeof(*pos), member);    \
         &pos->member != (head);                     \
         pos = n, n = list_entry(n->member.prev, typeof(*n), member))

/**
 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
 * @pos:    the loop cursor used in the list_for_each_entry_safe loop
 * @n:        temporary storage used in list_for_each_entry_safe
 * @member:    the name of the list_struct within the struct.
 *
 * list_safe_reset_next is not safe to use in general if the list may be
 * modified concurrently (eg. the lock is dropped in the loop body). An
 * exception to this is if the cursor element (pos) is pinned in the list,
 * and list_safe_reset_next is called after re-taking the lock and before
 * completing the current iteration of the loop body.
 */
#define list_safe_reset_next(pos, n, member)                \
    n = list_entry(pos->member.next, typeof(*pos), member)

/*
 * Double linked lists with a single pointer list head.
 * Mostly useful for hash tables where the two pointer list head is
 * too wasteful.
 * You lose the ability to access the tail in O(1).
 */
#endif

                                        

        测试环境:    Ubuntu18.04                                            

                              写代码不易           转载请标明作者

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值