首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >数据结构 From Zero To Hero(五)

数据结构 From Zero To Hero(五)

作者头像
1ess
发布2021-10-29 16:37:01
发布2021-10-29 16:37:01
3850
举报
文章被收录于专栏:0x7c00的专栏0x7c00的专栏

数据结构 From Zero To Hero(五)

發佈於 2021-02-22

本篇,我们介绍另一种利用线性表创建的一种数据结构 —— 队列(Queues)。

队列在我们日常工作中也经常使用,例如打印机、操作系统等。

Reversing a Queue

代码语言:javascript
复制
public void ReverseQueue<T>(Queue<T> q)
{
    var s = new Stack<T>();
    while (q.Count != 0)
    {
        s.Push(q.Dequeue());
    }
    while (s.Count != 0)
    {
        q.Enqueue(s.Pop());
    }
}

使用环形数组来构建队列

代码语言:javascript
复制
public class ArrayQueue<T>
{
    private class QueueOverFlowException : Exception
    {}

    private readonly T[] _queue;
    private int _count;

    private int _front;
    private int _rear;

    public ArrayQueue(int capacity)
    {
        _queue = new T[capacity];
    }

    public void Enqueue(T item)
    {
        if (_count == _queue.Length)
        {
            throw new QueueOverFlowException();
        }

        _queue[_rear] = item;
        _rear = (_rear + 1) % _queue.Length;
        _count++;
    }

    public T Dequeue()
    {
        if (IsEmpty())
        {
            throw new InvalidOperationException();
        }

        var item = _queue[_front];
        _queue[_front] = default(T);
        _front = (_front + 1) % _queue.Length;
        _count--;
        return item;
    }

    public T Peek()
    {
        if (IsEmpty())
        {
            throw new InvalidOperationException();
        }

        return _queue[_front];
    }

    public bool IsEmpty()
    {
        return _count == 0;
    }

    public bool IsFull()
    {
        return _count == _queue.Length;
    }

    public override string ToString()
    {
        return string.Format("[{0}]", string.Join(",", _queue));
    }

}

使用栈来构建队列

代码语言:javascript
复制
public class StackQueue<T>
{
    private class QueueOverFlowException : Exception
    { }

    private Stack<T> _pushStack;
    private Stack<T> _popStack;

    public void Enqueue(T item)
    {
        _pushStack.Push(item);
    }

    public T Dequeue()
    {
        if (IsEmpty())
        {
            throw new QueueOverFlowException();
        }

        PushStack2PopStack();

        return _popStack.Pop();
    }

    public T Peek()
    {
        if (IsEmpty())
        {
            throw new QueueOverFlowException();
        }

        PushStack2PopStack();

        return _popStack.Peek();
    }

    private void PushStack2PopStack()
    {
        if (_popStack.Count == 0)
        {
            while (_pushStack.Count != 0)
            {
                _popStack.Push(_pushStack.Pop());
            }
        }
    }

    public bool IsEmpty()
    {
        return _pushStack.Count == 0 && _popStack.Count == 0;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-02-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reversing a Queue
  • 使用环形数组来构建队列
  • 使用栈来构建队列
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档