zoukankan      html  css  js  c++  java
  • C语言实现常用数据结构——队列

    #include<stdio.h>
    #include<stdlib.h>
    #define MAX_SIZE 10
    /* 用一个动态数组来实现队列 */
    
    typedef struct Queue {
        int Capacity;
        int Front;
        int Rear;
        int Size;
        int data[MAX_SIZE];
    } Queue;
    
    void Error(char *error) {
        printf("%s",error);
    }
    
    void FatalError(char *fatalerror) {
        printf("%s",fatalerror);
    }
    
    int IsEmpty(Queue *Q) {
        return Q->Size == 0;
    }
    
    int IsFull(Queue *Q) {
        return Q->Size == Q->Capacity;
    }
    
    void Init( Queue *Q ) {
        Q->Size = 0;
        Q->Front = 1;
        Q->Rear = 0;
        Q->Capacity = MAX_SIZE;
    }
    
    
    static int Succ(int value,Queue *Q) {
        if(++value == Q->Capacity) {
            value = 0;
        }
        return value;
    }
    
    
    void Enqueue(int X,Queue *Q) {
        if( IsFull( Q ) )
            FatalError("Full queue");
        else {
            Q->Size++;
            Q->Rear = Succ(Q->Rear,Q);
            Q->data[ Q->Rear ] = X;
        }
    }
    
     
    void Dequeue(Queue *Q) {
        if(IsEmpty(Q))
            FatalError("Empty queue");
        else {
            Q->Size--;
            Q->Front = Succ(Q->Front,Q);
        }
    }
    
    
    int FrontAndDequeue(Queue *Q) {
        int Tmp;
        if(IsEmpty(Q))
            Error("Empty queue");
        else {
            Q->Size--;
            Tmp = Q->data[Q->Front];
            Q->Front = Succ(Q->Front,Q);
            return Tmp;
        }
    }
    
    
    void DisposeQueue( Queue *Q ) {
        free(Q->data);
        free(Q);
    }
    
    
    
    main() {
        Queue *q ;
        Init(q);
        int i;
        for(i = 0; i <MAX_SIZE; i++) {
            Enqueue(i,q);
        }
        for(i = 0; i <MAX_SIZE; i++) {
            printf("%d
    ",FrontAndDequeue(q));
        }
    }
  • 相关阅读:
    反射 元类
    多态
    封装
    继承
    面向基础
    包 logging模块 hashlib模块 openpyxl 深浅拷贝
    常用模块
    re模块(正则表达式)
    模块 导入方式 软件开发目录规范
    第 3 章 镜像
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/9593675.html
Copyright © 2011-2022 走看看