You are on page 1of 26

Data Structure 1

_______________________________________________________________________
_

Data Structure and Algorithm Analysis

Data
Data is derived from a Latin word “Datum” which means collection.
So data can be defined as collection of facts and figures.

Data is classified into two types.

1) Group data Item


2) Elementary data Item

1) Group Data Item


Data item that can be subdivided into different segments is called
group data item.
For example, name is a group data item because it can be
subdivided into different segments i.e. First name, middle name,
last name

2) Elementary Data Item


The data item that cannot be subdivided into different segments is
called elementary data item
For example, Account No, ID Number etc.

Field
Field is the collection of related character.
For example, Name, Roll No, Class etc.
A single field cannot provide full information about any entity.

Record
Record is the collection of related fields.
For example, the record of student includes its Roll No, Name, Class, and
Registration No. etc

Table/ Relation/ File


Collection of related records is called a file or table.
For example

Rno Name Class


1 Muhammad 3rd Year
2 Imran 3rd Year
3 Hussain 3rd Year

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 2
_______________________________________________________________________
_
Data Structure
Data structure is a mathematical way for storage of data in the
computer memory.
It is the way of storing and accessing the data from the computer memory. So
that large number of data is processed in small interval of time.

Types of Data Structure


There are two types of data structure
1) Linear Data Structure
2) Non Linear Data Structure

1) Linear Data Structure


When the data is to be stored in the memory in linear form is called
linear data structure.
For example, Array, Stack, Queue, Link List etc.

2) Non Linear Data Structure


When the data is to be stored in the computer memory in
dispersed order is called non-linear data structure.
For example, trees, files, graphs etc.

Classification of Linear Data Structure


It can be classified into two types.
1) Physical Data Structure
2) Logical Data Structure

1) Physical Data Structure


When the data is to be stored in the computer memory in
proper order is called physical data structure.
Example: -
When we insert the data in one-dimensional array, then the data is
stored in the memory in that way, as we insert the data in sequence.

2) Logical Data Structure


The two-dimensional array is called logical data
structure because we are not sure, that in which format data in stored in
the memory, then we represent the data in the memory at row major order
or column major order, so due to this reason it is called logical data
structure.

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 3
_______________________________________________________________________
_
Operation Perform on Data Structure
When data is proceeding then different type of operation
perform. Following are most important operation, which play major
role in data structure.
1) Traversing
2) Searching
3) Sorting
4) Insertion
5) Deletion
6) Merging
7) Copying

1) Traversing
Accessing of records is called traversing. It is also known as
visiting of records.

2) Searching: -
Finding the location of record with given value is called
Searching.

3) Sorting
The arrangement of data in ascending or descending order
is called sorting.

4) Insertion
Adding new record to structure is called inserting.

5) Deletions
Removing a record or set of records form data structure is
called deleting process.
6) Merging
When two or more than two records are combined, this
process is called merging.
7) Copying
The creation of duplicate data item is called copying
process.

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 4
_______________________________________________________________________
_
File
Collection of logical related records is called file. It is a set of
records that is stored on secondary storage devices like magnetic tap,
tape drives, floppy disc, hard disc, flash etc, when created.
There are three types of files.
1) Sequential File
2) Index sequential file
3) Random/ Relative file

1) Sequential File
In sequential file, the records are stored in sequential
order, in which they are inserted.
This type of file is can be stored on magnetic tape, magnetic disc and hard
disc.

2) Indexed Sequential File


In this file separate key is defined. The key uniquely
identifies the record. By using proper indexing the duplication of record is
not possible.
In index sequential file, the records can be accessed in sequential order
also.
Index sequential file cannot be processed by magnetic tape. It can be
processed by magnetic disc.

3) Random/ Relative File


