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

实现任务:

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

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

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

4.叫号服务(出队)

5.删除指定位置数据

6.销毁链队

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

 

 

 一、链队基本操作

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<windows.h>

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<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<windows.h>

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;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zkflzl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值