Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Stack<Element> {
- var items = [Element]()
- var count : Int {
- return items.count
- }
- mutating func push(_ item: Element) {
- items.append(item)
- }
- mutating func pop() -> Element? {
- return items.removeLast()
- }
- func peek() -> Element? {
- return items.last
- }
- }
- struct Queue<Element> {
- var inStack = Stack<Element>()
- var outStack = Stack<Element>()
- mutating func enqueue(_ item: Element) {
- inStack.push(item)
- }
- mutating func dequeue() -> Element? {
- fillOutStack()
- return outStack.pop()
- }
- mutating func peek() -> Element? {
- fillOutStack()
- return outStack.peek()
- }
- private mutating func fillOutStack() {
- if outStack.count == 0 {
- while inStack.count != 0 {
- outStack.push(inStack.pop()!)
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment