Professional Documents
Culture Documents
Prepared by J. Jones
Page 1 of 7
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
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;
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
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
Front = 3
MaxSize = 6
NumberInQueue = 5
{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
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
Procedure DeQueue
If NumberInQueue = 0 Then
Write Queue empty
Else
NewItem = Q[Front]
Subtract 1 from NumberInQueue
If Front = 6 Then
Front = 1
Else
EndIf
EndIf
EndProcedure
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
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