C#中的栈与队列/练习

这篇具有很好参考价值的文章主要介绍了C#中的栈与队列/练习。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

C#栈和队列的实现

用双向链表实现一个队列

public class DoubleNode
{
    public int Value;
    public DoubleNode pre;
    public DoubleNode next;
    public DoubleNode(int value)
    {
        this.Value = value;
        this.pre=null;
        this.next=null;
    }
}
public class MyQueue//使用双向链表实现队列
{
    public DoubleNode head;
    public DoubleNode tail;
    public void AddFromHead(DoubleNode node)//从队头插入节点
    {
        if(head == null)
        {
            head = node;
            tail = node;
        }
        else
        {
            node.next = head;
            head.pre = node;
            head = node; 
        }
    }
    public void AddFromTail(DoubleNode node)//从队尾插入节点
    {
        if(tail == null)
        {
            tail = node;
            head=node;
        }
        else
        {
            node.pre = tail;
            tail.next = node;
            tail = node;
        }
    }
    public DoubleNode PopFromHead()//从队头弹出节点
    {
        if (head == null) return null;
        DoubleNode res = head;
        if (head==tail)
        {
            head=null;
            tail=null;
        }
        else
        {
            head = head.next;
            res.next=null;
            head.pre=null;
        } 
        return res;
    }
    public DoubleNode PopFromTail()//从队尾弹出节点
    {
        if (tail==null) return null;
        DoubleNode res=tail;
        if (head==tail)
        {
            head=null;
            tail=null;
        }
        else
        {
            tail=tail.pre;
            res.pre=null;
            tail.next=null;
        }
        return res;
    }
}

使用双向链表实现栈文章来源地址https://www.toymoban.com/news/detail-711172.html

 public class MyStack//使用双向链表实现栈
    {
        public DoubleNode Head;
        public DoubleNode Tail;
        
        public void Push(int value)
        {
            DoubleNode temp = new DoubleNode(value); 
            if (Head == null)
            {
                Head = temp;
                Tail= temp;
            }
            else
            { 
                temp.next= Head;
                Head.pre = temp;
                Head = temp;
            }
        }
        public DoubleNode Pop()
        {
            if (Head == null) return null;
            DoubleNode res = Head; 
            if (Head == Tail)
            {
                Head = null; 
                Tail = null; 
                return res;
            } 
            Head = Head.next;
            res.next = null;
            Head.pre = null;
            return res;
        }
        public DoubleNode Peek()
        {
            if (Head == null) return null;
            return Head;
        }
    }

使用数组实现固定最大长度的栈和队列

 public class MyStack1//使用数组实现固定最大长度的栈,暂定为L
 {
     public int[]nums;
     public int index;
     public int limit;
     public MyStack1(int L)
     {
         limit = L;
         nums= new int[limit];
         index=0;
     }
     public void Push(int n)
     {
         if (index<limit)
             nums[index++] = n;
         else
             Console.WriteLine("栈已满");
     }
     public int Pop()
     {
         int res = nums[--index]; 
         return res;
     }
     public int Peek()
     {
         return nums[index-1];
     }
 }

 public class MyQueue1//使用数组实现固定最大长度的队列,暂定为L
 {
     public int[] nums;
     public int headIndex;
     public int tailIndex;
     public int size;
     public int limit;

     public MyQueue1(int L)
     {
         limit = L;
         nums=new int[limit];
         size=0;
         headIndex=0;
         tailIndex=0;
     }
     public int NextIndex(int i)
     {
         return i<=limit-1? i+1:0;
     }

     public void Push(int n)
     {
        if(size==limit)
         {
             Console.WriteLine("队列已经满了");
             return;
         }
        size++;
         nums[tailIndex] = n;
         tailIndex=NextIndex(tailIndex);
     }
     public int Pop()
     {
         if (size==0)
         {
             throw new Exception("队列空了");
         }
         size--;
         int res = nums[headIndex];
         headIndex=NextIndex(headIndex);
         return res;
     }

 }

使用现成的栈结构实现一个能返回最小值的栈

//一个能返回最小值的栈(使用现成的栈结构实现)
    public class MyStackWithNormal //用现成的栈结构实现
    { 
        public Stack<int> stack=new Stack<int>();  
        public Stack<int> minStack=new Stack<int>();
        public void push(int num)
        {
            stack.Push(num);
            if (minStack.Count == 0)
            {
                minStack.Push(num);
            }
            else
            {
                int temp = num > minStack.Peek() ? minStack.Peek() : num;
                minStack.Push(temp);
            }
        }
        public int pop()
        {
            return stack.Pop();
        }
        public int GetMin()
        {
            return minStack.Peek();
        }
    } 

使用现有的栈实现队列,和使用现有的队列实现栈

public class QueueWithStack//使用现有的栈结构实现队列
{
    private Stack<int> stack = new Stack<int>();
    private Stack<int> outStack = new Stack<int>();
    public Stack<int> OutStack { get 
        
        {//往输出栈中加数据时,输出栈必须为空,且必须把存储栈中的数据全部加入输出栈
           if(outStack==null)
            {
            if (stack.Count==0)
            {
                Console.WriteLine("队列为空");
                return null;
            }
                while (stack!=null)
                {
                    outStack.Push(stack.Pop());
                }
                return outStack;
            }
            else
            {
                return outStack;
            }
        }
    }
    public int Peek()
    {
        return outStack.Peek();
    }
    public int Pop() { return OutStack.Pop(); }
    public void Push(int value)
    {
        stack.Push(value);
    }
}


