180 likes | 319 Views
Data Structures. MSc.It :- Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer. ADD. Remove. Queue. Contains elements that are inserted and removed according to the first-in-first-out (FIFO) principle . . Rear. FRONT. Queues Operations.
E N D
Data Structures MSc.It :- Ali Abdul KaremHabib Kufa University / mathematics & Science Of Computer
ADD Remove Queue • Contains elements that are inserted and removed according to the first-in-first-out (FIFO) principle . Rear FRONT
Queues Operations • It supports two fundamental methods: • INSERT: insert an element at the end of the queue • DELETE: remove and return from the queue the element at the front • Checking Conditions: • Queue Overflow :If rear=maxsize • Queue Empty: If front=-1
Operations on Queues … Cont • The linear array queue has two pointer variables: FRONT , containing location of front element ; and REAR containing location of rear element. • The new element will add itself from the 'rear' end , then Queue's 'rear' value increments by one . • The element leave the queue from the 'front' end, so queue the Queue's 'front' value increments by one.
Insertion Algorithm • This algorithm for Add new element in queue Step1 : Start Step2 : let queue[Size] Step3 : let front=-1 , Rear=-1 // Initialization Queue Step4 : if (rear = queue . Size ) Then Step5 : print ( Queue Is Full“Data Is Data Over flow “) Step6 : Else rear=rear+1 Step7 : queue[rear]=item . Step8 : if front = -1 Then Step 9 : Front= 0 Step10: End
Delete Algorithm • This algorithm to remove item from queue . Step 1: Start Step 2: if ( front = -1) then Step 3: Print ( “ Queue Is Empty “ , “ Data Underflow “) Step 4: else print ( queue[front]) Step 5: if (front=rear ) then Step 6: front = -1 , rear=-1 Step 7: Else front =front+1 Step 8: End
Applications of Queue • It is used in scheduling the jobs to be processed by the processor. • A queue schedules the order of the print files to be printed. • A server maintains a queue of the client requests to be processed
Types Of queue • Linear Queue • Circular Queue • Double ended Queue • Priority Queue
Circular Queue • A circular queue is a Queue but a particular implementation of a queue. • They have a circular structure . • There is no space lost . • Properties of this type of queue is :- • Front pointing to the first item • Rear pointing to the last item . • When the rear arrived to end of queue make it wrap to first of queue (rear =0) , also this with front .
A 0 0 0 1 1 B 1 2 C 2 C C 2 C D 3 3 D 3 D D 4 E 4 E 4 F 5 5 F 5 6 G 6 G 6 7 7 7 H H I Queue Front = 0 Rear= 3 Front = 2 Rear= 3 Rear= 7 Front = 2 Rear= 0 Front = 2
Algorithm Insertion for CQ Step 1: Start Step 2: front = -1 , Rear =-1 Step 3: rear=rear+1 Step 4: if ( rear=CQ.Size ) then Step 5: Let rear=0 Step 6: if ( rear = front andCQ[rear] Not null ) then Step7: Print (“ Data CQ Is Overflow “ ) Step 8: else CQ[Rear]=item Step 9: End
Algorithm Delete for CQ Step 1: Start Step 2: If (front= -1 ) then Step 3: Print( CQ Is Empty ) Step 4: else Print (CQ[front]) Step 5: if ( front = CQ.Size) then Step 6: front =0 Step 7: else front =front+1 Step 8: end
Double End queue • It is a linear list in which elements are added or removed at either end but not in middle. • a double-ended queue is a data structure it supports the following operations: enq_front, enq_back, deq_front, deq_back. • DeQueue can be represented in two ways they are • 1) Input restricted De-Queue :- allows insertions at only one end but allows deletions on both ends of the list . • 2) Output-restricted de-queue:- allows deletions at only one end but allows insertions at both ends of the list.
Example • - We can delete X by F1 and add new element after T by R1 • - We can delete T by F2 and add new element after X by R2
Priority queue • A priority queue is a collection of elements such that each element has been assigned a priority and the order in which elements are deleted and processed comes from the following rules: • (1) An element of higher priority is processed before any element of lower priority. • (2) Two elements with the same priority are processed according to the order in which they were added to the queue.
Assignment 5 • Write Algorithm to insertion element into queue . • Write algorithm to deletion element from queue . • Write algorithm to insert element in to CQ . • Write algorithm to delete element from CQ . • Check the any number is palindrome Or not Using DE-Q