2021-09-30-《C#数据结构与算法》-学习笔记-P14、P18-P21队列

总结

1.视频资源P14、P18-P21:
https://www.bilibili.com/video/BV1gE41157pC?p=8&spm_id_from=pageDriver
2.学习内容:
1)栈与队列 P14;
先学习了队列–因为最近想用队列数据结构
2)数组+循环队列 P18- P19;
3)数组VS循环队列性能比较 P20;
4)链表队列及优化 P21。
3.2021/10/09回溯:
紫色红色笔均为回溯后增加
1)根据伪代码编写程序;
2)更新了关于“=>”表达式的知识。

具体内容

P14 队列
1、什么是队列。

在这里插入图片描述

P18 数组队列
1、设计队列接口;
2、创建数组队列类;
3、分析数组队列时间复杂度。

在这里插入图片描述

//IQueue.cs 队列接口
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    interface IQueue<E>
    {
        void Enqueue(E e);
        E Dequeue();
        E GetFirst();
        int Count{ get; }

        bool IsEmpty{ get; }
    }
}
//数组队列类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class Array1Queue<E> : IQueue<E>
    {
        //底层是Array1<E>动态数组
        private Array1<E> array1queue;

        //构造函数
        public Array1Queue(int capacity)
        {
            array1queue = new Array1<E>(capacity);
        }

        public Array1Queue()
        {
            array1queue = new Array1<E>(10);
        }

        //int IQueue<E>.Count => array1queue.Count;
        public int Count { get { return array1queue.Count;} }

        //bool IQueue<E>.IsEmpty => array1queue.IsEmpty;
        public bool IsEmpty { get { return array1queue.IsEmpty; } }

        public E Dequeue()
        {
            return array1queue.RemoveAtFirst();
        }

        public void Enqueue(E e)
        {
            array1queue.InsertAtLast(e);
        }

        public E GetFirst()
        {
            return array1queue.GetFirst();
        }

        public override string ToString()
        {
            return " Queue1 : front " + array1queue.ToString() + " tail ";
        }
    }
}

P19 循环队列
1、循环队列原理;
2、创建动态循环数组类;
3、创建动态循环队列类。

在这里插入图片描述
在这里插入图片描述

//循环数组类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class ArrayCircle2<E>
    {
        private E[] data;
        //数组中实际存储数据个数
        private int N;
        private int first;
        private int last;

        //明确传入参数的个数
        public ArrayCircle2(int capacity)
        {
            data = new E[capacity];
            first = 0;
            last = 0;
            N = 0;
        }

        //不确定传入参数的个数
        public ArrayCircle2()
        {
            //设置一个默认容量
            data = new E[10];
            first = 0;
            last = 0;
            N = 0;
        }

        #region 返回数组容量、元素个数、判断数组是否为空
        //返回数组容量
        public int Capacity
        {
            get { return data.Length; }
        }

        //返回数组中元素个数
        public int Count
        {
            get { return N; }
        }

        //判断数组是否为空
        public bool IsEmpty
        {
            get { return N == 0; }
        }
        #endregion

        # region 向数组中插入元素
        public void InsertAtLast(E e)
        {
            if(N==data.Length)
            {
                RestCapacity(2 * data.Length);
            }

            data[last] = e;
            last = (last + 1) % data.Length;
            N++;
        }
        #endregion

        #region 重写打印、获取元素
        //重写 ToString()
        public override string ToString()
        {
            StringBuilder res = new StringBuilder();
            res.Append(string.Format("Array1:count={0} capacity={1}\n", N, data.Length));
            res.Append("[");
            for (int i = 0; i < N; i++)
            {
                res.Append(data[(first+i)%data.Length]);
                if ((first + i) % data.Length != last)
                {
                    res.Append(",");
                }
            }
            res.Append("]");
            return res.ToString();
        }

        public E GetFirst()
        {
            if (IsEmpty)
            {
                throw new ArgumentException("数组为空");
            }

            E Firstdata = data[first];
            return Firstdata;
        }
        #endregion

        #region 删除元素
        //删除某索引处元素
        
        public E RemoveAtFirst()
        {
            if(IsEmpty)
            {
                throw new ArgumentException("数组为空");
            }

            if(N==data.Length/4)
            {
                RestCapacity(data.Length / 2);
            }

            E deldata = data[first];
            data[first] = default(E);

            first = (first + 1) % data.Length;
            N--;

            return deldata;

        }

        #endregion

        #region 扩容操作
        private void RestCapacity(int newCapacity)
        {
            E[] newData = new E[newCapacity];

            
            for (int i = 0; i < N; i++)
            {
                //newData[i] = data[first];
                //first = (first + 1) % data.Length;

                newData[i] = data[(first + i) % data.Length];
            }

            data = newData;
            first = 0;
            last = N;
        }
        #endregion
    }
}
//基于循环数组队列类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class ArrayCircle2Queue<E>:IQueue<E>
    {
        //底层是ArrayCircle<E>动态数组
        private ArrayCircle2<E> array2queue;

        //构造函数
        public ArrayCircle2Queue(int capacity)
        {
            array2queue = new ArrayCircle2<E>(capacity);
        }

        public ArrayCircle2Queue()
        {
            array2queue = new ArrayCircle2<E>(10);
        }

        public int Count { get { return array2queue.Count; } }

        public bool IsEmpty { get { return array2queue.IsEmpty; } }

        public void Enqueue(E e)
        {
            array2queue.InsertAtLast(e);
        }

        public E Dequeue()
        {
            return array2queue.RemoveAtFirst();
        }

        public E GetFirst()
        {
            return array2queue.GetFirst();
        }

        public override string ToString()
        {
            return " Queue2 : front " + array2queue.ToString() + " tail ";
        }
    }
}

