Hands-On Data Structures and Algorithms with JavaScript
上QQ阅读APP看书,第一时间看更新

Implementing APIs

Implementing an API is never as easy as it seems, as discussed earlier. When making generic classes, we can never predict what kinds of a situation our queue is going to be used in. With that in mind, let's create a very generic API for our queue and expand it in future as we see fit. Some of the most common operations that we can add to the queue are as follows:

  •  add(): Pushes an item to the back of the queue
  •  remove(): Removes an item from the start of the queue
  •  peek(): Shows the last item added to the queue
  •  front(): Returns the item at the front of the queue
  •  clear(): Empties the queue
  •  size(): Gets the current size of the queue