You are on page 1of 7

Data Structures: Abstract Data Type (ADT)

Stacks & Queues

Abstract Data Types


All processing on a computer involves the manipulation of data. This data can be
organized in the computers memory in different ways according to how it is to be
processed, and the different methods of organizing data are known as data structures.
Computer languages such as Pascal have built-in elementary data types (such as integer,
real, Boolean and char) and some built-in type structured or composite data types (data
structures) such as record, array and string. These composite data types are made up of a
number of elements of a specified type such as integer or real.
Some data structures such as queues, stacks and binary trees are not built into the
language and have to be constructed by the programmer. These are known as Abstract
Data Types (ADT). An Abstract Data Type (ADT) is the combination of a data structure
and a group of functions or procedures designed to manipulate the data within the data
structure.
An ADT must possess the following characteristics:
the facility to create a container to hold the data;
the facility to add a new element to the container;
the facility to remove/delete an element which satisfies some criterion from the
container;
the facility to find an element which satisfies some criterion within the container;
the facility to destroy the container when it is no longer required
Stacks
A stack is a particular kind of sequence that may only be accessed at one end, known as
the top of the stack (like plates on a pile in a cafeteria). Only two operations can be
carried out on a stack. Adding a new item involves placing it on the top of the stack
(pushing or stacking the item). Removing an item involves the removal of the item that
was most recently added (popping the stack). The stack is a LIFO Structure Last In,
First Out.

Prepared by J. Jones

Page 1 of 7

Data Structures: Abstract Data Type (ADT)

Stacks & Queues

Note that items do not move up and down as the stack is pushed and popped; instead, the
position of the top of the stack changes. A pointer called a stack pointer indicates the
position of the top of the stack.
Characteristics of a Stack
The specific operation which defines a stack is the concept of Last-in-first-out.
Data elements which have been added to the container are removed from the
container in the reversed order to which they are inserted.
Typically, this ADT is identified by a variable called the head which identifies
the location of the most recent element added to the container.
The functions of push and pop are typically used to describe the process of
inserting and deleting elements respectively, from the stack.
Implementation of a stack
A stack can be represented in memory by an array and two additional integer variables,
one holding the size of the array (i.e. the maximum size of the stack) and one holding the
pointer to the top of the stack (Top). Top will initially be set to 0, representing an empty
stack.
6
5
4
3
2
1

MaxStackSize = 6
Anne
Millie
Charles

Top = 3

Diagram showing an array that can hold a maximum of 6 elements with 3 items
currently in the stock.

Prepared by J. Jones

Page 2 of 7

Data Structures: Abstract Data Type (ADT)

Stacks & Queues

The following pseudocode may be used to add (push) an element onto a stack:
Procedure Push
If Top = MaxStackSize Then
Write Stack is full
Else
Add 1 to Top
Stack[Top] = NewItem
EndIf
EndProcedure
The following pseudocode may be used to remove (pop) an element from a stack:
Procedure Pop
If Top = 0 Then
Write Stack is empty
Else
PoppedItem = Stack[Top]
Subtract 1 from Top
EndIf
EndProcedure
Applications of stacks
Stacks are very important data structures in computing. They are used in many different
situations in computing, for example:

to store return addresses, parameters and register contents when subroutines are
called. When the subroutine ends, the address at the top of the stack is popped and
the computers continues execution from that address;

translating from one computer language to another, and transferring control from
one part of a program to another;

in evaluating mathematical expressions held in reverse Polish notation, i.e. a form


of notation used by compilers as an intermediate step in translating expressions
such as A:= (B*C) + D/E.

Stacks can also be used to reverse the elements of a queue. This is done by pushing the
elements of the queue one by one on to a stack, and then popping them one by one and
replacing them in the queue.

Prepared by J. Jones

Page 3 of 7

Data Structures: Abstract Data Type (ADT)

Stacks & Queues