In random file, the data can be stored randomly and the
record can be accessed randomly also. It is fast file processing as
compared to other file organization.
Random file can be processed by magnetic disc.

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 5
_______________________________________________________________________
_
Algorithm
Algorithm is derived from the name of Muslim scientist Abu
Jafir Muhammad Iban-e-musa Al-Khurzami.
Algorithm is a step-by-step process to solve a specific problem.
Algorithm is not only the base of data structure but it is also the base of
good programming.

Criteria of Algorithms
To prepare any algorithm for any problem, the algorithm
must follow these criteria.
1)Input 2)Output 3)Definiteness 4)Finiteness 5)Effectiveness

1) Input
Each and every algorithm must have input. It may be zero or
some thing else, i.e. input is necessary.
2) Output
The algorithm will generate result after processing the input.
This result is called output
3) Definiteness
Each and every instruction must be cleared and simple,
that can be understand by any body else. This phenomenon is called
definiteness.
4) Finiteness
Each algorithm must have proper starting and ending. I.e.
the sequence of steps must be stopped after giving the suitable result.
5) Effectiveness
Before preparing any algorithm, firstly it should be sketch on the
papers

Algorithm Notation/ Parts of Algorithm


To write any algorithm these notations are used usually.
1) Name of algorithm 2) Introductory Comments
3) Comments 4) Steps
5) Variable handling 6) Control
7) Assignment Statements 8) Input and Output Statements 9)
Exit

1) Name of Algorithm
Each algorithm has a name related with subject. The
name of the algorithm is always written a capital latter in very first line of
algorithm.
Example:
Algorithm SUM (a,b,c)

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 6
_______________________________________________________________________
_

2) Introductory Comments
In this part of algorithm the whole purpose of problem
is to be mention. These are always enclosed in square brackets.
Example:
[The algorithm is used for the addition of three numbers]

3) Comments
These are always enclosed in square brackets. Here the purpose of
each step is described.
Example:
[Assign value to num1]

4) Steps
In this part, the programming instructions are used.
Example:
Num  5
Or Num=5
Or Read Num

5) Variable Handling
The variable name should be a valid identifier. The name of
variable may be upper case or lower case letter.

6) Control
The execution of steps is performed in sequential manner.

7) Assignment Statement
By using assignment statement the value is assign to any variable.
The “=” , “=:” , “” are used for assignment.

8) Input and Output Statements


Input statement is used to input the data for variables.
Its Syntax is
Read var1,var2,………. var-n
Or Input Var1,var2,………. var-n

Output statement is used to display the result. Its syntax is


Print Message/ Variable
Or Write Message/ Variable

9) Exit
When all the statements are executed are executed then the
algorithm terminates.
Its syntax is
Exit

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 7
_______________________________________________________________________
_
Example of algorithm

ALGORITHM FOR CALCULATE AVARAGE

Algorithm AVERAGE (m1, m2, m3, m4, average)


[This algorithm reads marks of four subject and the calculate the average
of these marks. Where m1, m2, m3 and m4 are used to store the marks of
four subjects respectively and an other variable “average” is used to store
the average of total marks.]

Step-1. [Input individual marks]

Read (m1, m2, m3, m4)

Step-2 [Calculate the average]

Average(m1+m2+m3+m4)/4

Step-3 [Output the Result]

Print average

Step-4 [Finished]

Exit

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 8
_______________________________________________________________________
_
Array

Array is the collection of finite homogeneous type of data, which are


stored in successive memory locations.
The memory locations are identifies by a name which is called the array
name. The data items of the array are called the elements of array and
each data item is stored in separate memory location.
Each memory location within the array is identifies by its position value,
which is called the index of the array.
110 112 114 116 118 120 Memory
Location
22 11 13 52 21 05
x[1] x[2] x[3] x[4] x[5] x[6] Element

index
Types of array
There are two types of array.
1) One dimensional array
2) Two Dimensional array
1) One Dimensional Array (Linear Array)
One-dimensional array represent the data in the form of
vector, list or linear form. It is used to store similar type of data
consisting of only one row or column.
110 112 114 116 118 120 Memory
Location
22 11 13 52 21 05
x[1] x[2] x[3] x[4] x[5] x[6] Element

