What is an Abstract Data Type? Stacks Queues

J.E.D.I.

3.3 Abstract Data Types

3.3.1 What is an Abstract Data Type? An abstract data type ADT is a collection of data elements provided with a set of operations that are defined on the data elements. Stacks, queues and binary trees are some examples of ADT. In this section, you will be learning about stacks and queues.

3.3.2 Stacks

A stack is a set of data elements wherein manipulation of the elements is allowed only at the top of the stack. It is a linearly ordered collection of data on which the discipline of “last in, first out” LIFO is imposed. Stacks are useful for a variety of applications such as pattern recognition and conversion among infix, postfix and prefix notations. The two operations associated with stacks are the push and pop operations. Push simply means inserting at the top of the stack whereas pop refers to removing the element at the top of the stack. To understand how the stack works, imagine how you would add or remove a plate from a stack of plates. Your instinct would tell you to add or remove only at the top of the stack because otherwise, there is a danger of causing the stack of plates to fall. Here is a simple illustration of how a stack looks like. n-1 ... 6 5 Jayz top 4 KC 3 Jojo 2 Toto 1 Kyla DMX bottom Table 3: Stack illustration The stack is full if the top reaches the cell labeled n-1. If the top is equal to -1, this indicates that the stack is empty.

3.3.3 Queues

The queue is another example of an ADT. It is a linearly ordered collection of elements which follow the discipline of “first-in, first-out”. Its applications include job scheduling in operating system, topological sorting and graph traversal. Enqueue and dequeue are the operations associated with queues. Enqueue refers to inserting at the end of the queue whereas dequeue means removing front element of the queue. To remember how a queue works, think of the queue’s literal meaning – a line. Imagine yourself lining up to get the chance meeting your favorite star up close. If a new person would like to join the line, this person would have to go to the end of the line. Otherwise, there would probably be some fight. This is how enqueue works. Who gets to meet hisher favorite star first among those who are waiting in the line? It should have Introduction to Programming II Page 45 J.E.D.I. been the first person in the line. This person gets to leave the line first. Relate this to how dequeue works. Here is a simple illustration of how a queue looks like. 1 2 3 4 5 6 7 8 9 ... n-1 Eve Jayz KC Jojo Toto Kyla DMX front end ← Insert → Delete Table 4: Queue illustration The queue is empty if end is less than front. Meanwhile, it is full when the end is equal to n-1.

3.3.4 Sequential and Linked Representation