Queue
A queue is a First In First Out (FIFO) data structure. New elements may only be added to
the end of a queue, and elements may only be retrieved from the front of a queue. The
sequence of data items in a queue is determined, therefore, by the order in which they are
inserted. The size of the queue depends on the number of items in it, just like a queue at
the cinema or supermarket checkout.
Implementation of a queue
A queue can be implemented as an array with a pointer to the front of the queue and a
pointer to the rear of the queue. An integer holding the size of the array (the maximum
size of the queue) is needed, and it is useful to have an extra variable giving the number
of items currently in the queue.
John

Catherine

Rob

Front = 1

Rear = 3

MaxSize = 6

NumberInQueue = 3

After 2 people have left the queue and 3 more have joined, the queue will look like this:
Rob
Front = 3
MaxSize = 6

Max

Lisa

Anna
Rear = 6

NumberInQueue = 4

Now what? Only 4 people are in the queue but the end of the array has been reached. To
overcome this problem, the elements could be shuffled along, so the front of the queue is
always in location 1. This is known as Linear Queue. However, busy queues would
spend rather a lot of time shuffling elements. Another method to overcome the problem is
to implement the queue as a Circular Queue, so that when the next person joins they
enter at the front of the array:

Ben

Prepared by J. Jones

Rob

Max

Lisa

Anna

Page 4 of 7

Data Structures: Abstract Data Type (ADT)


Rear = 1

Stacks & Queues

Front = 3

MaxSize = 6

NumberInQueue = 5

Procedures to implement a circular queue


The above queue may be implemented by declaring variables as follows:
Q[6] as String
Front, Rear, NumberInQueue as Integer
To initialize the queue:
Procedure Initialize
Front = 1
Rear = 6

{or Rear = 0}

NumberInQueue = 0
EndProcedure
To add an element to the queue:
Procedure EnQueue
If NumberInQueue = 6 Then
Write Queue overflow
Else
If Rear = 6 Then
Rear = 1
Else

or Rear = (Rear Mod 6) + 1


Add 1 to Rear

EndIf
Q[Rear] = NewItem
Add 1 to NumberInQueue
EndIf
EndProcedure
To remove an element from the queue:

Prepared by J. Jones

Page 5 of 7

Data Structures: Abstract Data Type (ADT)

Stacks & Queues

Procedure DeQueue
If NumberInQueue = 0 Then
Write Queue empty
Else
NewItem = Q[Front]
Subtract 1 from NumberInQueue
If Front = 6 Then
Front = 1
Else

or Front = (Front Mod 6) + 1


Add 1 to Front

EndIf
EndIf
EndProcedure

Implementing a queue as a linked list


A queue may be implemented as a special kind of linked list, with each element in the
queue pointing to the next item. An external pointer points to the front of the queue, and
items may only be removed from the front of the list and added to the end of the list.

Data

Data

Data

Data

Front
The front of the queue is accessed through the pointer front. To add an element to the
queue, the pointers have to be followed until the node containing a pointer of 0 is reached
signifying the end of the queue, and this pointer is then changed to point to the new node.
(In some implementations two pointers are kept; one to the front and one to the rear. This
saves having to traverse the whole queue when a new element is to be added.)

Prepared by J. Jones

Page 6 of 7

Data Structures: Abstract Data Type (ADT)

Stacks & Queues

Applications of a queue
Output waiting to be printed is commonly stored in a queue on disk. In a room
full of networked computers, several people may send work to be printed at more
or less the same time. By putting the output into a queue on disk, the output is
printed on a first come, first served basis as soon as the printer is free.
Characters typed at keyboard are held in a queue in a keyboard buffer.
Jobs waiting to be run by the computer may be held in a queue.
Characteristics of a Queue
The specific operation which defines a queue is a concept of First-in-first-out.
Data elements which have been added to the container are removed from the
container in exactly the same order in which they were inserted.
Typically, this ADT is identified by a variable called the head which identifies
the location of the longest existing element in the container and another variable
called tail which identifies the most recent element added to the container.
The functions of enqueue and dequeue are typically used to describe the
process of inserting and deleting elements respectively from the queue.
In the queue, data elements are inserted through the tail and removed from the
head.

Prepared by J. Jones

Page 7 of 7

You might also like