index
2) Two Dimensional Array
Two-dimensional array represent the data in the form of
table or matrix.

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 9
_______________________________________________________________________
_
In two-dimensional array the storage of elements can be take place
by two techniques.
i) Row Major Order
ii) Column Major Order

Row Major Order


To understand the concept of row major order,
consider the following matrix of 2 X 3.
12 9 16
5 11 22
Now the storage of above matrix in row major order is
110 112 114 116 118 120
12 9 16 5 11 22
X(1,1) x(1,2) x(1,3) x(2,1) x(2,2) x(2,3)

Column Major Order


To understand the concept of column major order,
consider the following matrix of 2 X 3.
12 9 16
5 11 22
Now the storage of above matrix in column major order is
110 112 114 116 118 120
12 5 9 11 16 22
X(1,1) x(2,1) x(1,2) x(2,2) x(1,3) x(2,3)

Accessing/ Retrieving the elements of array from computer memory.


The elements of array can be accessed in four different
methods.
1) Dope Vector Method
2) Leiffe Access Method
3) Table Access Method
4) Hash Function

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 10
_______________________________________________________________________
_
1) Dope Vector Method
This method is used to access the elements of array by
using the proper formula, which access the memory address of the required
element.

Accessing of One-Dimensional Array Element


One-Dimensional array can be accessed by using the
following formula.
MA(i) = BA + w (i–1)
Where
MA = Required Memory Address
BA = Starting Memory Address
i = index
w = word size / Length
Example:
Consider the following array
110 112 114 116 118 120
22 11 13 52 21 05
x[1] x[2] x[3] x[4] x[5] x[6]
Now, suppose we have to find the memory address of x[4], then

MA(i) = BA + w (i-1)
MA(4) = 110 + 2 (4 –1)
= 110 + 6
= 116
So the required memory address of x[4] is 116 and the required element is
52.

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 11
_______________________________________________________________________
_
Accessing of Two-Dimensional Array Element
The element of two-dimensional array can be accessed in two ways.
Accessing 2-D array in Row Major Order

To understand the concept of row major order, consider the


following matrix of 2 X 3.
12 9 16
5 11 22
Now the storage of above matrix in row major order is
110 112 114 116 118 120
12 9 16 5 11 22
x(1,1) x(1,2) x(1,3) x(2,1) x(2,2) x(2,3)
Suppose we have to access x(2,1) element memory location, then
we use the following formula.
MA(i,j) = BA + w[ n (i-1) + (j-1) ]
Where
“i” represent row and “j” represent column and “n” are the total
numbers of columns.
Now
MA(2,1) = 110 + 2[ 3 (2-1) + (1-1) ]
= 110 + 6
= 116
Which is required memory address.
Accessing 2-D array in Column Major Order
To understand the concept of column major
order, consider the following matrix of 2 X 3.
12 9 16
5 11 22
Now the storage of above matrix in column major order is
110 112 114 116 118 120
12 5 9 11 16 22
x(1,1) x(2,1) x(1,2) x(2,2) x(1,3) x(2,3)

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 12
_______________________________________________________________________
_
Suppose we have to access x(1,3) element memory location, then we use the
formula.
MA (i,j) = BA + w[ m(j-1) + (i-1) ]
Where “m” is the total number of rows.
Now
MA (1,3) = 110 + 2[ 2(3-1) + (1-1)]
= 110 + 8
= 118
Which is the required memory location.

2) Lieffe Access Method


