栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

不带尾指针的循环队列的基本操作(C语言实现)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

不带尾指针的循环队列的基本操作(C语言实现)

问题描述:

该队列只有一个队头指针front,不设队尾指针rear,而设置一个计数器count用以记录队列中结点的个数。编写算法实现队列的

基本运算:判空、入队和出队等。

基本要求及提示:

(1)从键盘输入字符以$结束,建立循环队列,并显示结果。

(2)从键盘输入1个元素,执行入队操作,并显示结果。

(3)将队头元素出队,并显示结果。

(4)判断队列是否为空。

(5)判断队列是否为满。

要求程序通过一个主菜单进行控制,在主菜单界面通过选择菜单项的序号来调用各功能函数。

代码实现

#include

#include 

#define TRUE 0

#define FALSE 1

#define MAXSIZE 5

int count = 0;

//结构体定义

typedef struct {

    int data[MAXSIZE];

    int front;

} CycQueue;

//函数声明

int menu_select();

void InitQueue(CycQueue **);

int EnQueue(CycQueue *, int);

void EnterCycQueue(CycQueue *);

int DeCycQueue(CycQueue *, int *);

void DeleteCycQueue(CycQueue *);

int IFCycQueueEmpty();

int IFCycQueueFull();

void PrintCycQueue(CycQueue *);

// main()函数

int main() {

    CycQueue *Q;

    InitQueue(&Q);

    for (;;) {

        switch (menu_select()) {

        case 1:

            printf("1.循环队列的入队n");

            EnterCycQueue(Q);

            break;

        case 2:

            printf("2.循环队列的出队n");

            DeleteCycQueue(Q);

            break;

        case 3:

            printf("3.循环队列的判空n");

            IFCycQueueEmpty();

            break;

        case 4:

            printf("4.循环队列的判满n");

            IFCycQueueFull();

            break;

        case 5:

            printf("5.循环队列的输出n");

            PrintCycQueue(Q);

            break;

        case 0:

            printf("再见!n");

            free(Q);

            return 0;

        }

    }

} // main()函数结束

//菜单驱动程序

int menu_select()   //菜单

{

    int sn;

    printf("    欢迎来到队列管理系统!n");

    printf("===========================n");

    printf("      1.循环队列的入队n");

    printf("      2.循环队列的出队n");

    printf("      3.循环队列的判空n");

    printf("      4.循环队列的判满n");

    printf("      5.循环队列的输出n");

    printf("      0.退出系统n");

    printf("===========================n");

    printf("请输入你的选择(0~5):");

    for (;;) {

        scanf("%d", &sn);

        getchar();

        if (sn < 0 || sn > 5)

            printf("n您的选择有误,请重新选择(0~5):");

        else

            break;

    }

    return sn;

}

void InitQueue(CycQueue **Q) {

    *Q=(CycQueue *) malloc(sizeof(CycQueue));

    (*Q)->front=0;

}

int EnQueue(CycQueue *q, int e) {

    q->data[count]=e;

    count++;

}

void EnterCycQueue(CycQueue *q) {

    int i, e;

    char end;

    for (;;) {

        if (count == MAXSIZE) {

            printf("队列上溢出!n");

        } else{

            e=0;

            printf("请输入e的值:");

            scanf("%d", &e);

        }

        if (getchar() != '$') {

            EnQueue(q,e);

            for (i = 0; i < count; i++)

                printf("%4d", q->data[i]);

            printf(">>>队列中元素数:%dn", count);

        } else {

            break;

        }

    }

    return;

}

int DeCycQueue(CycQueue *q, int *e) {

    if(0==count)return FALSE;

    else{

        *e=q->data[q->front];

        q->front++;

        count--;

        return TRUE;

    }

}

void DeleteCycQueue(CycQueue *q) {

    int i, j, e, rear;

    if (q->front == count) {

        printf("队列为空!n");

        return;

    }

    rear = count;

    for (i = count; i > 0; i--) {

        if (0 == count) {

            printf("队空下溢出!n");

            q->front = 0;

            return;

        } else {

            DeCycQueue(q, &e);

            printf("出对元素:%dn", e);

        }

        if (q->front != rear)

            printf("队列中的元素:");

        for (j = q->front; j < rear; j++) {

            printf("%4d", q->data[j]);

        }

        printf("n");

    }

    return;

}

//判断队列是否为空

int IFCycQueueEmpty() {

    if ( count==0) {  //TODO:添加条件

        printf("队列为空!n");

        return FALSE;

    } else {

        printf("队列非空!n");

        return TRUE;

    }

}

//判断队列是否为满

int IFCycQueueFull() {

    if (count>=MAXSIZE) { //TODO:添加条件

        printf("队列已满!n");

        return FALSE;

    } else {

        printf("队列非满!n");

        return TRUE;

    }

}

//输出

void PrintCycQueue(CycQueue *q) {

    int i;

    if (0 == count) {

        printf("队列为空!n");

        return;

    } else {

        for (i = 0; i < count; i++) {

            printf("%4d", q->data[i]);

        }

        printf(">>>队列中的元素数:%dn", count);

    }

}

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1015469.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号