美文网首页
栈和队列(二)

栈和队列(二)

作者: 风之旅人c | 来源:发表于2018-04-30 12:20 被阅读0次

队列

代码实现

#include<malloc.h> 
#include<stdio.h> 
#define OK 1
#define ERROR 0
typedef int Status;     // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int QElemType;
#define MAXQSIZE 100    // 最大队列长度(对于循环队列,最大队列长度要减1)

typedef struct
{
    QElemType *base;    // 初始化的动态分配存储空间
    int front;          // 头指针,若队列不空,指向队列头元素
    int rear;           // 尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;


// 构造一个空队列Q,该队列预定义大小为MAXQSIZE
Status InitQueue(SqQueue &Q)   
{
    Q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
    if(!Q.base) return ERROR;
    Q.rear = Q.front = 0;
    return OK;
}


// 插入元素e为Q的新的队尾元素
Status EnQueue(SqQueue &Q,QElemType e)  
{ 
    if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
    Q.base[Q.rear]=e;
    Q.rear=(Q.rear+1)%MAXQSIZE;
    return OK;
}


// 若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK; 否则返回ERROR
Status DeQueue(SqQueue &Q, QElemType &e) 
{  
    if(Q.front==Q.rear) return ERROR;
    e=Q.base[Q.front];
    Q.front=(Q.front+1)%MAXQSIZE;
    return OK;
}


// 若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR
Status GetHead(SqQueue Q, QElemType &e)
{   
    if(Q.front == Q.rear) return ERROR;
    e = Q.base[Q.front];
    return OK;
}


// 返回Q的元素个数
int QueueLength(SqQueue Q)  
{
    //return Q.rear - Q.front;
    //return Q.rear%MAXQSIZE-Q.front%MAXQSIZE
    return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}


// 若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.
Status QueueTraverse(SqQueue Q)  
{
    int i;
    i=Q.front;
    if(Q.rear == Q.front)printf("The Queue is Empty!");  
    else
    {
        printf("The Queue is: ");
        while(i != Q.rear)     //请填空
        {
            printf("%d ",Q.base[i] );  
            i = (i+1) % MAXQSIZE;  
        }
    }
    printf("\n");
    return OK;
}

int main()
{
    int a;
    SqQueue S;
    QElemType x, e;
    if(InitQueue(S))    // 判断顺序表是否创建成功
    {
        printf("A Queue Has Created.\n");
    }
    while(1)
    {
        printf("1:Enter \n2:Delete \n3:Get the Front \n4:Return the Length of the Queue\n5:Load the Queue\n0:Exit\nPlease choose:\n");
        scanf("%d",&a);
        switch(a)
        {
            case 1: 
                scanf("%d", &x);
                if(!EnQueue(S, x)) printf("Enter Error!\n"); // 判断入队是否合法
                else printf("The Element %d is Successfully Entered!\n", x); 
                break;
            case 2: 
                if(!DeQueue(S, e)) printf("Delete Error!\n"); // 判断出队是否合法
                else printf("The Element %d is Successfully Deleted!\n", e);
                break;
            case 3: 
                if(!GetHead(S, e))printf("Get Head Error!\n"); // 判断Get Head是否合法
                else printf("The Head of the Queue is %d!\n", e);
                break;
            case 4: 
                printf("The Length of the Queue is %d!\n", QueueLength(S));
                break;
            case 5: 
                QueueTraverse(S);
                break;
            case 0: 
                return 1;
        }
    }
}

相关文章

  • 数据结构——栈和队列

    用数组实现栈和队列 用栈实现队列 用队列实现栈 栈和队列的经典算法题最小间距栈宠物收养所 数组实现栈和队列 用数组...

  • 栈和队列

    用栈定义队列(出入栈) 用队列定义栈(数据队列和辅助队列)

  • 算法导论 基本数据结构

    MIT公开课没有讲到的内容,介绍几种基本数据结构- 栈和队列- 链表- 二叉树 栈和队列 栈和队列都是动态集合,元...

  • Algorithm小白入门 -- 队列和栈

    队列和栈队列实现栈、栈实现队列单调栈单调队列运用栈去重 1. 队列实现栈、栈实现队列 队列是一种先进先出的数据结构...

  • 栈和队列

    栈和队列 本质上是稍加限制的线性表 栈和队列定义 栈顺序栈定义 链栈结点定义 队列顺序队列 链队列链队类型定义 链...

  • 栈和队列(二)

    队列 代码实现

  • 二、栈和队列

    二、栈和队列 栈和队列都是线性结构,它们是操作受限的线性表,即它们的操作是线性表操作的子集。因此也可以用线性表在某...

  • Python实现栈和队列以及使用list模拟栈和队列

    Python实现栈和队列 Python使用list模拟栈和队列

  • 算法-栈和队列算法总结

    栈和队列算法总结 1 模拟 1.1 使用栈实现队列 1.2 使用队列实现栈 2 栈的应用 2.1 栈操作 2.2 ...

  • 五、栈和队列(二)、队列

    数据结构目录 1.定义 队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。 与栈相反,...

网友评论

      本文标题:栈和队列(二)

      本文链接:https://www.haomeiwen.com/subject/yhpzlftx.html