zoukankan      html  css  js  c++  java
  • 2,数组模拟队列

    实现思路:

    1)front=-1指向队列头前一位置,rear=-1指向队列尾,maxSize初始化队列最大容量

    2)当rear<maxSize-1 表示队列还未满,允许继续在队列末尾添加rear++;

    3)当front==rear表示队列为空

    C#实现代码:

     1 using System;
     2 
     3 namespace 数据结构
     4 {
     5     public class ArrayQueue
     6     {
     7         private int maxSize;//队列最大值        
     8         private int front;//队列头
     9         private int rear;//队列尾
    10         private int[] arrayQueue;//模拟队列数组
    11         //初始化队列
    12         public ArrayQueue(int maxSize = 1)
    13         {
    14             this.maxSize = maxSize;
    15             this.arrayQueue = new int[maxSize];
    16             this.front = -1;//指向队列头前一个位置
    17             this.rear = -1;//指向队列尾,队列最后一个位置
    18         }
    19 
    20         //判断队列是否已满
    21         public bool IsFull()
    22         {
    23             return rear == maxSize - 1;
    24         }
    25         public bool IsEmpty()
    26         {
    27             return front == rear;
    28         }
    29 
    30         //入队
    31         public bool AddQueue(int item)
    32         {
    33             //队列已满
    34             if (IsFull())
    35             {
    36                 Console.WriteLine("队列已满...");
    37                 return false;
    38             }
    39             rear++;
    40             arrayQueue[rear] = item;
    41             return true;
    42         }
    43 
    44         //出队
    45         public int GetQueue()
    46         {
    47             //队列为空
    48             if (IsEmpty())
    49             {
    50                 throw new IndexOutOfRangeException("队列为空...");
    51             }
    52             front++;
    53             return arrayQueue[front];
    54         }
    55     }
    56 
    57     public class ArrayQueueDemo
    58     {
    59         static void Main(string[] args)
    60         {
    61             //初始化队列
    62             var queue = new ArrayQueue(5);
    63             try
    64             {
    65                 queue.GetQueue();
    66             }
    67             catch
    68             {
    69                 Console.WriteLine("队列还是空的呢...
    ");
    70             }
    71             Console.WriteLine("开始入队...
    ");
    72             for (int i = 1; i <= 6; i++)
    73             {
    74                 queue.AddQueue(i);
    75             }
    76             Console.WriteLine("
    开始出队...
    ");
    77             for (int i = 1; i < 6; i++)
    78             {
    79                 Console.WriteLine(queue.GetQueue());
    80             }
    81             try
    82             {
    83                 queue.GetQueue();
    84             }
    85             catch
    86             {
    87                 Console.WriteLine("全部出队了哦...");
    88             }
    89         }
    90     }
    91 }

  • 相关阅读:
    查询内容在网页里面分页显示+跳页查看
    struts2——通配符
    JavaScript 输出
    oracl函数
    oracle基本用法
    Java之线程处理之二
    java之多线程之一/序列化和反序列化
    错题整理之二
    S2结业考试的第一次测验
    cookie与session的爱恨情仇
  • 原文地址:https://www.cnblogs.com/xiaojvhuang/p/12677479.html
Copyright © 2011-2022 走看看