2. Primitive data structure
• These are basic structures and directly operated upon by
the machine instructions.
• have different representations on different computers.
• Integer, floating point numbers, character constants, string
constants, pointers etc. falls in this category
3. Non-primitive data structures
• These are more sophisticated data structures.
• These are derived from the primitive data structures.
• The Non-primitive data structures emphasize on
structuring of a group of homogeneous or heterogeneous
data items.
• The design of an efficient data structure must take
operations to be performed on the data structure into
account.
4. 1. Arrays
• Arrays are a homogeneous and contiguous collection of same
data types.
• They have a static memory allocation technique, which means, if
memory space is allocated for once, it cannot be changed during
runtime.
• The arrays are used to implement vectors, matrices and also
other data structures.
• If we do not know the memory to be allocated in advance then
array can lead to wastage of memory.
• Also, insertions and deletions are complex in arrays since
elements are stored in consecutive memory allocations.
5. 2. Files
• A file is a collection of records.
• The file data structure is primarily used for managing
large amounts of data which is not in the primary storage
of the system.
• The files help us to process, manage, access and retrieve
or basically work with such data, easily.
6. Lists
The lists support dynamic memory allocation. The memory space
allocated, can be changed at run time also. The lists are of two types:
a) Linear Lists
• The linear lists are those which have the elements stored in a
sequential order. The insertions and deletions are easier in the lists.
They are divided into two types:
• Stacks: The stack follows a “LIFO” technique for storing and retrieving
elements. The element which is stored at the end will be the first one to
be retrieved from the stack. The stack has the following primary
functions:
• Push(): To insert an element in the stack.
• Pop(): To remove an element from the stack.
7. • Queues: The queues follow “FIFO” mechanism for storing
and retrieving elements.
• The elements which are stored first into the queue will
only be the first elements to be removed out from the
queue.
• The “ENQUEUE” operation is used to insert an element
into the queue whereas the “DEQUEUE” operation is
used to remove an element from the queue.
8. b)Non Linear List
• The non linear lists do not have elements stored in a certain
manner. These are:
• Graphs: The Graph data structure is used to represent a
network. It comprises of vertices and edges (to connect the
vertices). The graphs are very useful when it comes to study a
network.
• Trees: Tree data structure comprises of nodes connected in a
particular arrangement and they (particularly binary trees) make
search operations on the data items easy. The tree data
structures consists of a root node which is further divided into
various child nodes and so on. The number of levels of the tree is
also called height of the tree.
9. The most commonly used operations on data structures are
broadly categorized into four types
• 1.CREATE
• 2.DESTROY or DELETE
• 3.SELECTION
• 4.UPDATION
10. • The CREATE operation results in reserving memory for the
program elements. This can be done by declaration statement.
The creation of data structure may take place during compile time
or during runtime.
• DESTROY operation destroys the memory space allocated for
the specified data structure. New and delete operators of C++
languages are used for these two operations respectively.
• The SELECTION operation deals with accessing a particular data
within a data structure.
• UPDATE as the name implies it updates or modifies the data in
the data structure.
11. Other operations performed on a data structures are ;
• Searching
• Sorting
• Merging
• Searching operation finds the presence of the desired data item in
the list of data item.It may also find the locations of all elements
that satisfy certain condition.
• Sorting is the processes of arranging all data items in a data
structure in a particular order say for example, either ascending or
descending.
• Merging is a process of combining the data items of two different
sorted lists into single sorted list.
12. Arrays-Memory Allocation And Implementation
• An array can be defined as a finite collection of homogeneous
elements.
There are some important points to be pointed out about arrays.
• Arrays are always stored in consecutive memory locations.
• An array can be stored multiple values which can be referenced
by a single name unlike a simple variable which store one value at
a time followed by an index or subscript, specified inside a square
bracket.
13. • Array name is actually a pointer to the first location of
the memory block allocated to the name of the array
• An array either be a integer, floating point ,character
data type can be initialized only during declaration time,
and not afterwards
• There is no bound checking concept for arrays in C++.
14. Advantages of Array
• Array provides the single name for the group of variables
of the same type therefore, it is easy to remember the
name of all the elements of an array.
• Traversing -> we just need to increment the base address
of the array in order to visit each element one by one.
• Any element in the array can be directly accessed by
using the index.
15. Array Limitations
• The prior knowledge of number of elements in the linear
array is necessary
• There are static structures -> memory is allocated at
compilation time, memory used by them cant be reduced
• Insertion and deletion are time consuming.
16. Memory Allocation of the array
• All the data elements of an array are stored at contiguous
locations in the main memory.
• The name of the array represents the base address or the
address of first element in the main memory.
• Each element of the array is represented by a proper indexing.
The indexing of the array can be defined in three ways.
• 1.0 (zero - based indexing) : The first element of the array will be
arr[0].
• 2.1 (one - based indexing) : The first element of the array will be
arr[1].
• 3.n (n - based indexing) : The first element of the array can reside
at any random index number.
18. One dimensional arrays
• A one dimensional array is one in which only one subscript specification is
needed to specify a particular element of the array. One dimensional array
can be declared as follows:
data_type var_name[Expression];
• data_type is the type of elements to be stored in the array.
• var_name specifies the name of array.
• The Expression or subscript specifies the number of values to be stored in
the array.
• Arrays are also called subscripted values .The subscript must be an integer
value and the subscript of the array starts from 0 onwards.
• The size of array can be determined by
• Size=(upperbound – lowerbound)+1
19. ARRAY OPERATIONS
Following are the basic operations supported by an array.
• 1.Traverse − print all the array elements one by one.
• 2.Insertion − add an element at given index.
• 3.Deletion − delete an element at given index
• 4.Merge –Combine two arrays
• 5.Sorting – arranging the elements
• 6.Searching
20. 1.Array Insertion
• Insertion of new elements in an array can be done in two ways:
• a.Insertion at the end of array
• b.Insertion at required position
• Inserting an element at the end of an array can be easily done
provided the memory space allocated for the array is large
enough to accommodate the additional element.
• For inserting the element at required position, elements must be
moved downwards to new location. To accommodate new
element and keep the order of the elements.
21. Algorithm for inserting new element at required location
INSERT(a,n,pos,item)
1.Set i:=n-1
2.Repeat for i=n-1 to pos-1
a.Set a[i+1]=a[i]
b.Set i=i-1
3.Set a[pos-1]=item
4.Set n=n+1
5.Exit
22. Array Deletion
• For deleting an element from the array, the logic is
straightforward.
• Deleting an element at the end of an array presents no
difficulties.
• Deleting element somewhere in the middle of the array
would require to shift all the element to fill the space
emptied by the deletion of the element, then the elements
following it were moved upward by one location .
23. Algorithm for deleting element at required location
DELETE (a,n,pos)
1.Set item=a[pos-1]
2.Repeat for i=pos-1 to n-1
a.Set a[i]=a[i+1]
b.i=i+1
3.set n=n-1
4.exit
24. 3.Traversing of Array
• Traversing means to access all the elements of the array,
starting from first element upto the last element in the
array one-by-one.
Algorithm
Traverse(a,LB,UB)
• 1.set i=LB
• 2.repeat i=LB to UB
• a.display a[i]
• b.set i=i+1
• 3.exit
25. Merging of Arrays
• The merging means combining elements of two arrays to form a
new array. Simplest way of merging two arrays is that first copy
all the elements of first array into third empty array and then copy
all the elements of the second array into the third array. The
elements of second array are copied into the third array after the
position at which the last elements of the first array are copied.
• The another way is to sort the array during merging. This
technique i.e., sorting while merging is more efficient as other
techniques require sorting of resultant array even if the two source
arrays are sorted beforehand.
26. Algorithm
1. Create three arrays array1,array2 and array3
2. Enter the elements in ascending order in array1 and array2
3. If (array1[m] ≤ array2[n])
a. Array3[c]=array1[m]
b. c=c+1
m=m+1
4. Else
a. Array3[c]=array2[n]
b. c=c+1
n=n+1
5. Assign remaining elements of array1 and array2 to array3
6. Display the elements of array3
7.Exit
27. TWO DIMENSIONAL ARRAY
• The two dimensional array can be declared in the same
manner as one dimensional array except that a separate
pair of bracket is required for the second dimension.
• i.e., datatype arrayname[rowsize][columnsize];
• Matrix is the best example for a two dimensional array. A
two dimensional array can be implemented in a
programming language in two ways:
• 1)Row major implementation
• 2)Column major implementation
28. Row major implementation
• It is a technique in which the element s of the array are
read row-wise, i.e., the first row is completely stored
followed by the second row.
• Eg:- consider a 3*3 matrix
• 2 3 4
• 5 6 7
• 8 9 10
30. Address of elements of row major implementation
• The address of the element a[i][j] is given as
• A[i][j]=B+w[n(i-l1)+(j-l2)]
• where,
• B is the base address, w is the size of each array
element, n is the number of columns, i and j is the row
and column of the location to be determined l1 and l2 is
the lower bound of the row and column of the given two
dimensional array.
• OR a[i][j] = B + w[n(i)+j]
31. Eg :- Each element of the array data[20][50] requires 4
bytes of storage. The base address of the array data is
2000. Determine the location of data[10][10] when the array
is stored as row major.
• a[10][10] = B + w[n(i-l1)+(j-l2)]
• = 2000 + 4[50(10-0)+(10-0)]
• = 2000 + 4[500+10]
• = 2000 + 2040
• = 4040
32. • Column major implementation
In column major implementation the memory allocation is
done column wise, i.e, first the elements of the first column
is stored followed by the elements of the second column.
• Eg :- Consider a 3*3 matrix
• 2 3 4
• 5 6 7
• 8 9 10
34. • The address of the element a[i][j]
• A[i][j] = B + w[(i-l1)+(j-l2)*m]
• Where m is the number of rows
• Eg :- Each element of the array data[20][50] requires 4 bytes of
storage. The base address of the array data is 2000. Determine
the location of data[10][10] when the array is stored as column
major.
• a[i][j] = B + w[(i-l1)+(j-l2)*m]
• = 2000 + 4[(10-0)+(10-0)*20]
• = 2000 + 4[210]
• = 2000 + 840
• = 2840
35. • Eg :- A two dimensional array a[5][4] is stored in the memory and the first
element of the array occupies the location 80. Find the memory of a[3][2] if
each element requires 4 bytes of memory. Find the memory allocated for
a[3][2] both in the case of row major and column major implementation.
• Row major
• a[i][j] = B + w[n(i-l1)+(j-l2)]
• = 80 + 4[4(3-0)+(2-0)]
• = 80 + 4[12+2]
• = 80 + 56
• = 136
• Column major
• a[i][j] = B + w[(i-l1)+(j-l2)*m]
• = 80 + 4[(3-0)+(2-0)*5]
• = 80 + 4[13]
• = 132
36. Array Applications
Sparse Matrix
• An m x n matrix A is said to be sparse if many of its
elements are zero. i.e. matrices with a relatively high
proportion of zero entries are called a sparse matrix.
• A matrix that is not sparse is called Dense Matrix.
37. Why to use Sparse Matrix instead of simple matrix ?
• Storage: There are lesser non-zero elements than zeros
and thus lesser memory can be used to store only those
elements.
• Computing time: Computing time can be saved by
logically designing a data structure traversing only non-
zero elements.
38. • Representing a sparse matrix by a 2D array leads to
wastage of lots of memory as zeroes in the matrix are of
no use in most of the cases.
• So, instead of storing zeroes with non-zero elements, we
only store non-zero elements.
• This means storing non-zero elements with 3 triples
method- (Row, Column, value).
39. Polynomial
• Polynomial Representation and addition
• Polynomials are told to be symbolic as it contains the list of
coefficients and exponents in any given polynomial.
• A polynomial is defined to be a sum of terms where each term is
of the form axe ,here ‘x’ is a variable,’ a’ is the coefficient and ‘e’ is
the exponent.
• A general polynomial A(x) is of the form,
• anxn+an-1xn-1+…+a1x +a0 where an ≠0,the degree of a is n.
40. • Example: Consider two polynomials A(x) = 3x2 +2x + 4 and
B(x) =x4 +10x3 +3x2 +1
A(x)
B(x)
• In the array representation of the polynomial it uses 2m+1
locations, where m is the number of terms in the polynomial and
the first entry in the array represents the total number of terms.
• The resultant polynomial C contains 2(m + n)+1 locations.
3 2 3 1 2 0 4
4 4 1 3 10 2 3 0 1
41. Procedure PolyADD (A, B, C)
// A (1:2m+1), B (1:2n+1), C (1:2(m+n) +1) //
m ← A(1);
n ← B(1);
p ← q ← r ← 2; //pointers to three arrays
While p<=2m and q<=2n do
case
: A (p) = B (q) : C (r+1) ← A (p+1) + B (q+1)
if C (r+1) ≠ 0
then [C(r) ←A (p); r ← r + 2]
p ← p + 2 ; q ← q + 2
: A (p) < B (q) : C (r+1) ← B (q+1); C(r) ←B(q)
q ← q + 2; r ← r + 2
: A (p) > B (q) : C (r+1) ← A (p+1) ;C(r) ←A(p)
p ← p + 2; r ← r + 2
End
End
42. While p <= 2m do
C( r) ←A (p);
C(r+1) ←A (p+1)
p ←p+2; r← r+2;
End
While q<=2n do
C( r) ←B (q);
C(r+1) ←B (q+1)
q ←q+2; r← r+2;
End
C (1) ← r/2
End PolyADD
43. STACKS
• Stack is a linear data structure.
• A stack is defined as an ordered collection of
homogeneous data elements where insertions and
deletions take place at only one end known as TOP of the
stack.
• Since the item that was inserted last is the item that will
be deleted first, it is also called a Last in First out (LIFO)
data structure.
44. The difference of stack between array and linear
linked list are:
• 1)In an array and linear linked list insertions and deletions are
possible at any position, where as in a stack the insertions and
deletions are possible at only on end of the data structure
called TOP, which is a pointer pointing to the top item of the
stack.
• 2)An insertion operation referred as PUSH operation and
deletion operation is called POP operation.
• 3)Each element in a stack is referred as ITEM.
• 4)The maximum number of items in a stack is termed as SIZE
of the stack.
45. • For example a stack can be represented in Figure: 3.1,
Here the top of the stack contains the element A. The
bottom of the stack contains P.
A
B
C
P
Top
PUSH POP
46. Concept of Stack (Representation of stack)
• Representation of stack refers to the storage of the stack
data structure in memory.
• A stack is represented in memory mainly in 2 ways:
• a)Using one dimensional array(Static representation)
• b)Using Singly linked list (Dynamic representation )
47. Array representation of stack
• Memory block of sufficient size has to be allocated, to
accommodate the full capacity of stack.
• Items of the stack are stored in sequential order, from the starting
position.
• ITEMi denotes ith item in the stack, ‘l’ and ‘u’ denotes index range
of array.
• Two status conditions of a stack in array representation are :
• EMPTY : TOP < l
• FULL : TOP >= u - l +1
49. Stack data structure in Array
Algorithm
CREATE ( ) : : = declare STACK ( 1:n ) ; top = 0
ISEMTS (STACK) : : = if top = 0 then true
Else false
TOP (STACK) : : = if top = 0 then error
Else STACK ( top )
50. OPERATIONS ON STACK
• The two main operations done on a stack is PUSH
(Insertion) and POP (Deletion) operation.
51. Procedure PUSH_ARRAY ( item , STACK , n , top )
//insert item into the STACK of maximum size n; top is the
number of elements currently in STACK //
• if top ≥ n then
• call STACK_FULL
• top ← top + 1
• STACK ( TOP ) ← item
• End PUSH
52. Procedure POP_ARRAY (item, STACK, top)
// removes the top element of STACK and stores it in item,
unless STACK is empty //
• if top ≤ 0 then
• call STACK_EMPTY
• item ← STACK ( top )
• top ← top – 1
• end
53. Algorithm STATUS_ A ( )
// To check the status of a stack implemented in the array //
If TOP < 1 then
Print “Stack is empty “
Else
If ( TOP >= SIZE ) then
Print “Stack is Full “
Else
Print “The Element at TOP is”, A [TOP ]
Free = (SIZE – TOP ) / SIZE * 100
Print “Percentage of free stack is’, Free.
Endif
Endif
Stop
54. CONVERSION OF INFIX EXPRESSION TO
POST FIX EXPRESSION
• The stack can be used for conversion of an infix expression to
postfix and prefix forms, and is also used in the evaluation of the
postfix and prefix forms.
• An Expression is made up of operands, operators and delimiters.
• For Example, The expression, A / B ^ C + D * E – A * C has five
operands, namely A, B, C, D, and E. The basic arithmetic
operation include ( *, /, +, -, ^ ) and the relational operators include
( <, ≤, =, ≠, ≥, > ).
• Consider an Arithmetic expression, a + b / 3, If a= 9 and b = 6,
The first result is calculated as 9 + (6 / 3 ) = 9 + 2 = 11.The
second result is calculated as ( 9 + 6 ) / 3 = 15 / 3 = 5.
55. Evaluation:
• In the expression ( a + b )/3, the expression inside the
parenthesis is evaluated first.
• Therefore the expression inside the parenthesis has the
highest priority. The list of operators include : ‘ + ‘, ‘ – ‘, ‘ * ‘
, ‘ / ‘ , and ‘ ^ ’.
• In order to perform the evaluations each operator is
assigned with priority number as per the precedence list.
56. Forms of arithmetic Expression
• Representation of an arithmetic expression E, depends on
the position where the operators are written.
• The three ways of representing an arithmetic expression
are:
• 1)Infix Expression.
• 2)Prefix Expression.
• 3)Postfix Expression.
57. Infix Form
• When the Expression E is written in the form where the
operators are in between the operands is called Infix form.
This is the normal way of expressing an expression.
• E.g.: E = A + B
• Infix Form = A + B
58. Prefix Form
• The prefix expression refers to writing operators before
the operands.
• E.g.: E = A + B
• Prefix Form = + A B
59. Postfix Form
• A postfix form is the way of writing an expression E,
where operator is written after the operands.
• E.g.: E = A + B
• Postfix Form = A B +
60. 1)INFIX: A + B / C – D * E + F
PREFIX: A + { / B C } – { * D E } + F
+ { A / B C } - { + * D E F }
-+ { A / B C } { + * D E F }
-+ A / B C + * D E F
POSTFIX: A + { B C / } – { D E * } + F
{ A B C / + } – { D E * F + }
A B C / + D E * F + -
61.
62.
63. Procedure EVAL ( E )
// evaluate the postfix expression E, it is assumed that the last
character in E is an ‘ ‘ .A procedure NEXT_TOKEN is used to
extract from E the next token. A token is either an operand, operator,
or ‘ ‘ .
A one-dimensional array STACK ( 1 : n ) is used as a stack //
top ← 0 //initialize STACK //
loop
x ← NEXT_TOKEN ( E )
while ( x ≠ ‘NULL‘ ) do
If ( x = operand ) then
PUSH ( x , STACK , n , top )
64. Else
op = x
y = POP ( )
x = POP ( )
t = x op y
PUSH ( t , STACK , n , top )
End if
End While
Value = pop ()
End EVAL
65.
66.
67. QUEUE
• Queue is a linear data structure, which is an ordered collection of
homogeneous data elements.
• insertion and deletion operations take place at two extreme ends called
as REAR and FRONT.
• Queue insertion is called ENQUE and deletion operation is called
DEQUE.
• The Enqueue operation is done at an end called REAR and dequeue
done at the other end called FRONT end.
68. • Elements in the queue are termed as ITEM. The number of elements
that a queue can accommodate is termed as its LENGTH.
• A Queue is a data structure where data in the queue is processed in the
same order as it is entered, therefore called as First in First out ( FIFO )
structure.
• Queues arise in the computer solution of many problems. The most
common occurrence of the queue in computer applications is for the
scheduling of jobs.
• In batch processing, the jobs are queued up as they are read in and
executed one after another in the order they were received.
69. REPRESENTATION OF QUEUE
• The queue data structure can be implemented in two ways in the
computer memory as:
i) Implementation using Array.
ii)Implementation using Linked List.
70. Implementation of a linear queue using arrays
• In the array representation of the Queue one – dimensional array is used,
where it uses fixed size structures. In the 1-d array form Q[1.....N ] is used
to represent a queue. The pointer FRONT and REAR is used to indicate the
two ends of the queue.
• The three different states of the queue represented are:
1)Queue Empty
• FRONT = 0
• REAR = 0
1)Queue is full
• REAR = N
• FRONT = 1
1)The elements are present if FRONT <= REAR. The total number of
elements are REAR – FRONT + 1
71. Algorithm: (Algorithm to add an item into Queue)
procedure ADDQ ( item , Q , n , rear)
// insert item into the Queue represented in Q(1:n) //
if rear = n then
call QUEUE_FULL
Exit.
else
rear rear +1
Q ( rear ) item
Endif.
end ADDQ
72. Algorithm:(Algorithm to delete an item from Queue)
procedure DELETEQ ( item ,Q , front , rear )
// delete an element from a queue //
if front = rear = 0 then
call QUEUE_EMPTY.
Else
item Q ( front )
if ( front = rear)
front = rear = 0
else
front front + 1
End DELETEQ
73. CIRCULAR QUEUE
• In the array representation of a queue using array, the disadvantage is
that, when the rear pointer reaches the end, i.e., last position of the array,
the insertion is no more possible, even though space is available in the
front.
• This problem is solved by the concept of circular queue representation.
74. • The circular Queue array CQ[1….N ], implies A[1] comes after A[N] or
A[N] before A[1].the pointers in the circular Queue move in a clockwise
direction, controlled by MOD operation. From position ‘ i ‘ to ‘i+1’, the
pointer is shifted to the next location using the operation i MOD LENGTH
+ 1, where 1 <= i <=LENGTH, LENGTH refers to queue length. When, i =
LENGTH, the next position of i is i = 1.The two cases of Circular Queue
are:
1)Circular Queue is empty
• FRONT = 0
• REAR = 0
1)Circular Queue is Full
• FRONT = ( REAR MOD LENGTH ) + 1
75. • In order to add an element, we have to move the rear one position
clockwise.
• if rear = n-1 then rear = 0;
• else rear = rear +1;
• The above statement is same as rear = ( rear + 1 ) % n. Similarly in
order to delete an element it is necessary to move front one position
clockwise. i.e., if front = n-1 then front = 0
• Else front = front + 1 , is same as Front = ( front + 1) % n.
76. Algorithm (Algorithm to Add a new element to the circular queue)
Algorithm ENCQUE (ITEM)
If ( FRONT = 0 )then
FRONT = 1
REAR = 1
CQ [REAR ] = ITEM
Else
REAR = ( REAR MOD LENGTH ) +1
If ( REAR ≠ FRONT ) then
CQ [ REAR ] = ITEM
Else
Print “Queue is FULL”
Endif
Endif
Stop.
77. Algorithm (Algorithm to Delete an element in the circular queue)
Algorithm DECQUE ( )
If ( FRONT = 0 )then
Print “Queue is Empty “
Exit.
Else
ITEM = CQ [ FRONT ]
If ( FRONT = REAR ) Then
FRONT = 0
REAR = 0
Else
FRONT = ( FRONT MOD LENGTH ) + 1
Endif
Endif
Stop.
78. DEQEUE
• The Data Structure DEQEUE is a type of Queue, is a structure where
both insertions and deletions can be made at either ends of the structure.
• Therefore it is known as Double Ended Queue.
DELETION DELETION
INSERTION INSERTION
Figure: 3.22 Deque Structure with Insertion and Deletion Operations on both the ends.
79. • The operations possible on a deque are:
1)Insert at Rear.
2)Insert at Front.
3)Delete at Rear.
4)Delete from Front.
80. 1)Input restricted Deque: It is a deque, that allows insertions at one end
called REAR, but allows deletions from both ends i.e., from FRONT and
REAR ends.
• The operations are:
a) Delete at FRONT
b)Delete at REAR
c) Insert at REAR.
81. 1)Output restricted Deque: It is a deque, that allows deletion at one end
called FRONT end, but allows insertions at both the ends, i.e., FRONT
and REAR ends.
• The operations are:
a) Delete at FRONT
b)Insert at REAR
c) Insert at FRONT.
82. Input restricted Deque
• The operations illustrated are Deletion at FRONT and REAR, and
Insertion at REAR.
• All the operations are performed using a circular linked Array of Length
as DQ[ 1……….LENGTH ].
• It is called as Input Restricted, because here the Input Operation is
restricted and is possible only at the REAR end.
DELETION DELETION
INSERTION
Figure: 3.23 Input restricted Deque .
FRONT REAR
83. Algorithm to insert an item at the REAR end of a
Input restricted Deque, DQ
Algorithm INSERT_REAR_DQ ( ITEM )
If ( FRONT = 0 ) then
FRONT = 1
REAR = 1
DQ [ REAR ] = ITEM
Else
next = REAR +1
If ( next ≠ FRONT ) then
REAR = next
DQ [ REAR ] = ITEM
85. Algorithm DELETE _FRONT_DQ ( )
If ( FRONT = 0 )then
Print “Queue is Empty “
Exit.
Else
ITEM = DQ [ FRONT ]
If ( FRONT = REAR ) then
FRONT = 0
REAR = 0
Else
FRONT = FRONT + 1
Endif
Endif
Stop.
86. Algorithm (To delete the item pointed by the pointer REAR
of a Deque, DQ)
Algorithm DELETE _REAR_DQ ( )
1: If rear=0
print(“Cannot delete value at rear end”);
2: Else
no=q[rear];
if front= rear
front=0;
rear=0;
else
rear=rear-1;
print(“Deleted element is”, no);
3: Exit
88. Output restricted Deque
• The operations illustrated are Insertion at FRONT and REAR, and
deletion at REAR.
• All the operations are performed using a circular linked Array of
Length as DQ[ 1……….LENGTH ].
• It is called as Output Restricted, because here the Output Operation is
restricted and is possible only at the FRONT end.
89. Algorithm to delete the item pointed by the pointer
FRONT of a Output restricted Deque, DQ
Algorithm DELETE _FRONT_DQ ( )
If ( FRONT = 0 )then
Print “Queue is Empty “
Exit.
Else
ITEM = DQ[ FRONT ]
If ( FRONT = REAR ) then
ITEM = DQ [ REAR ]
FRONT = 0
REAR = 0
Else
FRONT = FRONT + 1
Endif
Endif
Stop.
90. Algorithm to insert an item at the FRONT end of an
Output restricted Deque, DQ
Algorithm INSERT_FRONT_DQ ( ITEM )
If ( FRONT = = 0 ) then
FRONT = 1
REAR = 1
DQ [ REAR ] = ITEM
Else
FRONT =FRONT – 1
If(FRONT!=0)
DQ[ FRONT ] = ITEM
Else
Print “Insertion at front not possible”
Endif
91. Algorithm to insert an item at the REAR end of a
Output restricted Deque, DQ
Algorithm INSERT_REAR_DQ ( ITEM )
If ( FRONT = 0 ) then
FRONT = 1
REAR = 1
DQ [ REAR ] = ITEM
Else
Next = ( REAR MOD LENGTH ) +1
If ( next ≠ FRONT ) then
REAR = next
DQ [ REAR ] = ITEM
Else
Print “ Queue is Full “
Endif
Endif
Stop.
92. PRIORITY QUEUE
• Priority Queue is a type of queue, where each element is assigned a
value, called Priority of the element.
• The insertion and deletion is not strictly based on the FRONT and
REAR position, but at any position on the queue.
• All the insertions and deletions are based on its priority number
assigned ‘Pi’ of any element ‘X’, therefore priority queue does not strictly
follow FIFO principle.
• In a priority Queue,
1)An element of higher priority is processed before any element of lower
priority.
2)Two elements of same priority are processed according to the order in
which they are added to the queue.
93.
94. • With this implementation, an element X of priority pi may be deleted
before an element which is at FRONT.
• Similarly, insertion of an element is based on its priority, i.e., instead of
adding it after the REAR it may be inserted at any position dictated by
its priority value.
• Priority queue is not a queue since it doesn’t follow FIFO principle.
Representation of priority queue
• A priority queue can be implemented in the following ways:
i) Using simple /circular array.
ii)Multi Queue Implementation.
iii)Using a double linked list.
iv)Using heap tree structure.
95. Priority queue using an array
• With this an array is maintained to hold the item and its priority value.
• The element will be inserted at the rear end as usual. The deletion
operation will then be performed either of the following two ways.
a. Starting from the FRONT end, if the element to be deleted is the front
most element, delete it, otherwise traverse the array for an element of the
highest priority and Delete this element from the queue shifting all its
trailing elements after the deleted element one position each to fill up the
vacant position.
96.
97. Disadvantage:
• This implementation is very inefficient as it involves the searching
the queue for the highest priority element and shifting the trailing
elements after the deletion.
98. • The second method solves the problem in the first method and
insertion is done at the REAR end as usual case, Using a stable
sorting algorithm, the elements of the queue are sorted, so that the
highest priority elements is at the FRONT end.
• When a deletion is required, delete it from the FRONT end only.
99.
100. Multi queue implementation
• This implementation assumes n different priority values.
• For each priority pi there are two pointers Fi and Ri corresponding to
FRONT and REAR pointer respectively. The elements between Fi and Ri
are all of equal priority value Pi.
101. • Disadvantages of multi queue representation:
a. To insert an item, it requires large number of shifting.
b. Large numbers of pointers are needed to represent it, as the number of
queue increases.
102. The alternate representation techniques are:
i) Multiple queues with simple queues-The multiple queue structure
maintains multiple number of queues, each with its own front and rear
pointers where each queue maintains elements of same priority. The
advantage of a multiqueue representation is that queues of different
arbitrary length can be maintained.
ii)Matrix representation-This structure maintains multiple queues in a 2-d
matrix format each with its own priority number.
Disadvantage: The memory utilization of the above representations is
very low as the majority of memory space remains vacant.
103. APPLICATION OF QUEUE
• The Data structure queue has great importance in various aspects of the
operating system, for example in multiprogramming environment, it uses
several queues to control the various programs, and also, queues are
useful to implement various scheduling algorithms.
• The two major applications in the system side include:
1. CPU scheduling in multiprogramming Environment.
2. Round Robin Algorithm.
104. CPU scheduling in multiprogramming
Environment
• In a multiprogramming environment, a single CPU has to serve more
than one program simultaneously. In a multiprogramming environment, the
possible jobs to the CPU are categorized into three groups.
a) In a multiprogramming environment, the Interrupts to be serviced, which
are service requests by a variety of devices and terminals connected to
the CPU, at any time produced.
b)Interactive users to be serviced, mainly programs at various terminals
under execution.
c) Batch jobs to be serviced, which are long term jobs. E.g.: print
document.