public class StackWithQueue//使用现有的队列结构实现栈
{
    public Queue<int> queue1 = new Queue<int>();
    public Queue<int> queue2 = new Queue<int>();

    public void Push(int n)
    {
        queue1.Enqueue(n); 
    }
    public int Peek()
    {
    if (queue1.Count==0 &&queue2.Count==0)
    {
        throw new Exception("栈为空");
    }
    Queue<int> data=queue1.Count==0 ? queue2 : queue1;
        Queue<int> help=data==queue1? queue2 : queue1;
        while(data.Count > 1)
        {
            help.Enqueue(data.Dequeue());
        }
        int res=data.Peek();
        help.Enqueue(data.Dequeue() );
        return res; 
    }
    public int Pop()
    {
    if( queue1.Count==0 &&queue2.Count==0)
    {
        throw new Exception("栈为空");
    }
        Queue<int> data=queue1.Count==0 ? queue2 : queue1;
        Queue<int> help=data==queue1? queue2 : queue1;
        while(data.Count > 1)
        {
            help.Enqueue(data.Dequeue());
        }
        int res=data.Dequeue();
        return res; 
    }
}

到了这里,关于C#中的栈与队列/练习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【C++初阶10-stack&queue】STL中的栈和队列(附优先级队列

    本期分享:STL中的栈和队列。 在数据结构初阶时,我们已经学习这来那个两种数据结构,如今来看STL中的,不过是更加标准化。而实现起来,会简单得超乎你想象! 文中不足错漏之处望请斧正! STL中的栈和队列是容器适配器。容器适配器是对某种已有容器的再次封装。 比如

    2024年02月06日
    浏览(33)
  • 关于线性结构中的双向链表如何实现?

    在上一篇文章中,主要是给大家介绍了单向链表的特点及其原理,但是我们没有通过代码进行练习。今天我会继续通过一篇文章,来给大家讲解双向链表的内容,尤其是会通过代码来进行链表的操作,希望大家重点关注哦。 全文大约【 3500】 字,不说废话,只讲可以让你学到

    2024年02月09日
    浏览(25)
  • 关于线性结构中的双向链表如何实现的方法

    在上一篇文章中,主要是给大家介绍了单向链表的特点及其原理,但是我们没有通过代码进行练习。今天我会继续通过一篇文章,来给大家讲解双向链表的内容,尤其是会通过代码来进行链表的操作,希望大家重点关注哦。 全文大约【 3500】 字,不说废话,只讲可以让你学到

    2024年02月16日
    浏览(24)
  • 辅助栈、单调栈与单调队列在lc中的应用

    三者都有何区别? 个人建议 单调栈/队列 中存放的元素最好是下标而不是值,因为有的题目需要根据下标计算,这样泛化性更好。参考lc239和lc496 大概的思路是先把不满足单调性的元素poll掉,然后offer一个当前符合条件的元素。参考lc239和lc496 // 说明:这里使用两个堆来维护

    2024年02月14日
    浏览(27)
  • 【数据结构经典题目】—两个队列实现栈与两个栈实现队列

    ​                                           食用指南:本文在有C基础的情况下食用更佳                                            🔥 这就不得不推荐此专栏了: C语言                                         🍀

    2024年02月13日
    浏览(35)
  • 【数据结构】 栈与队列的相互实现

    队列与栈的操作算法是笔试面试中较为常见的题目。 本文将着重介绍平时面试中常见的关于队列与栈的应用题目,马上要进行秋招了。希望对你们有帮助 _😀 请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。 实现

    2024年02月10日
    浏览(35)
  • Leetcode的AC指南 —— 栈与队列:232.用栈实现队列

    摘要: **Leetcode的AC指南 —— 栈与队列:232.用栈实现队列 **。题目介绍:请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty): 实现 MyQueue 类: void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的开头移除并返回元素 int

    2024年01月20日
    浏览(32)
  • (详解)数据结构-----------栈与队列 c语言实现

    本章将会详细讲解以下知识点: 目录 一:栈         1:栈的定义,栈的特点         2:用什么结构来实现栈与原因的分析?         3:  (超详解)栈的常用接口并且附上测试用例 二:队列         1:队列的定义,队列的特点         2:用什么结构来实现队列与原因的分析

    2024年02月11日
    浏览(34)
  • 数据结构之栈与队列的实现与详细解析

    个人主页:点我进入主页 专栏分类:C语言初阶      C语言程序设计————KTV       C语言小游戏     C语言进阶 C语言刷题       数据结构初阶 欢迎大家点赞,评论,收藏。 一起努力,一起奔赴大厂。 目录 1.前言 2.栈 2.1栈的概念与性质 2.2栈的实现 3.队列 3.1队列的概

    2024年02月05日
    浏览(35)
  • day11 代码回想录-栈与队列part02-有效的括号&删除字符串中的所有相邻重复项&逆波兰表达式求值

    大纲 ● 20. 有效的括号 ● 1047. 删除字符串中的所有相邻重复项 ● 150. 逆波兰表达式求值 有效的括号 题目链接:20. 有效的括号 题目需要判断括号是否匹配 解题思路: 使用栈来实现,当为**{[( 时入栈,当遇到 )]} 时,判断栈顶元素释放能匹配。需要单独处理只有 右边**单个

    2024年02月11日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包