zoukankan      html  css  js  c++  java
  • 数据结构队列的各种操作

    #include"stdio.h"  
    #include"stdlib.h"  
    typedef int ElementType;  
    typedef struct Queue  
    {  
        int rear,front;  
        ElementType *elements;  
        int MaxSize;  
    }Queue;  
    void InitQueue(Queue *Q,int sz)//初始化  
    {  
        Q->MaxSize=sz;  
        Q->elements=(ElementType *)malloc(sizeof(ElementType)*Q->MaxSize);  
        Q->front=Q->rear=0;  
    }  
    void freeQueue(Queue *Q,int sz)//释放空间  
    {  
        free(Q->elements);  
    }  
    void MakeEmpty(Queue *Q)//置空  
    {  
        Q->front=Q->rear=0;  
    }  
    int Length(Queue *Q)//返回长度  
    {  
        return (Q->rear-Q->front+Q->MaxSize)%(Q->MaxSize);  
    }  
    int IsFull(Queue *Q)//判断是否为满  
    {  
        if (Q->rear!=0&&(Q->front==(Q->rear)%(Q->MaxSize)))  
            return 1;  
        else 
            return 0;  
    }  
    int IsEmpty(Queue *Q)//判断是否为空  
    {  
        if(Q->front==Q->rear)  
            return 1;  
        else 
            return 0;  
    }  
    void EnQueue(Queue *Q)//进队  
    {   ElementType item;  
        scanf("%d",&item);  
        while((!IsFull(Q))&&(item!=-1))  
        {  
            Q->elements[Q->rear]=item;  
            Q->rear=(Q->rear+1)%(Q->MaxSize);  
            scanf("%d",&item);  
        }  
    }  
    ElementType DeQueue(Queue *Q)  
    {  
        ElementType item;  
        if(!IsEmpty(Q))  
        {  
            item=Q->elements[Q->front];  
            Q->front=(Q->front+1)%(Q->MaxSize);  
            return item;  
        }  
        else 
        {  
            printf("对空!
    ");  
            exit(1);  
        }  
    }  
    ElementType GetFront(Queue *Q)  
    {  
        if(!IsEmpty(Q))  
            return Q->elements[Q->front];  
        else 
        {  
            printf("队空!
    ");  
            exit(1);  
        }  
    }  
    void main()  
    {  
        int i;  
        Queue Q;  
        InitQueue(&Q,10);  
        EnQueue(&Q);  
        for(i=0;i<10;i++)  
        if(!IsEmpty(&Q))  
        printf("%-5d",DeQueue(&Q));  
    } 
    

      

    本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/817866

  • 相关阅读:
    GitHub Interesting Collection
    使用 CSS3 Flexible Boxes 布局
    消失的属性
    浅谈 JavaScript 模块化编程
    为你的 Javascript 加点咖喱
    软件测试
    osi七层模型
    3_Hydra(爆破神器)
    2_NC(瑞士军刀)
    1_HTTP协议详解
  • 原文地址:https://www.cnblogs.com/umgsai/p/3908156.html
Copyright © 2011-2022 走看看