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;
    }
    View Code
  • 相关阅读:
    markdown===在新窗口中打开网址的解决办法,以及其他遗留问题!
    关于比特币
    给windows设置隐藏文件夹的方法
    python实战===百度文字识别sdk
    python实战===用python对比两张图片的不同
    python基础===python os.path模块
    python实战===爬取所有微信好友的信息
    python基础===Sublime Text 3 快捷键
    python实战===一键刷屏
    linux===进程操作
  • 原文地址:https://www.cnblogs.com/jingmoxukong/p/3782825.html
Copyright © 2011-2022 走看看