数组运用之 队列

using System;
using System.Collections.Generic;
using System.Text;

namespace Queue
{
    class QueueTest//队列类
    {
        int[] array;
        int length = 0;//保存元素个数,初始化为0
        public QueueTest(int num) {
            array = new int[num];//数组分配空间
        }
        public int Length {//只读属性读取当前队列的元素个数

            get {
                return this.length;
            }
        
        }

        public void Push(int num) { //数据进队列方法

            if (length > 0)
            {
                for (int i = length - 1; i >= 0; i--)
                {

                    array[i+1] = array[i];
                }

            }
            array[0] = num;
           
            length++;
        }

        public int Pop() {

            //return array[--length];
            return array[--length];
        }

    }

    class Test {
        static void Main(string[] args) {
            
            QueueTest qt = new QueueTest(100);
            
            qt.Push(1);//往队列里放入几个数
            qt.Push(2);
            qt.Push(3);
            qt.Push(4);
            
            while (qt.Length > 0) {//取出所有数

                Console.Write(qt.Pop() + "\t");
            
           }
            Console.Read();
        
        }
    
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值