C#数据结构与算法—栈与队列

本文介绍了C#中栈和队列的实现,包括数组栈和链表栈,分析了它们的性能差异。接着讨论了数组队列与循环队列,指出循环队列在出队操作上的优势。最后提到了链表队列的优化,通过添加尾指针来提升性能。这些内容展示了数据结构在实际应用中的优化策略。
摘要由CSDN通过智能技术生成

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

数组栈

接口代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure3
{
   
    interface IStack<E>
    {
   
        int Count {
    get; }
        bool IsEmpty {
    get; }
        void Push(E e);
        E Pop();
        E Peek();
    }
}

数组栈类代码:此处调用的方法为为之前所写数组类方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure3
{
   
    class Array1Stack<E> : IStack<E>
    {
   
        private Array1<E> arr;

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

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

        public Array1Stack(int capacity)//构造函数
        {
   
            arr = new Array1<E>(capacity);
        }
        public Array1Stack()//没有参数就自动扩容
        {
   
            arr = new Array1<E>();
        }

        public void Push(E e)
        {
   
            arr.AddLast(e);//选择数组的尾部作为栈顶,此处调用的方法为为之前所写数组类方法
        }

        public E Pop()
        {
   
            return arr.RemoveLast();//按照删除数组尾部元素作为出栈
        }

        public E Peek()//查询栈顶元素
        {
   
            return arr.GetLast();
        }
        public override string ToString()
        {
   
            return "Stack:" + arr.ToString() + "top";
        }
    }
}

主程序代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure3
{
   
    class Program
    {
   
        static void Main(string[] args)
        {
   
            Array1Stack<int> stack = new Array1Stack<int>();
            for(int i=0;i<5;i++)
            {
   
                stack.Push(i);
                Console.WriteLine(stack);
            }
            stack.Pop();
            Console.WriteLine(stack);

            Console.Read();
        }
    }
}

链表栈

链表栈类代码:所用接口为上面数组栈所添加的接口Istack,方法为上节所写的链表类方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure3
{
   
    class LinkedList1Stack<E>:IStack<E>//使用链表类封装一个链表栈
    {
   
        private LinkedList1<E> list;
       public LinkedList1Stack()
        {
   
            list = new LinkedList1<E>();
        }

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


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


        public E Peek()
        {
   
            return list.GetFirst();
        }

        public E Pop()
        {
   
            return list.RemoveFirst();
        }

        public void Push(E e)
        {
   
            list.AddFirst(e);//以链表头部作为栈顶
        }
        public override string ToString()//打印输出方法
        {
   
            return "Stack:top" + list.ToString();
        }
    }
}

主程序代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure3
{
   
    class Program
    {
   
        static void Main(string[] args)
        {
   
            
            LinkedList1Stack<int> stack = new LinkedList1Stack<int>();
            for (int i = 0; i < 5; i++)
            {
   
                stack.Push(i);
                Console.WriteLine(stack);
            }
            stack.Pop();
            Console.WriteLine(stack);

            Console.Read();
        }
    }
}

结果

Stack:top0->Null
Stack:top1->0->Null
Stack:top2->1-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值