Interfaces
Distinguish between interfaces and the implementation of data structures.
In this lesson, we look at the operations supported by the most commonly used data structures. Anyone with a bit of programming experience can see that these operations are not hard to implement correctly. It should be considered required reading.
When discussing data structures, it is important to understand the difference between a data structure’s interface and its implementation. An interface describes what a data structure does, while an implementation describes how the data structure does it.
Interface versus implementation
An interface, sometimes also called an abstract data type, defines the set of operations supported by a data structure and the semantics, or meaning, of those operations. An interface tells us nothing about how the data structure implements these operations; it only provides a list of supported operations along with specifications about what types of arguments each operation accepts and the value returned by each operation.
A data structure implementation, on the other hand, includes the internal representation of the data structure as well as the definitions of the algorithms that implement the operations supported by the data structure. Thus, there can be many implementations of a single interface. For example, we can implement List interface using arrays and we can also implement it using pointer-based data structures. Each implements the same interface, List
, but in different ways.
The Queue
, Stack
, and Deque
interfaces
The Queue
interface represents a collection of elements to which we can add elements and remove the next element. More precisely, the operations supported by the Queue
interface are:
add(x)
: It adds the valuex
to the Queue.remove()
: It removes the next (previously added) value,y
, from the Queue and returny
.
Note: The
remove()
operation takes no argument. TheQueue
’s queueing discipline decides which element should be removed.
There are many possible queueing disciplines, the most common of which include FIFO, priority, and LIFO.
A FIFO (first-in-first-out) Queue
, which is illustrated below, removes items in the same order they were added, much in the same way a queue (or line-up) works when checking out at a cash register in a grocery store. This is the most common Queue
so the qualifier FIFO is often omitted. In other texts, the add(x)
and remove()
operations on a FIFO Queue
are often called enqueue(x)
and dequeue()
, respectively.
A priority Queue
, illustrated below, always removes the smallest element from the Queue
, breaking ties arbitrarily. This is similar to the way in which patients are triaged in a hospital emergency room. As patients arrive they are evaluated and then placed in a waiting room. When a doctor becomes available he or she first treats the patient with the most life-threatening condition. The remove()
operation on a priority Queue
is usually called deleteMin()
in other texts.
A very common queueing discipline is the LIFO (last-in-first-out) discipline, illustrated below. In a LIFO Queue
, the most recently added element is the next one removed. This is best visualized in terms of a stack of plates; plates are placed on the top of the stack and also removed from the top of the stack. This structure is so common that it gets its own name: Stack
. Often, when discussing a Stack
, the names of add(x)
and remove()
are changed to push(x)
and pop()
; this is to avoid confusing the LIFO and FIFO queueing disciplines.
A Deque
is a generalization of both the FIFO Queue
and LIFO Queue
(Stack
). A Deque
represents a sequence of elements, with a front and a back. Elements can be added at the front of the sequence or the back of the sequence. The names of the Deque
operations are self-explanatory: addFirst(x)
, removeFirst()
, addLast(x)
, and removeLast()
. It is worth noting that a Stack can be implemented using only addFirst(x)
and removeFirst()
while a FIFO Queue
can be implemented using addLast(x)
and removeFirst()
.
The List
interface: linear sequences
This course will talk very little about the FIFO Queue
, Stack
, or Deque
interfaces. This is because these interfaces are subsumed by the List
interface. A List
, illustrated below, represents a sequence, of values.
In the above list, a call to get(2)
would return the value c
.
The List
interface includes the following operations:
size()
: It returnsn
, the length of the list.get(i)
: It returns the value
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy