zoukankan      html  css  js  c++  java
  • 队列

    目录

     

    队列的基本操作有初始化队列,判队列是否为空,入队,出队

    栈可分为两种存储结构:顺序队和链队。

    顺序队

    /* 顺序队结构 */
    typedef struct 
    {
    ElemType data[MAXSIZE];
    int front;
    int rear;
    } SqQueue;

    顺序队四个要素:

    (1)队空条件:qu.rear == qu.front;

    (2)队满条件: (qu.rear + 1) % MAXSIZE == qu.front;

    (3)进队条件: qu.rear = (qu.rear + 1) % MAXSIZE; qu.data[qu.rear] = data;

    (4)出队条件: qu.front = (qu.front + 1) % MAXSIZE; data = qu.data[qu.front];

    顺序队基本操作

    复制代码
    #include "stdafx.h"
    #include <stdlib.h>
    
    #define MAXSIZE 10
    
    typedef int ElemType;
    
    /* 顺序栈结构 */
    typedef struct 
    {
        ElemType data[MAXSIZE];
        int front;
        int rear;
    } SqQueue;
    
    void InitStack(SqQueue &qu)
    {
        qu.front = qu.rear = 0;
    }
    
    bool IsEmpty(SqQueue &qu)
    {
        return (qu.front == qu.rear);
    }
    
    int InQueue(SqQueue &qu, ElemType data)
    {
        if ((qu.rear + 1) % MAXSIZE == qu.front)
        {
            return -1;
        }
        
        qu.data[qu.rear] = data;
        qu.rear = (qu.rear+1) % MAXSIZE;
        
        return 0;
    }
    
    int OutQueue(SqQueue &qu, ElemType &data)
    {
        if (qu.front == qu.rear)
        {
            return -1;
        }
    
        qu.front = (qu.front + 1) % MAXSIZE;
        data = qu.data[qu.front];    
        return 0;
    }
    
    void PrintQueue(SqQueue &qu)
    {
        int i = 0;
        if (qu.front == qu.rear)
            return;
    
        i = qu.front;
        while (i != qu.rear)
        {
            printf("%d	", qu.data[i]);
            i = (i+1)%MAXSIZE;
        }
        printf("
    ");
        return;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
        ElemType array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int i = 0;
        int data = 0;
        SqQueue queue = { 0 };
    
        InitStack(queue);
        for (i = 0; i < sizeof(array)/sizeof(ElemType); i++)
        {
            InQueue(queue, array[i]);
        }
    
        PrintQueue(queue);
        OutQueue(queue, data);
        OutQueue(queue, data);
        PrintQueue(queue);
        InQueue(queue, 11);
        InQueue(queue, 12);
        PrintQueue(queue);
        return 0;
    }
    复制代码

    复制代码
    #include "stdafx.h"
    #include <stdlib.h>
    
    #define MAXSIZE 10
    
    typedef int ElemType;
    
    /* 顺序栈结构 */
    typedef struct 
    {
        ElemType data[MAXSIZE];
        int front;
        int rear;
    } SqQueue;
    
    void InitStack(SqQueue &qu)
    {
        qu.front = qu.rear = 0;
    }
    
    bool IsEmpty(SqQueue &qu)
    {
        return (qu.front == qu.rear);
    }
    
    int InQueue(SqQueue &qu, ElemType data)
    {
        if ((qu.rear + 1) % MAXSIZE == qu.front)
        {
            return -1;
        }
        
        qu.data[qu.rear] = data;
        qu.rear = (qu.rear+1) % MAXSIZE;
        
        return 0;
    }
    
    int OutQueue(SqQueue &qu, ElemType &data)
    {
        if (qu.front == qu.rear)
        {
            return -1;
        }
    
        qu.front = (qu.front + 1) % MAXSIZE;
        data = qu.data[qu.front];    
        return 0;
    }
    
    void PrintQueue(SqQueue &qu)
    {
        int i = 0;
        if (qu.front == qu.rear)
            return;
    
        i = qu.front;
        while (i != qu.rear)
        {
            printf("%d	", qu.data[i]);
            i = (i+1)%MAXSIZE;
        }
        printf("
    ");
        return;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
        ElemType array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int i = 0;
        int data = 0;
        SqQueue queue = { 0 };
    
        InitStack(queue);
        for (i = 0; i < sizeof(array)/sizeof(ElemType); i++)
        {
            InQueue(queue, array[i]);
        }
    
        PrintQueue(queue);
        OutQueue(queue, data);
        OutQueue(queue, data);
        PrintQueue(queue);
        InQueue(queue, 11);
        InQueue(queue, 12);
        PrintQueue(queue);
        return 0;
    }
    复制代码
  • 相关阅读:
    Kaggle & Machine Learning
    练习题目总结
    日常练习//算法类
    论文阅读
    codeforces专项
    10.26—11.1
    常见算法笔记
    DM/ML学习实践(一)
    Python常见代码
    HDU 4251 --- 主席树(划分树是正解)
  • 原文地址:https://www.cnblogs.com/aimei/p/12199915.html
Copyright © 2011-2022 走看看