c语言———————————链队数据结构基本 *** 作任务

c语言———————————链队数据结构基本 *** 作任务,第1张

实现任务:

1.初始化                        //我写的其实可以不用初始化应为我写的呢个创建链队的函数已经初始化过了

2.可以自动取号码(从  A000  开始)并入队

3.打印链队中所有元素但不出队

4.叫号服务(出队)

5.删除指定位置数据

6.销毁链队

7.求当前序号前面的排队人数                //求当前结点前面的结点数

 

 

 

一、链队基本 *** 作

#include
#include
#include
#include
#include

typedef char ElemType;

typedef struct qnode
{
    ElemType data[5];
    struct qnode* next;
}QNode;

typedef struct
{
    QNode* front;
    QNode* rear;
}LQueue;


//创建链队
LQueue* createLQueue()
{
    LQueue* q=(LQueue*)malloc(sizeof(LQueue));
    q->front=q->rear=NULL;
    return q;
}


//创建链队结点
QNode* createQNode(ElemType data[5])
{
    QNode* newNode=(QNode*)malloc(sizeof(QNode));
    strcpy(newNode->data,data);
    newNode->next=NULL;
    return newNode;
}

//入队
void EnQueue(LQueue* q,ElemType data[5])
{
    QNode* newNode=createQNode(data);
    if(q->front==NULL)
        q->front=newNode;
    else
        q->rear->next=newNode;
    q->rear=newNode;
}


//出队
void DeleteLQueue(LQueue* q)
{
    if(q->front==q->rear)
    {
        printf("队空无法出队\n");
    }
    QNode* nextNode=q->front->next;
    free(q->front);
    q->front=nextNode;
}

 1、初始化

///1 初始化银行排队队列
//初始化链队
void InitLQueue(LQueue* q)
{
    q->front=q->rear=NULL;
}

2、取号进队       (进队 *** 作在主函数case呢快)

//自动排号
void getNumber(LQueue* q,ElemType ch[5])
{
    if(q->rear==NULL)
    {
        ElemType arr[5]="A000";
        strcpy(ch,arr);
    }
    else
    {
        strcpy(ch,q->rear->data);
        if(ch[3]<'9')
        {
            ch[3]+=1;
        }
        else if(ch[2]<'9')
        {
            ch[3]='0';
            ch[2]+=1;
        }
        else if(ch[1]<'9')
        {
            ch[3]='0';
            ch[2]='0';
            ch[1]+=1;
        }
        else
        {
            ch[3]='0';
            ch[2]='0';
            ch[1]='0';
            ch[0]+=1;
        }
    }
    printf("您的排队号码为:\t\t%s\n\n",ch);
}

 3、排队等候

///3 排队等候
//个人理解就是打印一下在排队的人啊
void printLQueue(LQueue* q)
{
    LQueue* p=createLQueue();       //存储原来位置
    p->front=q->front;
    printf("\n当前在排队等候人为:\n");
    while(q->front)
    {
        printf("------------------%s\n",q->front->data);
        q->front=q->front->next;
    }
    q->front=p->front;              //遍历完后再返回初始位置
    printf("\n");
}

4、叫号服务

///4 叫号服务
//个人理解就是出队啊
int callNmeber(LQueue* q)
{
    if(q->front==NULL)
    {
        printf("暂无人\n");
        return 0;
    }
    printf("叫到的号为:%s\n",q->front->data);
    DeleteLQueue(q);

}

 5、不再排队

///5 不再排队
//个人理解就是指定位置删除啊
int DelByAppoint(LQueue* q)
{
    if(q->front==q->rear)
    {
        printf("队空无法 *** 作\n");
        return 0;
    }
    QNode* pMove=q->front;
    ElemType arr[5];
    printf("请输入不再排队人的号码:");
    scanf("%s",&arr);
    if(!Judge(q,arr))
    {
        printf("输入的号码有误\n");
    }
    else
    {
        while(1)
        {
            if(strcmp(arr,pMove->data)==0)
            {
                DeleteLQueue(q);
                printf("%s已退出排队\n",arr);
                break;
            }
            else
            {
                if(strcmp(arr,pMove->next->data)==0)
                {
                    QNode* nextNode;
                    nextNode=pMove->next;
                    pMove->next=nextNode->next;
                    free(nextNode);
                    printf("%s已退出排队\n",arr);
                    break;
                }
            }
            pMove=pMove->next;
        }
    }
}

 6、下班后

///6 下班后
//销毁队列
void Destory(LQueue *q)
{
    free(q->front);
    free(q);
    q=NULL;
    printf("已下班不能办理业务!!!!!\n");
    exit(0);
}

7、 求当前序号前面的排队人数

///7 求当前序号前面的排队人数
void getnum(LQueue* q)
{
    QNode* pMove=q->front;
    ElemType arr[5];
    int count=0;        //计数
    printf("请输入当前序号:");
    scanf("%s",&arr);
    if(!Judge(q,arr))
    {
        printf("输入号码有误\n");
    }
    else
    {
        while(strcmp(arr,pMove->data)!=0)
        {
            count++;
            pMove=pMove->next;
        }
        printf("当前序号前面排队人数为:%d\n",count);
    }
}

 看你滑这么厉害  那就源码随便看了啊

#include
#include
#include
#include
#include

typedef char ElemType;

typedef struct qnode
{
    ElemType data[5];
    struct qnode* next;
}QNode;

typedef struct
{
    QNode* front;
    QNode* rear;
}LQueue;


//创建链队
LQueue* createLQueue()
{
    LQueue* q=(LQueue*)malloc(sizeof(LQueue));
    q->front=q->rear=NULL;
    return q;
}


