We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Full swift solution as the hackerrank managed part to parse input is missing:
importFoundation// Enter your code here classTwoStackQueue{varinputStack=[Int]()varoutputStack=[Int]()funcenqueue(_elem:Int){inputStack.insert(elem,at:0)}funcdequeue(){ifoutputStack.isEmpty{balanceStacks()}if!outputStack.isEmpty{outputStack.removeFirst()}}funcprintFirstElement(){ifoutputStack.isEmpty{balanceStacks()}ifletelem=outputStack.first{print(elem)}}privatefuncbalanceStacks(){whileletelem=inputStack.first{outputStack.insert(elem,at:0)inputStack.removeFirst()}}}funcmain(){letqueue=TwoStackQueue()guardletnumberOfQueries=Int(readLine()??"X")else{print("Unknown number of queries")return}for_in0..<numberOfQueries{letline=readLine()??""switchInt(String(line.first??Character("X"))){case1:letargs=line.split(separator:" ")guardletarg=args.last,letargInt=Int(arg)else{print("Unknown query type 1 \(line)")return}queue.enqueue(argInt)case2:queue.dequeue()case3:queue.printFirstElement()default:print("Unknown query \(line)")return}}}main()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
Swift
Full swift solution as the hackerrank managed part to parse input is missing: