A Breif Notes On Data Structure by asifsohail465

VIEWS: 12,631 PAGES: 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) 2) Group data Item 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 1 2 3 Name Muhammad Imran Hussain Class 3rd Year 3rd Year 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
1) 2) 1) It can be classified into two types. Physical Data Structure Logical Data Structure

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. 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. 2)

_________________________________________________________________________________________ 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. Sorting The arrangement of data in ascending or descending order is called sorting.

3)

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. 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. 3)

_________________________________________________________________________________________ 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
1) 3) 5) 7) To write any algorithm these notations are used usually. Name of algorithm 2) Introductory Comments Comments 4) Steps Variable handling 6) Control Assignment Statements 8) Input and Output Statements Exit 1)

9)

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] Comments These are always enclosed in square brackets. Here the purpose of each step is described. Example: [Assign value to num1] Steps In this part, the programming instructions are used. Example: Num  5 Or Num=5 Or Read Num Variable Handling The variable name should be a valid identifier. The name of variable may be upper case or lower case letter. Control The execution of steps is performed in sequential manner. Assignment Statement By using assignment statement the value is assign to any variable. The “=” , “=:” , “” are used for assignment. 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

3)

4)

5)

6)

7)

8)

_________________________________________________________________________________________ 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 Location 22 x[1] 11 x[2] 13 x[3] 52 x[4] 21 x[5] 05 x[6] Element 112 114 116 118 120 Memory

index

Types of array
There are two types of array. 1) 2) 1) One dimensional array Two Dimensional array 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 Location 22 x[1] 11 x[2] 13 x[3] 52 x[4] 21 x[5] 05 x[6] Element 112 114 116 118 120 Memory

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) ii) Row Major Order Column Major Order

i)

Row Major Order To understand the concept of row major order, consider the following matrix of 2 X 3. 12 5 9 11 16 22

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

ii)

Column Major Order To understand the concept of column major order, consider the following matrix of 2 X 3. 12 5 9 11 16 22

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

Accessing/ Retrieving the elements of array from computer memory. The elements of array can be accessed in four different methods. 1) 2) 3) 4) Dope Vector Method Leiffe Access Method Table Access Method 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 22 x[1] 112 11 x[2] 114 13 x[3] 116 52 x[4] 21 x[5] 118 05 x[6] 120

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. i) 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 5 9 11 16 22

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

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. ii) 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 5 9 11 16 22

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

_________________________________________________________________________________________ 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 5 9 11 16 22

Now the storage of above matrix in row major order is 110 12 x(1,1) 112 9 x(1,2) 114 16 x(1,3) 116 5 x(2,1) 11 x(2,2) 118 22 x(2,3)
Row No. Total Elements

120

1 2 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 2D are store in column major order. Now 12 9 16

3 3

_________________________________________________________________________________________ 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 12 x(1,1) 112 9 x(1,2) 114 16 x(1,3) 116 5 x(2,1) 118 11 x(2,2) 120 22 x(2,3)
Column No. Total Elements

1 2 3 Suppose we have to access x(2,2) element, then We search the row using the column.

2 2 2

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 Position

Total Elements

1 2

1 6 13

5 6 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

3

_________________________________________________________________________________________ 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 01 02

1201

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 Hash = 7201 % 100
_________________________________________________________________________________________ By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan

Data Structure 15 ________________________________________________________________________ = 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 LB = Lower Bound UB = Upper Bound] I = Loop Counter [initialize the loop counter] Repeat step-2 for I=LB to UB Step-2 [Process the Element of Array] Print  X [I] Step-3 Exit
ALGORITHM FOR THE INSERTION OF ELEMENT AT THE END OF ARRAY.

where N=UB

Step-1

[Finished]

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 [Read Element] Read X [I] End of Loop [Input location where element is to be inserted] Read (K) [Perform check for Position] If (K<= N) then Goto step-7 [Insert the new element] X[K]  Item

Step-1

Step-3

Step-4

Step-5

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

Data Structure 18 ________________________________________________________________________ Step-6 [Reset the value of N] N=N+1 [Finished] Exit

Step-7

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 [Read Element] Read X [I] End of Loop [Input location where element is to be Deleted] Read (K) [Perform check for Position] If (K= = N) then Item  X[K] Else Goto step-6 [Reset the value of N] NN-1 [Finished] Exit

Step-1

Step-3

Step-4

Step-5

Step-6

_________________________________________________________________________________________ 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 Computer related Example
_________________________________________________________________________________________ By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan

Data Structure 20 ________________________________________________________________________ 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 [Increment the Top Pointer] Top = Top + 1 [Insert the element into stack] Stack [Top] = item Goto setp-1

Step-2

Step-3

[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 [Delete the item from stack] Item  stack [Top] [Decrement the Top Pointer] Top = Top – 1 Goto step-1

Step-4

Step-2

Step-3

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

Data Structure 21 ________________________________________________________________________ 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 When no more elements can be inserted in the queue then this condition is called overflow condition. 2) Underflow Condition
_________________________________________________________________________________________ By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan

Data Structure 22 ________________________________________________________________________ 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 [Increment the Rear Pointer] Rear = Rear + 1 [Insert the element at Rear position] Q [Rear] = item [Reset Pointer] If (Front = 0 ) then Set Front = 1 End if Goto step-1 [Finished] Exit

Step-2

Step-3

Step-4

Step-5

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 [Delete the item from queue] Item  Q [Front]

Step-2

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

Data Structure 23 ________________________________________________________________________ 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 [Reset Rear Pointer] If (Front = Rear =0) then Rear = Front = 1 Else if (Rear = N) then

Step-2

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

Data Structure 24 ________________________________________________________________________ Rear = 1 Else Rear = Rear + 1 End if

Step-3

[Insert the element] CQ [Rear] = item Goto step-1 [Finished] Exit

Step-4

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 [Delete the item from Circular queue] Item  CQ [Front] [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 [Finished] Exit

Step-2

Step-3

Step-4

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.
_________________________________________________________________________________________ By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan

Data Structure 25 ________________________________________________________________________ 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 [Reset Rear Pointer] If (Rear =0) then Rear = Front = 1 Else if (Rear = N) then Rear = 1 Else Rear = Rear + 1 End if [Insert the element] DQ [Rear] = item Goto step-1 [Finished] Exit

Step-2

Step-3

Step-4

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
_________________________________________________________________________________________ By: Haji Asif Sohail, BERGER Paints Pakistan Limited, Lahore Pakistan

Data Structure 26 ________________________________________________________________________ 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 [Check for overflow] If (Front = Rear) then Write (“Overflow”) Goto step-5 End if [Insert the element] DQ [Front] = item [Set the Rear Pointer] If (Rear = 0) then Rear = N End if Goto step-1 [Finished] Exit

Step-2

Step-3

Step-4

Step-5

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


								
To top