This method is used to access the elements of array by
means of pointers.
To access 2-D array elements, there are two techniques that we can use,
are given below.
i) Follow Row to search Column
This technique is used when the elements of 2-D are store in row
major order. Now
12 9 16
5 11 22
Now the storage of above matrix in row major order is
110 112 114 116 118 120
12 9 16 5 11 22
x(1,1) x(1,2) x(1,3) x(2,1) x(2,2) x(2,3) Row No. Total
Elements
1 3
2 3
Suppose we have to access x(2,2) element, then
We search the column using the row.
ii) Follow Column to search Row
This technique is used when the elements of 2-
D are store in column major order. Now
12 9 16
_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 13
_______________________________________________________________________
_
5 11 22
Now the storage of above matrix in row major order is
110 112 114 116 118 120
12 9 16 5 11 22
x(1,1) x(1,2) x(1,3) x(2,1) x(2,2) x(2,3) Column No. Total
Elements
1 2
2 2
3 2

Suppose we have to access x(2,2) element, then


We search the row using the column.

3) Access Table Method


Dope vector method and Lieffe access method are used to access
the numeric values, where as these are fail to access String values.
So to access string value, access table method is used.
Now consider an array of string

IMRAN
HUSSAIN
VIRGO

Now the storage of this array in memory is


I M R A N H U S S A I N V I R G I O

String Starting Total Elements

Position
1 1 5
2 6 6
3 13 5

When a specific string is to be searched, then the control goes to directly at


specific string by means of pointer.

4) Hash Function

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 14
_______________________________________________________________________
_
Hash function is used to store or retrieve the array elements randomly.
The formula to calculate the hash value is
Hash = Element % Size of an array
A function that is used to transfer the elements into index table is called
hash function and the process is called hashing.
Example:
Consider an array of 100 elements that is used to store and retrieve
the elements. Now the structure of the array

00
1201 01
02

99

Now if we want to store the 1201 element in the array, then it can be
stored in its proper location. Now
Hash = 1201 % 100
= 01
So 1201 element will be stored at 01 location.
Now if another element likes 6601is to be stored in the array, then
Hash = 6601 % 100
= 01
Since the element is already available at 01 location, the next element
cannot be stored at the same location, so collision is occurred.
To remove this collision we have three techniques.
i) Linear Probing Method
ii) Rehash
iii) Bucket and Chaining

i) Linear Probing Method


In this technique, the element can be stored at first empty
location.
Suppose the element 6601 is to be stored in the array then
Hash = 6601 % 100
= 01
As at the 01 location, the element already exists, so the element will be
stored at next first empty location.

ii) Rehash
In this technique, if the element already exists, then the
second one can be stored by rehash.
Now consider the formula
Rehash = (Hash + Constant) % Array Size
Constant may be any fixed Odd number.
Suppose, 7201 element is to be stored in the array, then
_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 15
_______________________________________________________________________
_
Hash = 7201 % 100
= 01
Now again
Rehash = (01 + 03) % 100
= 04
So the element will be stored at location “04”
Note: -
If the value of the constant is “1” then the Rehash function behave
like Linear probing method.

iii) Bucket and Chaining


In this technique, multiple values can be stored at
same location.
Now we construct a bucket for the storage of
elements that can store5 element at same location.
So

Suppose 7301 is to be
stored in the array,
then
Hash =
7301 % 100
= 01
So it will be stored at 01 location, further we can store 4 other elements in
this location, and if the 6th element have to be stored at the same location
the, collision occur.
So to remove this collision we use Chaining process
In chaining process link list is created, which is dynamic data structure and
is collection of set of nodes.
Suppose, “1201” element is already stored in the array, and we have to
store “301” element in the array, then
Hash = 7301 % 100 = 01
So chain will be created, because “1201” element is already exist at location
01.
Now consider the structure,

1201 301 φ

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 16
_______________________________________________________________________
_

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 17
_______________________________________________________________________
_
TRAVERSING OF ARRAY ELEMENT

Algorithm Traversing (X, LB, UB, N)

[This algorithm is used to traverse the element of array “X” where


N = Total Number where N=UB
LB = Lower Bound
UB = Upper Bound]
I = Loop Counter
Step-1 [initialize the loop counter]
Repeat step-2 for I=LB to UB
Step-2 [Process the Element of Array]
Print  X [I]
Step-3 [Finished]
Exit

