题解 | #【模板】队列#

【模板】队列

https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549?tpId=308&tqId=2110348&ru=/exam/oj&qru=/ta/algorithm-start/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D308

using System.Collections.Generic;
using System;
public class Program {
    public static void Main() {
        Queue q = new Queue(); 
        //读取操作个数
        int n = int.Parse(Console.ReadLine());
        while (n-- >0 ) { 
            string opr = Console.ReadLine();
            if(opr.IndexOf("push")!=-1){
                q.Enqueue(int.Parse(opr.Split(" ")[1]));
            }else if(opr.Equals("pop")){
                q.Dequeue();
            }else{
                q.Peek();
            }
        }
    }

    public class Queue{
        List<int> list = new List<int>();
        public void Enqueue(int val){
            list.Add(val);
        }

        public void Dequeue(){
            if(list.Count == 0)
            {
                Console.WriteLine("error");
                return;
            }
            Console.WriteLine(list[0]);
            list.RemoveAt(0);        
        }

        public void Peek(){
            if(list.Count == 0)
            {
                Console.WriteLine("error");
                return;
            }
            Console.WriteLine(list[0]);
        }
    }
}

全部评论

相关推荐

04-11 23:51
门头沟学院 Java
坚定的芭乐反对画饼_许愿Offer版:人人都能过要面试干嘛,发个美团问卷填一下,明天来上班不就好了
点赞 评论 收藏
分享
AAA专业长城贴瓷砖刘大爷:这样的简历我会直接丢进垃圾桶,花里胡哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务