P20 数组队列&循环数组队列性能比较
1、时间复杂度比较。

在这里插入图片描述

P21 链表队列及优化
1、新建链表队列类并分析时间复杂度;
2、优化链表队列;
3、新建优化后的链表类及链表队列类;
4、两者性能进行比较。

在这里插入图片描述

//基于包含头指针的链表队列类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class Linklist1Queue<E>:IQueue<E>
    {
        private Linkedlist1<E> linkedlist1queue;

        public Linklist1Queue()
        {
            linkedlist1queue = new Linkedlist1<E>();
        }

        public int Count { get{ return linkedlist1queue.Count; } }

        public bool IsEmpty { get { return linkedlist1queue.IsEmpty; } }

        public E Dequeue()
        {
            return linkedlist1queue.RemoveAtFirst();
        }

        public void Enqueue(E e)
        {
            linkedlist1queue.InsertLast(e);
        }

        public E GetFirst()
        {
            return linkedlist1queue.GetFirst();
        }

        public override string ToString()
        {
            return " LinkedlistQueue1 : front " + linkedlist1queue.ToString() + " tail ";
        }
    }
}
//包含首尾指针链表类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class Linkedlist2<E>
    {
        //创建一个私有的节点类
        private class Node
        {
            public E e;
            public Node next;

            public Node(E e,Node next)
            {
                this.e = e;
                this.next = next;
            }

            public Node(E e)
            {
                this.e = e;
                this.next = null;
            }

            public override string ToString()
            {
                return e.ToString();
            }
        }

        //记录头结点的Node型变量
        private Node head;
        private Node tail;
        //记录链表中元素个数
        private int N;

        //创建链表类的构造函数,初始化时链表没有元素
        public Linkedlist2()
        {
            head = null;
            tail = null;
            N = 0;
        }

        #region 两个用户访问属性:链表中元素个数、链表是否为空
        public int Count
        {
            get { return N; }
        }
        public bool IsEmpty
        {
            get { return N == 0; }
        }
        #endregion

        #region 往链表尾部增加结点
        public void InsertLast(E e)
        {
            Node node = new Node(e);

            if (IsEmpty)
            {
                head = node;
                tail = node;
            }
            else
            {
                tail.next = node;
                tail = node;
            }
            N++;
        }
        #endregion

        #region 查询、打印
        //查询并返回索引处元素--通用、首部、尾部
        public E GetFirst()
        {
            if (IsEmpty)
            {
                throw new ArgumentException("链表为空");
            }

            return head.e;
        }

        public E GetLast()
        {
            if (IsEmpty)
            {
                throw new ArgumentException("链表为空");
            }

            return tail.e;
        }

        //重写ToString打印方法
        public override string ToString()
        {
            StringBuilder res = new StringBuilder();
            Node cur = head;

            for (int i = 0; i < N; i++)
            {
                res.Append(cur.e + "->");
                cur = cur.next;
            }

            res.Append("Null");
            return res.ToString();
        }
        #endregion

        # region 删除
        //根据索引删除并返回链表中的结点--首结点、尾结点
        public E RemoveAtFirst()
        {
            if (IsEmpty)
            {
                throw new ArgumentException("链表为空");
            }
            else
            {
                E delNode = head.e;
                head = head.next;
                N--;
                if (head == null)
                {
                    tail = null;
                }
                return delNode;
            }
           
            
        }
        #endregion
    }
}
//基于包含首位指针链表的队列类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class Linklist2Queue<E>:IQueue<E>
    {
        private Linkedlist2<E> linkedlist2queue;

        public Linklist2Queue()
        {
            linkedlist2queue = new Linkedlist2<E>();
        }

        public int Count { get{ return linkedlist2queue.Count; } }

        public bool IsEmpty { get { return linkedlist2queue.IsEmpty; } }

        public E Dequeue()
        {
            return linkedlist2queue.RemoveAtFirst();
        }

        public void Enqueue(E e)
        {
            linkedlist2queue.InsertLast(e);
        }

        public E GetFirst()
        {
            return linkedlist2queue.GetFirst();
        }

        public override string ToString()
        {
            return " LinkedlistQueue2 : front " + linkedlist2queue.ToString() + " tail ";
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值