queue

Upload: michael-medina

Post on 10-Oct-2015

19 views

Category:

Documents


0 download

DESCRIPTION

data structure queue

TRANSCRIPT

  • 5/20/2018 Queue

    1/22

    1

    Queue

    Subtopics:

    Simple Queue

    Circular Queue

    Project Made by:

    Mehul BhanderiKishan Jasani

    Vinesh KumbhaniNilay Bharti

  • 5/20/2018 Queue

    2/22

    2

    1. Introduction ____________________________________________________3

    2. Operations _____________________________________________________6

    3.Illustration of Simple Queue _______________________________________7

    4.Algorithm for inserting data in Simple Queue __________________________8

    5.Algorithm for deleting data from Simple Queue ________________________96.Drawback of Simple Queue________________________________________10

    7.Circular Queue__________________________________________________12

    8. Illustration of Circular Queue______________________________________13

    9.Algorithm for inserting data in Circular Queue_________________________19

    10. Algorithm for deleting data from Cicular Queue ______________________20

    11. Summary and Conclusion________________________________________21

    12. Sources______________________________________________________22

    INDEX

  • 5/20/2018 Queue

    3/22

    3

    1. Introduction

    QueueA queue is a particular kind of collection in which the entitiesin the collection are kept in order and the principal (or only)operations on the collection are the addition of entities tothe rear terminal position and removal of entities from the

    front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure,the first element added to the queue will be the first one tobe removed. This is equivalent to the requirement thatonce an element is added, all elements that were added

    before have to be removed before the new element can beinvoked. A queue is an example of a linear data structure.

  • 5/20/2018 Queue

    4/22

    4

    Queues provide services in computer science,transport, and operations research where variousentities such as data, objects, persons, or events arestored and held to be processed later. In thesecontexts, the queue performs the function of a buffer.

    Queues are common in computer programs, wherethey are implemented as data structures coupledwith access routines, as an abstract data structure orin object-oriented languages as classes. Common

    implementations are circular buffers and linked lists.

  • 5/20/2018 Queue

    5/22

    5

    The defining attribute of a queue data structure is

    the fact that allows access to only the front andback of the structure. Furthermore, elements canonly be removed from the front and can only beadded to the back. In this way, an appropriatemetaphor often used to represent queues is the

    idea of a checkout line. Other examples of queuesare people traveling up an escalator, machineparts on an assembly line, or cars in line at apetrol station. The recurring theme is clear:

    queues are essentially the same as a queue youwould get in a shop waiting to pay.

  • 5/20/2018 Queue

    6/22

    6

    2. Operations

    In queue, data elements are added at one end, calledthe rear and removed from another end, called the fron tof the list.

    Two basic operations are associated with queue:

    1. Insert operation is used to insert an element into a

    queue.

    2. Delete operation is used to delete an element from a

    queue.

  • 5/20/2018 Queue

    7/22

    7

    3. Consider the following queue (simple queue).

    Rear = 4 and Front = 1 and N = 7

    10 50 30 40

    (1) Insert 20. Now Rear = 5 and Front = 1

    1 2 3 4 5 6 7

    10 50 30 40 201 2 3 4 5 6 7

    (2) Delete Front Element. Now Rear = 5 and Front = 2

    50 30 40 201 2 3 4 5 6 7

    (3) Delete Front Element. Now Rear = 5 and Front = 3

    30 40 201 2 3 4 5 6 7

    (4) Insert 60. Now Rear = 6 and Front = 3

    30 40 20 601 2 3 4 5 6 7

  • 5/20/2018 Queue

    8/22

    8

    4. Algorithm for inserting data inSimple Queue:Insert ( ):Here QUEUE is an array with N locations. FRONT andREAR points to the front and rear ofthe QUEUE. ITEM is the value to be inserted.1. If (REAR == N) Then [Check for overflow]2. Print: Overflow

    3. Else4. If (FRONT and REAR == 0) Then [Check if QUEUE isempty](a) Set FRONT = 1(b) Set REAR = 15. Else

    6. Set REAR = REAR + 1 [Increment REAR by 1][End of Step 4 If]7. QUEUE[REAR] = ITEM8. Print: ITEM inserted[End of Step 1 If]9. Exit

  • 5/20/2018 Queue

    9/22

    9

    5. Algorithm for deleting data:Delete ( ):

    Here QUEUE is an array with N locations. FRONT and REARpoints to the front and rear ofthe QUEUE.1. If (FRONT == 0) Then [Check for underflow]2. Print: Underflow3. Else

    4. ITEM = QUEUE[FRONT]5. If (FRONT == REAR) Then [Check if only one element is left](a) Set FRONT = 0(b) Set REAR = 06. Else7. Set FRONT = FRONT + 1 [Increment FRONT by 1][End of Step 5 If]8. Print: ITEM deleted[End of Step 1 If]9. Exit

  • 5/20/2018 Queue

    10/22

    10

    6. Drawback of Simple Queue

    Problem: RightwardDrifting:

    After a sequence of additions and removals, items will

    drift towards the end of the array

    Even though, there are empty spaces in front of the

    queue array, insert operation cannot be performed

    on the queue, since back= size - 1.

  • 5/20/2018 Queue

    11/22

    11

    Rightward Drifting Solutions

    To optimize space and to solve rightward drifting:

    Shift array elements after each deletion.

    However, shifting is not effective and dominates

    the cost of the implementation.

  • 5/20/2018 Queue

    12/22

    12

    7. Circular Queue:Using a circular array makes it easier: When front

    or back reach theend of the array, wrap them around to the

    beginning of the array.

    Let's see inserting and

    Deleting of data.

  • 5/20/2018 Queue

    13/22

    13

    8. Example: Consider the following circular queuewith N=5

    .

    Initially, Rear = 0, Front =

    0.

    2. Insert 10, Rear = 1, Front = 1.Rear

    Front

  • 5/20/2018 Queue

    14/22

    14

    3. Insert 50, Rear = 2, Front = 1.

    RearFront

    4. Insert 20, Rear = 3, Front = 0.

    Rear

    Front

  • 5/20/2018 Queue

    15/22

    15

    5. Insert 70, Rear = 4, Front = 1.

    6. Delete front, Rear = 4, Front = 2.

    Rear

    Rear

    Front

    Front

  • 5/20/2018 Queue

    16/22

    16

    7. Insert 100, Rear = 5, Front = 2.

    8. Insert 40, Rear = 1, Front = 2.

    Front

    Rear

    Front

    Rear

  • 5/20/2018 Queue

    17/22

    17

    9. Insert 140, Rear = 1, Front = 2. As Front = Rear + 1, so Queue overflow.

    Rear Front

    10. Delete front, Rear = 1, Front = 3.

    Rear

    Front

  • 5/20/2018 Queue

    18/22

    18

    11. Delete front, Rear = 1, Front = 4.

    12. Delete front, Rear = 1, Front = 5.

    Rear

    Rear

    Front

    Front

  • 5/20/2018 Queue

    19/22

    19

    9. Algorithm to insert data in Circular Queue:Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear

    elements of the QUEUE. ITEM is the value to be inserted.1. If (FRONT == 1 and REAR == N) or (FRONT == REAR + 1) Then

    2. Print: Overflow

    3. Else

    4. If (REAR == 0) Then [Check if QUEUE is empty]

    (a) Set FRONT = 1

    (b) Set REAR = 1

    5. Else If (REAR == N) Then [If REAR reaches end if QUEUE]

    6. Set REAR = 1

    7. Else

    8. Set REAR = REAR + 1 [Increment REAR by 1]

    [End of Step 4 If]9. Set QUEUE[REAR] = ITEM

    10. Print: ITEM inserted

    [End of Step 1 If]

    11. Exit

  • 5/20/2018 Queue

    20/22

    20

    10. Algorithm to delete data from CircularQueue:Here QUEUE is an array with N locations. FRONT and

    REAR points to the front and rearelements of the QUEUE.1. If (FRONT == 0) Then [Check for Underflow]2. Print: Underflow3. Else4. ITEM = QUEUE[FRONT]

    5. If (FRONT == REAR) Then [If only element is left](a) Set FRONT = 0(b) Set REAR = 06. Else If (FRONT == N) Then [If FRONT reaches end ifQUEUE]7. Set FRONT = 1

    8. Else9. Set FRONT = FRONT + 1 [Increment FRONT by 1][End of Step 5 If]10. Print: ITEM deleted[End of Step 1 If]11. Exit

  • 5/20/2018 Queue

    21/22

    21

    11. Summary and Conclusion Queue can be implemented using linear array and

    circulararray. Structure of queue is linear, items that manage thearray are front and back. Insertion happens at back, while deletion happens at

    front. Drawback of simple queue is that it will lead torightward drifting problem after sequence of deletionand insertion is performed on the queue. Circular Queue can be used in order to solve the

    problem, whereby after front or back reach the end ofthe array, it will wrap around to the beginning of thearray.

  • 5/20/2018 Queue

    22/22

    22

    12 . Sources:

    http://www.wiziq.com/tutorial/191784-Linear-Queue-and-Circular-Queue

    https://www.princeton.edu/~achaney/tmve/wiki100k/docs/

    Queue_(data_structure).html

    http://www.w3professors.com/Pages/Courses/Data-Structure/Algorithms/Data-Structure-Algorithm.html

    http://ocw.utm.my/file.php/31/Module/ocwQueueDec2011.pdf

    http://mithun039.weebly.com/uploads/3/5/3/6/3536408/queue.ppt

    http://www.wiziq.com/tutorial/191784-Linear-Queue-andhttps://www.princeton.edu/~achaney/tmve/wiki100k/docs/http://www.w3professors.com/Pages/Courses/Data-Structurehttp://www.w3professors.com/Pages/Courses/Data-Structurehttp://www.w3professors.com/Pages/Courses/Data-Structurehttp://www.w3professors.com/Pages/Courses/Data-Structurehttp://www.w3professors.com/Pages/Courses/Data-Structurehttps://www.princeton.edu/~achaney/tmve/wiki100k/docs/https://www.princeton.edu/~achaney/tmve/wiki100k/docs/http://www.wiziq.com/tutorial/191784-Linear-Queue-andhttp://www.wiziq.com/tutorial/191784-Linear-Queue-andhttp://www.wiziq.com/tutorial/191784-Linear-Queue-andhttp://www.wiziq.com/tutorial/191784-Linear-Queue-andhttp://www.wiziq.com/tutorial/191784-Linear-Queue-andhttp://www.wiziq.com/tutorial/191784-Linear-Queue-andhttp://www.wiziq.com/tutorial/191784-Linear-Queue-andhttp://www.wiziq.com/tutorial/191784-Linear-Queue-and