//创建链队结点
QNode* createQNode(ElemType data[5])
{
    QNode* newNode=(QNode*)malloc(sizeof(QNode));
    strcpy(newNode->data,data);
    newNode->next=NULL;
    return newNode;
}
///1 初始化银行排队队列
//初始化链队
void InitLQueue(LQueue* q)
{
    q->front=q->rear=NULL;
}

//入队
void EnQueue(LQueue* q,ElemType data[5])
{
    QNode* newNode=createQNode(data);
    if(q->front==NULL)
        q->front=newNode;
    else
        q->rear->next=newNode;
    q->rear=newNode;
}


//出队
void DeleteLQueue(LQueue* q)
{
    if(q->front==q->rear)
    {
        printf("队空无法出队\n");
    }
    QNode* nextNode=q->front->next;
    free(q->front);
    q->front=nextNode;
}

///2 取号进队
//自动排号
void getNumber(LQueue* q,ElemType ch[5])
{
    if(q->rear==NULL)
    {
        ElemType arr[5]="A000";
        strcpy(ch,arr);
    }
    else
    {
        strcpy(ch,q->rear->data);
        if(ch[3]<'9')
        {
            ch[3]+=1;
        }
        else if(ch[2]<'9')
        {
            ch[3]='0';
            ch[2]+=1;
        }
        else if(ch[1]<'9')
        {
            ch[3]='0';
            ch[2]='0';
            ch[1]+=1;
        }
        else
        {
            ch[3]='0';
            ch[2]='0';
            ch[1]='0';
            ch[0]+=1;
        }
    }
    printf("您的排队号码为:\t\t%s\n\n",ch);
}

///3 排队等候
//个人理解就是打印一下在排队的人啊
void printLQueue(LQueue* q)
{
    LQueue* p=createLQueue();       //存储原来位置
    p->front=q->front;
    printf("\n当前在排队等候人为:\n");
    while(q->front)
    {
        printf("------------------%s\n",q->front->data);
        q->front=q->front->next;
    }
    q->front=p->front;              //遍历完后再返回初始位置
    printf("\n");
}

///4 叫号服务
//个人理解就是出队啊
int callNmeber(LQueue* q)
{
    if(q->front==NULL)
    {
        printf("暂无人\n");
        return 0;
    }
    printf("叫到的号为:%s\n",q->front->data);
    DeleteLQueue(q);

}

int Judge(LQueue* q,ElemType arr[5])
{
    QNode* pMove=(QNode*)malloc(sizeof(QNode));
    pMove=q->front;
    while(pMove)
    {
        if(strcmp(pMove->data,arr)==0)
            return 1;
        else if(pMove==NULL)
            return 0;
        pMove=pMove->next;
    }
}



///5 不再排队
//个人理解就是指定位置删除啊
int DelByAppoint(LQueue* q)
{
    if(q->front==q->rear)
    {
        printf("队空无法 *** 作\n");
        return 0;
    }
    QNode* pMove=q->front;
    ElemType arr[5];
    printf("请输入不再排队人的号码:");
    scanf("%s",&arr);
    if(!Judge(q,arr))
    {
        printf("输入的号码有误\n");
    }
    else
    {
        while(1)
        {
            if(strcmp(arr,pMove->data)==0)
            {
                DeleteLQueue(q);
                printf("%s已退出排队\n",arr);
                break;
            }
            else
            {
                if(strcmp(arr,pMove->next->data)==0)
                {
                    QNode* nextNode;
                    nextNode=pMove->next;
                    pMove->next=nextNode->next;
                    free(nextNode);
                    printf("%s已退出排队\n",arr);
                    break;
                }
            }
            pMove=pMove->next;
        }
    }
}



///6 下班后
//销毁队列
void Destory(LQueue *q)
{
    free(q->front);
    free(q);
    q=NULL;
    printf("已下班不能办理业务!!!!!\n");
    exit(0);
}

///7 求当前序号前面的排队人数
void getnum(LQueue* q)
{
    QNode* pMove=q->front;
    ElemType arr[5];
    int count=0;        //计数
    printf("请输入当前序号:");
    scanf("%s",&arr);
    if(!Judge(q,arr))
    {
        printf("输入号码有误\n");
    }
    else
    {
        while(strcmp(arr,pMove->data)!=0)
        {
            count++;
            pMove=pMove->next;
        }
        printf("当前序号前面排队人数为:%d\n",count);
    }
}



void menu()
{
    printf("****************************\n");
    printf("******银行排队叫号系统******\n");
    printf("*\t0.退出系统         *\n");
    printf("*\t1.初始化           *\n");
    printf("*\t2.取号             *\n");
    printf("*\t3.查看排队等候人   *\n");
    printf("*\t4.叫号             *\n");
    printf("*\t5.退出排队         *\n");
    printf("*\t6.销毁             *\n");
    printf("*\t7.查看前面人数     *\n");
    printf("****************************\n");
    printf("\n----------------------------------\n\n");
}

static ElemType gn[5];      //用于自动取号
int main()
{
    LQueue* myqueue=createLQueue();
    int choice;
    menu();
    while(1)
    {
        printf("请选择所需要的 *** 作(1.2.....):");
        scanf("%d",&choice);
        getchar();
        printf("\n");
        switch(choice)
        {
            case 0:system("cls"),printf("系统已退出\n"),exit(0);break;
            case 1:InitLQueue(myqueue);break;
            case 2:getNumber(myqueue,gn),EnQueue(myqueue,gn);break;
            case 3:printLQueue(myqueue);break;
            case 4:callNmeber(myqueue);break;
            case 5:DelByAppoint(myqueue);break;
            case 6:Destory(myqueue);break;
            case 7:getnum(myqueue);break;
            default:printf("无该选项\n");break;
        }
    }
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/567680.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存