title: Queue implementation in Swift language
body: <p>I m trying to implement Queue collection type in Swift platform. I have got some problems about peek, poll and offer functions. When I try to use these functions in my code, it fails. Do you have any advice or true algorithm for that?</p>
  
  <pre><code>import Foundation
  
  
  class Node&lt;T&gt; {
      var value: T? = nil
      var next: Node&lt;T&gt;? = nil
      var prev: Node&lt;T&gt;? = nil
  
      init() {
      }
  
      init(value: T) {
          self.value = value
      }
  }
  
  class Queue&lt;T&gt; {
  
  var count: Int = 0
  
  var head: Node&lt;T&gt; = Node&lt;T&gt;()
  
  var tail: Node&lt;T&gt; = Node&lt;T&gt;()
  
  var currentNode : Node&lt;T&gt; = Node&lt;T&gt;()
  
      init() {
      }
  
      func isEmpty() -&gt; Bool {
          return self.count == 0
      }
  
      func next(index:Int) -&gt; T? {
  
          if isEmpty() {
              return nil
          } else if self.count == 1 {
              var temp: Node&lt;T&gt; = currentNode
              return temp.value
          } else if index == self.count{
              return currentNode.value
  
          }else {
              var temp: Node&lt;T&gt; = currentNode
              currentNode = currentNode.next!
              return temp.value
          }
  
      }
  
      func setCurrentNode(){
          currentNode = head
      }
  
      func enQueue(key: T) {
          var node = Node&lt;T&gt;(value: key)
          if self.isEmpty() {
              self.head = node
              self.tail = node
          } else {
              node.next = self.head
              self.head.prev = node
              self.head = node
          }
  
          self.count++
      }
  
      func deQueue() -&gt; T? {
          if self.isEmpty() {
              return nil
          } else if self.count == 1 {
              var temp: Node&lt;T&gt; = self.tail
              self.count--
              return temp.value
          } else {
              var temp: Node&lt;T&gt; = self.tail
              self.tail = self.tail.prev!
              self.count--
              return temp.value
          }
      }
  
  
  
      //retrieve the top most item
      func peek() -&gt; T? {
  
          if isEmpty() {
              return nil
          }
  
          return head.value!
      }
  
      func poll() -&gt; T? {
  
          if isEmpty() {
              return nil
          }else{
              var temp:T = head.value!
              deQueue()
              return temp
          }
  
      }
  
      func offer(var key:T)-&gt;Bool{
          var status:Bool = false;
  
          self.enQueue(key)
          status = true
  
  
          return status
      }
  }
  </code></pre>