Escolar Documentos
Profissional Documentos
Cultura Documentos
Lecture No. 09
___________________________________________________________________
Data Structures
Lecture No. 09
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 3
3.3.3, 3.4.1, 3.4.2
Summary
Memory Organization
Stack Layout During a Function Call
Queues
Queue Operations
Implementing Queue
Queue using Array
Use of Queues
Memory Organization
By the end of last lecture, we discussed the uses of stack to develop a process from an
executable file and then in function calls. When you run an executable, the operating
system makes a process inside memory and constructs the followings for that purpose.
- A code section that contains the binary version of the actual code of the program
written in some language like C/C++
- A section for static data including global variables
- A stack and
- Finally, a heap
Page 1 of 13
Code
Process 3
(Word)
Static Data
Process 4
(Excel)
Stack
Process 2
(Dev-C++)
Heap
Windows OS
Parameters (F)
Parameters (F)
Local variables (F)
Return address (F)
sp
Parameters (G)
Parameters (G)
Local variables (G)
Return address (G)
At point of call
sp
sp
After Call
During Execution of G
Fig 2: Stack Layout; When function F calls function G
The above diagrams depict the layout of the stack when a function F calls a function G.
Here sp stands for stack pointer. At the very left, you will find the layout of the stack just
before function F calls function G. The parameters passed to function F are firstly
inserted inside the stack. These are followed by the local variables of the function F and
finally the memory address to return back after the function F finishes. Just before
function is made to the function G, the parameters being passed to the function G, are
Page 2 of 13
Queues
A queue is a linear data structure into which items can only be inserted at one end and
removed from the other. In contrast to the stack, which is a LIFO (Last In First Out)
structure, a queue is a FIFO (First In First Out) structure.
The usage of queue in daily life is pretty common. For example, we queue up while
depositing a utility bill or purchasing a ticket. The objective of that queue is to serve
persons in their arrival order; the first coming person is served first. The person, who
comes first, stands at the start followed by the person coming after him and so on. At the
serving side, the person who has joined the queue first is served first. If the requirement is
to serve the people in some sort of priority order, there is a separate data structure that
supports priorities. The normal queue data structure, presently under discussion, only
supports FIFO behavior.
Now, lets see what are the operations supported by the queue.
Queue Operations
The queue data structure supports the following operations:
Operation
enqueue(X)
dequeue()
Description
Place X at the rear of the queue.
Remove the front element and return it.
front()
Page 4 of 13
Implementing Queue
There are certain points related to the implementation of the queue. Suppose we are
implementing queue with the help of the linked -list structure. Following are the key
points associated with the linked list implementations:
- Insert works in constant time for either end of a linked list.
- Remove works in constant time only.
- Seems best that head of the linked list be the front of the queue so that all removes
will be from the front.
- Inserts will be at the end of the list.
front
1
rear
7
rear
front
1
front
rear
5
rear
7
Page 5 of 13
front
rear
7
rear
7
Page 7 of 13
real
front
rear
real
7
front
rear
As shown in the above diagram, an element i.e. 6 has been inserted in the queue. Now,
the rear index is containing 4 while the front has the same 0 index. Lets see the figure of
the array when another element 8 is inserted in the queue.
enqueue(8)
front
real
1 7 5 2 6 8
2
3
front
6
4
8
5
rear
5
When an element is removed from the queue. It is removed from the front index.
Page 8 of 13
dequeue( )
front
real
7 5 2 6 8
0
front
rear
real
5
5 2 6 8
0
front
rear
real
5
5 2 6 8 9 12
0
front
rear
12
7
rear
2
12
6
12
2
8
front
2
rear
7
The number of locations in the above circular array are also eight, starting from index 0
to index 7. The index numbers are written outside the circle incremented in the clockwise direction. To insert an element 21 in the array , we insert this element in the
location, which is next to index 7.
enqueue(21)
front
0
rear
1
21
5
12
5 2 6 8 9 12 21
6
Fig 13. An element added in circular array
2
8
5
6
4
front
2
rear
noElements
size
8
Now, we will have to maintain four variables. front has the same index 2 while the, size is
8. rear has moved to index 0 and noElements is 7. Now, we can see that rear index has
decreased instread of increasing. It has moved from index 7 to 0. front is containing index
2 i.e. higher than the index in rear. Let see, how do we implement the enqueue() method.
Page 10 of 13
rear
0
7
5 2 6 8 9 12 21 7
6
1
21
12
2
8
5
Fig 14. Another element added in circular array
6
4
front
2
rear
noElements
size
8
Now, the queue, rather the array has become full. It is important to understand, that queue
does not have such characteristic to become full. Only its implementation array has
become full. To resolve this problem, we can use linked list to implement a queue. For
the moment, while working with array, we will write the method isFull(), to determine
the fullness of the array.
int isFull()
{
return noElements == size;
}
int isEmpty()
{
return noElements == 0;
}
Page 11 of 13
dequeue()
front
rear
6 8 9 12 21 7
6
1
21
12
9
8
5
Fig 15. Element removed from the circular array
6
4
front
4
rear
noElements
size
8
int dequeue()
{
int x = array[front];
front = (front + 1) % size;
noElements = noElements - 1;
return x;
}
In the first line, we take out an element from the array at front index position and store it
in a variable x. In the second line, front is incremented by 1 but as the array is circular,
the index is looped from 0 to 7. That is why the mod (%) is being used. In the third line,
number of elements (noElements) is reduced by 1 and finally the saved array element is
returned.
Use of Queues
We saw the uses of stack structure in infix, prefix and postfix expressions. Lets see the
usage of queue now.
Out of the numerous uses of the queues, one of the most useful is simulation. A
simulation program attempts to model a real-world phenomenon. Many popular video
games are simulations, e.g., SimCity, Flight Simulator etc. Each object and action in the
simulation has a counterpart in the real world. Computer simulation is very powerful tool
and it is used in different high tech industries, especially in engineering projects. For
example, it is used in aero plane manufacturing. Actually Computer Simulation is fullfledged subject of Computer Science and contains very complex Mathematics,
sometimes. For example, simulation of computer networks, traffic networks etc.
If the simulation is accurate, the result of the program should mirror the results of the
Page 12 of 13
Page 13 of 13