zoukankan      html  css  js  c++  java
  • 数据结构 静态队列笔记

    /*
    队列:一种先进先出的线性表,它只允许在表的一端插入元素,另一端
    删除元素,其中插入元素的一端叫做队尾,删除元素的一端叫做对头。
     
    静态队列——使用一维数组储存队列中的元素,一个指针(front)指向
    队头,一个指针(rear)指向队尾。 
    使用的数组必须是循环数组
    */
    
    # include <stdio.h>
    # include <stdlib.h>
    # include <malloc.h>
    
    #define LEN 6  // 定义队列长度 
    
    // 说明:此循环队列使用空出一个位置的方法解决队列空与满的问题 
    typedef struct QUEUE
    {
        int * pBase;       // 用于作为循环数组 
        int front;       // 用于标记队列头 
        int rare;        // 用于标记列尾 
    }Queue, * pQ; 
    
    // 创建一个静态队列 
    void Create_Queue(pQ queue, int len); 
    // 静态队列初始化
    void Init_Queue(pQ queue);
    // 遍历整个队列
    void Travel_Queue(pQ queue); 
    // 判断队列是否满 
    bool Is_Full(pQ queue);
    // 判断队列是否为空
    bool Is_Empty(pQ queue); 
    // 入队
    void En_Queue(pQ queue, int val);
    // 出队,并将出队的数据返还给主函数 
    void Out_Queue(pQ queue, int * val); 
    // 在队列中查找某元素,并返回该位置 
    int Search_InQueue(pQ queue, int val);
    // 计算此时队列长度 
    int Length_Queue(pQ queue);
    // 销毁整个队列
    void Destory_Queue(pQ queue); 
    
    int main(void)
    {
        Queue queue;
        Create_Queue(&queue, LEN);
        Init_Queue(&queue);
        En_Queue(&queue, 1);
        En_Queue(&queue, 2);
        En_Queue(&queue, 3);
        En_Queue(&queue, 4);
        Travel_Queue(&queue);
        
        int len = Length_Queue(&queue);
        printf("队列长度为:%d
    ", len);
        
        int val;
        Out_Queue(&queue, &val);
        printf("出队的元素是:%d
    ", val);
        
        Search_InQueue(&queue, 5);
        
        Destory_Queue(&queue);
        
        return 0;
    }
    
    void Create_Queue(pQ queue, int len)
    { 
        queue->pBase = (int *)malloc(len * (sizeof(int)));
        if (NULL == queue->pBase)
        {
            printf("动态内存分配失败!
    ");
            exit(-1);
        } 
        
        return;
    } 
    
    void Init_Queue(pQ queue)
    {
        queue->front = 0;
        queue->rare = 0;
        
        return;
    }
    
    void Travel_Queue(pQ queue)
    {
        int pos;
        for (pos = queue->front; pos != queue->rare; pos = (pos+1) % LEN)
        {
            printf("%-5d", queue->pBase[pos]);
        }
        printf("
    ");
        
        return;
    }
    
    bool Is_Full(pQ queue)
    {
        if ( (queue->rare + 1) % LEN == queue->front )
            return true;
        else
            return false;
    }
    
    bool Is_Empty(pQ queue)
    {
        if (queue->front == queue->rare)
            return true;
        else
            return false;
    }
    
    void En_Queue(pQ queue, int val)
    {
        if ( Is_Full(queue) )
        {
            printf("该队列已满!
    ");
            exit(-1);
        }
        queue->pBase[queue->rare] = val;
        queue->rare = (queue->rare + 1) % LEN;
        
        return;
    }
    
    void Out_Queue(pQ queue, int * val)
    {
        if ( Is_Empty(queue) )
        {
            printf("该队列为空!
    ");
            exit(-1);
        }
        *val = queue->pBase[queue->front];
        queue->front = (queue->front + 1) % LEN;
        
        return;
    }
    
    int Search_InQueue(pQ queue, int val)
    {
        int pos;
        for (pos = queue->front; pos != queue->rare; pos = (pos+1) % LEN)
        {
            if (queue->pBase[pos] == val)
            {
                return pos;
            }
        }
        printf("该元素不在此队列!
    ");
        
        return -1;
    }
    
    int Length_Queue(pQ queue)
    {
        int pos, cnt = 0;
        for (pos = queue->front; pos != queue->rare; pos = (pos+1) % LEN)
        {
            ++cnt;
        }
        
        return cnt;
    }
    
    void Destory_Queue(pQ queue)
    {
        free(queue->pBase);
        queue->pBase = NULL;
        
        return;
    }
  • 相关阅读:
    js中的原生Ajax和JQuery中的Ajax
    this的用法
    static的特性
    时政20180807
    java compiler没有1.8怎么办
    Description Resource Path Location Type Java compiler level does not match the version of the installed Java project facet Unknown Faceted Project Problem (Java Version Mismatch)
    分词器
    [数算]有一个工程甲、乙、丙单独做,分别要48天、72天、96天完成
    一点感想
    解析Excel文件 Apache POI框架使用
  • 原文地址:https://www.cnblogs.com/lnlin/p/6730920.html
Copyright © 2011-2022 走看看