A
few weeks ago, the concept of abstract data types (ADTs) was introduced in
CSC148. An abstract data type is basically a way of storing information and
organizing that information. It is defined in terms of its data items and
associated operations, but not its actual implementation details, in which case
the abstract methods indicates ‘what’ they do instead of ‘how’ they do.
Stack
and queue are two examples of ADTs. The structure of stack can be described by
the term ‘last in first out’ (LIFO), which means that whatever the last thing
we add to the stack is the first thing that will come out of the stack. In this
case, tray dispenser is an example. Either if we want to add a tray or remove a
tray, the operation always occurs on the top of the tray dispenser. The normal
operations associated with stack are ‘push’, ‘pop’, ‘top’, and ‘is_empty’. The operations ‘push’
and ‘pop’ allows us add and remove items in the stack, ‘top’ helps us examine
whatever the top item is, and ‘is_empty’ indicates whether the stack is empty.
Items have no index.
In a
queue data structure, elements are sorted by insertion order, referring to the
term ‘last in last out’ (FIFO). For instance, the way queue works is similar to
the line at the cash register. People line up one after another, but the first
person that will be served will be the one whom came first. It only allows us
to add to the back and remove from front, associating with two operations of
queue: ‘enqueue’ and ‘dequeue’. Notice that middle elements cannot be accessed
directly.