ALGORITHM FOR THE INSERTION OF ELEMENT AT THE END OF ARRAY.

Algorithm InsertionEnd (X, I, N, K, Item)


[This algorithm is used to insert the element at the end of array where
X = Array Name
I = Loop Variable
K = Position where element is to be stored
N = Total Element
Item = Element which is to be inserted. ]

Step-1 [Read an array using for loop]


Repeat Step-2 For I = 1 To N

Step-1 [Read Element]


Read X [I]
End of Loop

Step-3 [Input location where element is to be inserted]


Read (K)

Step-4 [Perform check for Position]


If (K<= N) then
Goto step-7

Step-5 [Insert the new element]


X[K]  Item
_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 18
_______________________________________________________________________
_

Step-6 [Reset the value of N]


N=N+1

Step-7 [Finished]
Exit

ALGORITHM FOR THE DELETION OF ELEMENT AT THE END OF ARRAY.

Algorithm DeletionEnd (X, I, N, K, Item)


[This algorithm is used to delete the element at the end of array where
X = Array Name
I = Loop Variable
K = Position where element is to be deleted
N = Total Element
Item = Element which is to be deleted ]

Step-1 [Read an array using for loop]


Repeat Step-2 For I = 1 To N

Step-1 [Read Element]


Read X [I]
End of Loop

Step-3 [Input location where element is to be Deleted]


Read (K)

Step-4 [Perform check for Position]


If (K= = N) then
Item  X[K]
Else
Goto step-6

Step-5 [Reset the value of N]


N N-1

Step-6 [Finished]
Exit

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 19
_______________________________________________________________________
_

Lists
The arrangement of elements in some sequence is called list. There are
two types of list.
i) Ordered List
ii) Unordered List

i) Ordered List
The proper sequence of elements is called ordered list. For example
The days of week, the month of year, seasons of year.

ii) Unordered List


In this type of list the storage of element is not take place in proper
order.
For example Stack, Queue, Dequeue, Circular Queue, Link List

Stack
Stack is that type of list in which insertion and deletion of elements
can take place from one end. The end of stack is called Top pointer.

Stack work on two principal.


LIFO (Last In First Out)
FILO (First In Last Out)

Operation on Stack
Different operation can be performed with stack.
1) Create Stack The creation of stack structure
2) Push Operation The insertion of element in the stack.
3) Pop Operation The Deletion of element from the stack
4) Full Stack If stack is full, False value is returned i.e Boolean value
5) Empty Stack If stack is empty, True value is returned i.e Boolean value

Two Major Operation of the Stack


1) Over Flow
When the stack is full and new element is inserted/ Push then
overflow condition occur.

2) Under Flow
When the stack is empty, and a deletion process is performed then
under flow condition occur.
Example
i) Pile of Trays in Cafeteria
ii) Stack of iron shorts
iii) Pile of bricks
iv) Pile of books in Library
_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 20
_______________________________________________________________________
_

Computer related Example


i) Expression, evolution in the stack process
ii) Calling functions/ Procedures and returned value stored in
stack
iii) Conversion of decimal to binary is also a stack process.

ALGORITHM FOR THE INSERTION OF ELEMENT IN THE STACK

Algorithm push (stack, N, item, Top)


[ This algorithm is used to insert the element in the stack where
Stack = Array
N = Total number of elements
Item = which is to be inserted
Top = Top pointer/ Counter ]

Step-1 [Check for overflow]


If (Top >= 4) then
Write (“ Over Flow”)
Goto step-4
End if

Step-2 [Increment the Top Pointer]


Top = Top + 1

Step-3 [Insert the element into stack]


Stack [Top] = item
Goto setp-1

Step-4 [Finished]
Exit
ALGORITHM FOR THE DELETION OF ELEMENT FROM THE STACK
Algorithm pop (stack, item, Top)
[ This algorithm is used to delete a element from the stack where
Stack = Array
Item = which is to be inserted
Top = Top pointer/ Counter ]

