队列操作--出队入队

来源:互联网 发布:伦敦黄金交易软件 编辑:程序博客网 时间:2024/04/29 23:06
/// : 假设以数组sequ[m]存放循环队列的元素,同时设变量/// rear 和quelen 分别指示循环队列中队尾元素的位置和内含元素的个数。/// 试给出判别此循环队列的队满条件,并写出相应的入队列和出队列的算法。/// 说明:本程序之所以不用typedef 把char定义成datatype,是因为:/// 在C语言中没有输入输出的重构函数,所以即使把char定义成datatype,/// 在处理输入输出的时候仍然要面对众多的 %d、%c之类,反而容易引起错误/// 因此,为了使问题更加便于理解,采用了char而不是datatype。#include <stdio.h>#include <stdlib.h>#include <conio.h>#define MAXSIZE  10typedef struct sequeue{        char sequ[MAXSIZE];        int rear;        int quelen;}sequeue;// 显示队列的函数void PrintQueue(sequeue* head){        if(head == NULL || head->quelen <= 0)        {        printf("这是一个空列表。\n");        return;        }        for(int i = 0; i < head->quelen; i++)        {                printf("%c", head->sequ[i //                       (head->rear - head->quelen + i + 1 + MAXSIZE) % MAXSIZE                                                                ]);                //注意这句,在纸上画一下比较好理解///i就可以了        }        printf("\n");        return;}//判断队列是否满的函数bool IsFull(sequeue* head){        if (head == NULL)        {                printf("队列不存在!\n");                //返回真,防止在非法队列中进行的入队操作                //因为一般情况下不会对一个满队列进行入队                return true;        }        if (head->quelen == MAXSIZE)        {                return true;        }        else        {                return false;        }}//入队函数char* EnQueue(sequeue* head, char data){        printf("入队:%c\t\t", data);        if (IsFull(head))        {                printf("入队失败!\n");                return NULL;        }        if (head->rear + 1 >= MAXSIZE)        {                head->rear -= MAXSIZE;        }        head->rear += 1;        head->sequ[head->rear ] = data;  //      head->rear += 1;        head->quelen += 1;        PrintQueue(head);        return &(head->sequ[head->rear - 1]);}//出队函数char DeQueue(sequeue* head){        printf("出队:");        if (head->quelen <= 0)        {                printf("出队失败!\n");                return -1;        }        int tempdata = head->sequ[1//(MAXSIZE + head->quelen - head->rear + 1) % MAXSIZE];        head->quelen -= 1;        if(head->rear >= MAXSIZE)        {                head->rear -= MAXSIZE;        }        printf("%c\t\t", tempdata);        PrintQueue(head);        return tempdata;}//带提示的出队函数char DeQueueWithData(sequeue* head){        char temp = DeQueue(head);        if(temp != -1)        {                printf("%c已经出队\n", temp);        }        return temp;}//带提示的入队函数void EnQueueWithData(sequeue* head){        char temp;        printf("请输入一个字母:");        temp = getche();        printf("\n");        EnQueue(head, temp);}//队列初始化函数void InitialQueue(sequeue* head){        head->quelen = 0;        head->rear = MAXSIZE - 1;        for(int i = 0; i < MAXSIZE; i ++)        {                head->sequ[i] = 0;        }}//输入字符串的函数sequeue* InputList(void){        sequeue* head = NULL;        head = (sequeue*)malloc(sizeof(sequeue));        if (head == NULL)        {                printf("内存分配出错!\n");                return NULL;        }        InitialQueue(head);        char ch = ' ';        printf("请输入一个字符串:\n");        ch = getchar();        while(ch != '\n' && !IsFull(head))        {                EnQueue(head, ch);                ch = getchar();        }        return head;}int main(int argc, char* argv[]){        sequeue* head = NULL;        char ch = 'y';        head = InputList();        PrintQueue(head);        while(ch != 27)        {                printf("请输入E入队,D出队,ESC退出\n");                ch = getch();                switch(ch)                {                case 'd':                case 'D':                        DeQueueWithData(head);                        break;                case 'e':                case 'E':                        EnQueueWithData(head);                        break;                default:                        break;                }        }        return 0;}

0 0
原创粉丝点击