Step-1 [Check for under flow]


If (Top = 0) then
Write (“ Under Flow”)
Goto step-4
End if

Step-2 [Delete the item from stack]


Item  stack [Top]

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 21
_______________________________________________________________________
_
Step-3 [Decrement the Top Pointer]
Top = Top – 1
Goto step-1
Step-4 [Finished]
Exit

Queue
Queue is that type of list in which insertion can take place from one end
called (Rear) and deletion can take place from another end called (Front).
Queue works on the principle of first in first out (FIFO) i.e
List come First Serve and Last in last out (LILO).

Daily Life Example


i) The number of cars at police check
ii) People purchase tickets from cinema make a queue.
iii) People deposits their bills in bank make a queue.

Computer Based Example


i) When number of instructions are given for printing to
computer, then that instructions will be executed first that
was first assign to computer.

Operations on Queue
Different operations can be performed at queue as

1) Create Queue
By using this operation the structure of the queue is generated.

2) Enqueue
The insertion of element in the queue is called enqueue.

3) Dequeue
The deletion of element from the queue is called dequeue.

4) Empty Queue
It returns Boolean value true, if queue is empty.

5) Full Queue
It returns Boolean value false, if queue is full.

6) Destroy Queue
This operation deletes all the elements of the queue and also deletes the
structure of queue from the computer memory.

Major Operation on Queue

1) Overflow Condition
_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 22
_______________________________________________________________________
_
When no more elements can be inserted in the queue then this
condition is called overflow condition.
2) Underflow Condition
When queue is empty and no more elements can be deleted from
the queue then this condition is called underflow condition.

ALGORITHM FOR THE INSERTION OF ELEMENT IN THE QUEUE


Algorithm enqueue (Q, N, item, Rear, Front)
[This algorithm is used to insert the element in the queue where
Q = Queue Array
N = Total number of elements
Item = which is to be inserted
Front, Rear = Deletion and Insertion pointers

Step-1 [Check for overflow]


If (Rear >= N) then
Write (“ Over Flow”)
Goto step-5
End if

Step-2 [Increment the Rear Pointer]


Rear = Rear + 1

Step-3 [Insert the element at Rear position]


Q [Rear] = item

Step-4 [Reset Pointer]


If (Front = 0 ) then
Set Front = 1
End if
Goto step-1

Step-5 [Finished]
Exit

ALGORITHM FOR THE DELETION OF ELEMENT FROM THE QUEUE


Algorithm DelQueue (Q, item, Front, Rear)
[ This algorithm is used for the deletion of element from the queue where
Q = Array
Item = which is to be deleted

Step-1 [Check for under flow]


If ((Front = 0) or (Front > Rear)) then
Write (“ Under Flow”)
Goto step-4
End if
_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 23
_______________________________________________________________________
_

Step-2 [Delete the item from queue]


Item  Q [Front]

Step-3 [Set Front Pointer]


If (Front = Rear) then
Front  Rear 0
Else
Front  Front + 1
End if
Goto step-1

Step-4 [Finished]
Exit

Circular Queue
Circular queue provide flexible way for the insertion of element in a
circular way.
In queue we are nor sure that when the rear pointer goes to an end i.e
Rear = N , overflow condition occur, may be the same element from the front
are deleted, it is the major drawback of queue.
This drawback can be overcome with the help of circular queue.
In circular queue the insertion can be take place by using this sequence
1,2,3, …… N

Check for Underflow


When the front and rear pointer become equal that is
Front  Rear  0
Underflow condition may arise.

Check for Overflow


When Front = 1 and Rear = N then overflow condition may arise. OR
When Front  Rear + 1 then overflow condition may arise.

ALGORITHM FOR THE INSERTION OF ELEMENT IN CIRCULAR QUEUE


Algorithm Circularinsertion (CQ, N, item, Rear, Front)
[This algorithm is used to insert the element in CQ, where
CQ = Array
N = Final Limit
Item = which is to be inserted
Front, Rear = Deletion and Insertion pointers ]

Step-1 [Check for overflow]


If ((Front = 1) && (Rear = N) or (Front = Rear + 1) then
Write (“Over Flow”)
Goto step-4
End if
_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 24
_______________________________________________________________________
_

Step-2 [Reset Rear Pointer]


If (Front = Rear =0) then
Rear = Front = 1
Else if (Rear = N) then
Rear = 1
Else
Rear = Rear + 1
End if

Step-3 [Insert the element]


CQ [Rear] = item
Goto step-1

Step-4 [Finished]
Exit

ALGORITHM FOR THE DELETION OF ELEMENT FROM CIRCULAR QUEUE


Algorithm DeleteCQ (CQ, item, N, Front, Rear)
[This algorithm is used for the deletion of element from Circular queue where
CQ = Array
Item = which is to be deleted
N = Total number of element

Step-1 [Check for under flow]


If ((Front = Rear = 0)) then
Write (“Under Flow”)
Goto step-4
End if

Step-2 [Delete the item from Circular queue]


Item  CQ [Front]

Step-3 [Set Front Pointer]


If (Front = Rear) then
Front  Rear 0
Else if (Front = N) then
Front =1
Else
Front  Front + 1
End if
Goto step-1

Step-4 [Finished]
Exit

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 25
_______________________________________________________________________
_
Dequeue
It is a linear data structure. This queue is called double ended
queue. In this type of queue insertion and deletion of elements can be take
place from both ends.
In this queue, insertion and deletion of elements from Rear and Front can
take place on the principal of Right and Left pointer.
There are two types of dequeue.
1) input restricted queue
In this type of queue, insertion can take place only from one end
and deletion can take place from both ends.
2) Output restricted queue
In this type of queue, insertion can take place from both ends and
deletion can take place from one end.

Principal for Insertion


When the elements are inserted from Rear/ Right then Rear pointer
is incremented and when elements are inserted from Front/ Left then Front
pointer decremented.

ALGORITHM FOR THE INSERTION OF ELEMENT TO RIGHT IN DEQUEUE


Algorithm DQInsertRight (DQ, N, item, Rear, Front)
[This algorithm is used to insert the element to the right of DQ, where
DQ = Array
N = Final Limit
Item = which is to be inserted
Front, Rear = Deletion and Insertion pointers ]

Step-1 [Check for overflow]


If ((Front = 1) && (Rear = N) or (Front = Rear + 1) then
Write (“Over Flow”)
Goto step-4
End if

Step-2 [Reset Rear Pointer]


If (Rear =0) then
Rear = Front = 1
Else if (Rear = N) then
Rear = 1
Else
Rear = Rear + 1
End if

Step-3 [Insert the element]


DQ [Rear] = item
Goto step-1

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan
Data Structure 26
_______________________________________________________________________
_
Step-4 [Finished]
Exit

ALGORITHM FOR THE INSERTION OF ELEMENT TO LEFT IN DEQUEUE


Algorithm DQInsertLeft (DQ, N, item, Rear, Front)
[This algorithm is used to insert the element to the left of DQ, where
DQ = Array
N = Final Limit
Item = which is to be inserted
Front, Rear = Deletion and Insertion pointers ]

Step-1 [Set the Front Pointer]


If ( Front = 1 or Front = 0 ) then
Front = N
Else
Front = Front - 1
End if

Step-2 [Check for overflow]


If (Front = Rear) then
Write (“Overflow”)
Goto step-5
End if

Step-3 [Insert the element]


DQ [Front] = item

Step-4 [Set the Rear Pointer]


If (Rear = 0) then
Rear = N
End if
Goto step-1

Step-5 [Finished]
Exit

_________________________________________________________________________________________
By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan

You might also like