queues son

Upload: salman-khan

Post on 06-Apr-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Queues Son

    1/61

    Queues

    EENG212

    Algorithm

    And

    Data Structures

  • 8/3/2019 Queues Son

    2/61

    DEFINITION OF QUEUE

    A Queue is an ordered collection of items from which items maybe deleted at one end (called the frontof the queue) and intowhich items may be inserted at the other end (the rearof thequeue).

    The first element inserted into the queue is the first element to beremoved. For this reason a queue is sometimes called a fifo(first-in first-out) list as opposed to the stack, which is a lifo(last-in first-out).

  • 8/3/2019 Queues Son

    3/61

    Queue

    items[MAXQUEUE-1]

    . .

    . .

    . .

    items[2] Citems[1] B

    items[0] A Front=0

    Rear=2

  • 8/3/2019 Queues Son

    4/61

    Declaration of a Queue

    # define MAXQUEUE 50 /* size of the queueitems*/

    typedef struct {int front;

    int rear;

    int items[MAXQUEUE];}QUEUE;

  • 8/3/2019 Queues Son

    5/61

    QUEUE OPERATIONS

    Initializethe queue

    Insertto the rear of the queue

    Remove(Delete) from the front of thequeue

    Is the Queue Empty

    Is the Queue Full What is the size of the Queue

  • 8/3/2019 Queues Son

    6/61

    INITIALIZE THE QUEUE

    items[MAXQUEUE-1]

    . .

    . .

    .items[1]

    items[0] front=0

    rear=-1

    The queue is initialized by having the rearset to -1, and frontset to 0.Let us assume that maximum number of the element we have in a queueis MAXQUEUE elements as shown below.

  • 8/3/2019 Queues Son

    7/61

    insert(&Queue, A) an item (A) is insertedat the Rearof the queue

    items[MAXQUEUE

    -1]

    . .

    . .

    items[3]

    items[2]

    items[1]

    items[0] A Front=0,

    Rear=0

  • 8/3/2019 Queues Son

    8/61

    insert(&Queue, B) A new item (B) is insertedat the Rearof the

    queue

    items[MAXQUEUE

    -1]

    . .

    . .

    items[3]

    items[2]

    items[1] B Rear=1

    items[0] A Front=0

  • 8/3/2019 Queues Son

    9/61

    insert(&Queue, C) A new item (C) is insertedat the Rearof the

    queue

    items[MAXQUEUE

    -1]

    . .

    . .

    items[3]

    items[2] C Rear=2

    items[1] B

    items[0] A Front=0

  • 8/3/2019 Queues Son

    10/61

    insert(&Queue, D) A new item (D) is insertedat the Rearof the

    queue

    items[MAXQUEUE-1]

    . .

    . .

    items[3] D Rear=3

    items[2] C

    items[1] B

    items[0] A Front=0

  • 8/3/2019 Queues Son

    11/61

    InsertOperation

    void insert(QUEUE *qptr, char x)

    {

    if(qptr->rear == MAXQUEUE-1)

    {

    printf("Queue is full!");exit(1);

    }

    else

    {

    qptr->rear++;

    qptr->items[qptr->rear]=x;

    }

    }

  • 8/3/2019 Queues Son

    12/61

    char remove(&Queue) an item (A) is removed (deleted) from the

    Frontof the queue

    items[MAXQUEUE-1]

    . .

    . .

    items[3] D Rear=3

    items[2] C

    items[1] B Front=1

    items[0] A

  • 8/3/2019 Queues Son

    13/61

    char remove(&Queue)

    Removetwo items from the front of the queue.

    items[MAXQUEUE-1]

    . .

    . .

    items[3] D Rear=3

    items[2] C Front=2

    items[1] B

    items[0] A

  • 8/3/2019 Queues Son

    14/61

    char remove(&Queue)

    Removetwo items from the front of the queue.

    items[MAXQUEUE-1]

    . .

    . .

    items[3] D Front=Rear=3

    items[2] C

    items[1] B

    items[0] A

  • 8/3/2019 Queues Son

    15/61

    char remove(&Queue)

    Removeone more item from the front of thequeue.

    items[MAXQUEUE-1]

    . .

    items[4] Front=4

    items[3] D Rear=3

    items[2] C

    items[1] B

    items[0] A

  • 8/3/2019 Queues Son

    16/61

    RemoveOperationchar remove(struct queue *qptr){char p;if(qptr->front > qptr->rear){printf("Queue is empty");

    exit(1);}else{p=qptr->items[qptr->front];qptr->front++;return p;}

    }

  • 8/3/2019 Queues Son

    17/61

    INSERT / REMOVE ITEMS Assume thatthe rear= MAXQUEUE-1

    What happens if we want to insert a newitem into the queue?

    items[MAXQUEUE-1] X rear=MAXQUEUE-1

    . .

    . .

    items[3] D front=3

    items[2] C

    items[1] B

    items[0] A

  • 8/3/2019 Queues Son

    18/61

    INSERT / REMOVE ITEMS

    What happens if we want to insert a newitem F into the queue?

    Although there is some empty space, thequeue is full.

    One of the methods to overcome thisproblem is to shift all the items to occupy

    the location of deleted item.

  • 8/3/2019 Queues Son

    19/61

    REMOVE ITEM

    items[MAXQUEUE-1]

    . .

    . .

    items[3] D Rear=3

    items[2] C

    items[1] B Front=1

    items[0] A

  • 8/3/2019 Queues Son

    20/61

    REMOVE ITEM

    items[MAXQUEUE-1]

    . .

    . .

    items[3] D Rear=3

    items[2] C

    items[1] B Front=1

    items[0] B

  • 8/3/2019 Queues Son

    21/61

    REMOVE ITEM

    items[MAXQUEUE-1]

    . .

    . .

    items[3] D Rear=3

    items[2] C

    items[1] C

    items[0] B

  • 8/3/2019 Queues Son

    22/61

    REMOVE ITEM

    items[MAXQUEUE-1]

    . .

    . .

    items[3] D Rear=3

    items[2] D

    items[1] C

    items[0] B

  • 8/3/2019 Queues Son

    23/61

    REMOVE ITEM

    items[MAXQUEUE-1]

    . .

    . .

    items[3] D

    items[2] D Rear=2

    items[1] C

    items[0] B

  • 8/3/2019 Queues Son

    24/61

    Modified RemoveOperation

    char remove(struct queue *qptr)

    {char p;int i;if(qptr->front > qptr->rear){

    printf("Queue is empty");exit(1);}else{p=qptr->items[qptr->front];

    for(i=1;irear;i++)qptr->items[i-1]=qptr->items[i];qptr->rear--return p;}

    }

  • 8/3/2019 Queues Son

    25/61

    INSERT / REMOVE ITEMS

    Since all the items in the queue are requiredto shift when an item is deleted, this methodis not preferred.

    The other method is circular queue.

    When rear = MAXQUEUE-1, the nextelement is entered at items[0] in case that

    spot is free.

  • 8/3/2019 Queues Son

    26/61

    Initializethe queue.

    items[6] front=rear=6

    items[5]

    items[4]

    items[3]

    items[2]

    items[1]

    items[0]

  • 8/3/2019 Queues Son

    27/61

    Insert items into circularqueue

    items[6] front=6

    items[5]

    items[4]

    items[3]

    items[2]

    items[1]

    items[0] A rear=0

    InsertA,B,C to the rear of the queue.

  • 8/3/2019 Queues Son

    28/61

    Insert items into circularqueue

    items[6] front=6

    items[5]

    items[4]

    items[3]

    items[2]

    items[1] B rear=1

    items[0] A

    InsertA,B,C to the rear of the queue.

  • 8/3/2019 Queues Son

    29/61

    Insert items into circularqueue

    InsertA,B,C to the rear of the queue.

    items[6] front=6

    items[5]

    items[4]

    items[3]

    items[2] C rear=2

    items[1] B

    items[0] A

  • 8/3/2019 Queues Son

    30/61

    Removeitems from circular queue

    Removetwo items from the queue.

    items[6]

    items[5]

    items[4]

    items[3]

    items[2] C rear=2

    items[1] B

    items[0] A front=0

  • 8/3/2019 Queues Son

    31/61

    Removeitems from circular queue

    Removetwo items from the queue.

    items[6]

    items[5]

    items[4]

    items[3]

    items[2] C rear=2

    items[1] B front=1

    items[0] A

  • 8/3/2019 Queues Son

    32/61

    Removeitems from circular queue

    Removeone more item from the queue.

    items[6]

    items[5]

    items[4]

    items[3]

    items[2] C rear=front=2

    items[1] B

    items[0] A

  • 8/3/2019 Queues Son

    33/61

    InsertD,E,F,G to the queue.

    items[6] G rear=6

    items[5] F

    items[4] E

    items[3] D

    items[2] C front=2

    items[1] B

    items[0] A

  • 8/3/2019 Queues Son

    34/61

    InsertH and I to the queue.

    items[6] G

    items[5] F

    items[4] E

    items[3] D

    items[2] C front=2

    items[1] B

    items[0] H rear=0

  • 8/3/2019 Queues Son

    35/61

    InsertH and I to the queue.

    items[6] G

    items[5] F

    items[4] E

    items[3] D

    items[2] C front=2

    items[1] I

    items[0] H rear=0

  • 8/3/2019 Queues Son

    36/61

    InsertJ to the queue.

    items[6] G

    items[5] Fitems[4] E

    items[3] D

    items[2] ?? front=rear=2items[1] I

    items[0] H

  • 8/3/2019 Queues Son

    37/61

    Declarationand Initializationof a Circular Queue.

    #define MAXQUEUE 10 /* size of the queue items*/

    typedef struct {

    int front;

    int rear;int items[MAXQUEUE];

    }QUEUE;

    QUEUE q;

    q.front = MAXQUEUE-1;

    q.rear= MAXQUEUE-1;

  • 8/3/2019 Queues Son

    38/61

    InsertOperationfor circular Queue

    void insert(QUEUE *qptr, char x){if(qptr->rear == MAXQUEUE-1)

    qptr->rear=0;

    elseqptr->rear++;

    /* or qptr->rear=(qptr->rear+1)%MAXQUEUE) */if(qptr->rear == qptr->front){

    printf("Queue overflow");

    exit(1);}qptr->items[qptr->rear]=x;}

  • 8/3/2019 Queues Son

    39/61

    RemoveOperationfor circular queue

    char remove(struct queue *qptr){if(qptr->front == qptr->rear){

    printf("Queue underflow");

    exit(1);}if(qptr->front == MAXQUEUE-1)

    qptr->front=0;else

    qptr->front++;return qptr->items[qptr->front];}

  • 8/3/2019 Queues Son

    40/61

    Example

    Following program is an example of circularqueueinsertion and deletion operation.

  • 8/3/2019 Queues Son

    41/61

    #include #include

    #define MAXELEMENTS 50#define TRUE 1#define FALSE 0typedef struct

    {int items[MAXELEMENTS];int front , rear ;

    } QUEUE;

    void qinsert( QUEUE * , int);int qdelete(QUEUE *);int empty(QUEUE *);

    int main(){

  • 8/3/2019 Queues Son

    42/61

    {char operation;int x;QUEUE q;q.front = q.rear = MAXELEMENTS - 1;do{printf("%s\n",Queue Operation type I(nsert) D(elete) or E(xit) ");scanf("\n%c",&operation);switch (operation) {case 'I':case 'i':printf("%s\n","Insert an element");

    scanf("\n%d",&x);qinsert(&q , x);break;

    case 'D' :case 'd':x=qdelete(&q);printf("\n %d is deleted \n",x);

    break;default: printf(Incorrect Operation type\n);break;

    }}while (operation != 'E'&&operation!='e');

    return 0;}

    int empty(QUEUE *qptr){

  • 8/3/2019 Queues Son

    43/61

    {return((qptr->front == qptr->rear) ? TRUE : FALSE);

    }

    int qdelete(QUEUE *qptr){

    if (empty(qptr)) {printf("Queue underflow ");exit(1);}

    qptr->front=(qptr->front+1)%(MAXELEMENTS)return(qptr->items[qptr->front]);

    }void qinsert(QUEUE *qptr , int x){

    /* make room for new element */printf("\n %d is inserted \n",x);

    qptr->rear=(qptr->rear+1)%(MAXELEMENTS)

    if (qptr->rear == qptr->front) {printf("Queue overflow");exit(1);}

    qptr->items[qptr->rear] = x;

    return;}

  • 8/3/2019 Queues Son

    44/61

    Example

    Write a program to simulate a queuestructure with following specifications:

    User will be able to insert an item, remove anitem, display all of the queue items and clearthe queue.

    The items will be floating point numbers.

  • 8/3/2019 Queues Son

    45/61

    #include#include#define MAXQUEUE 5 /* size of the queueitems*/typedef struct{int front;

    int rear;float items[MAXQUEUE];}queue;void insert(queue *, float);

    float remove(queue *);void display( queue *);void clear( queue *);void menu();

    int main()

  • 8/3/2019 Queues Son

    46/61

    int main(){int choice;float x;queue q;q.front = MAXQUEUE-1;q.rear= MAXQUEUE-1;do{menu();scanf("%d",&choice);switch(choice){case 1: printf("Enter the item to be inserted:");

    scanf("%f",&x);insert(&q,x);break;case 2: x=remove(&q); printf("\nRemoved item:%f",x);break;case 3: display(&q);break;

    case 4: clear(&q);break;default: printf("\nWrong entry, Try again!!");break;}}while(choice != 5);

    return 0;}

  • 8/3/2019 Queues Son

    47/61

    void menu(){printf("Press 1 to insert \n");printf("Press 2 to remove \n");

    printf("Press 3 to display \n");printf("Press 4 to clear \n");printf("Press 5 to quit\n");printf("\n\nEnter your choice:\n");}

    void insert(queue *qptr, float x){if(qptr->rear == MAXQUEUE-1)qptr->rear=0;elseqptr->rear++;

    if(qptr->rear == qptr->front){printf("Queue overflow");exit(1);}qptr->items[qptr->rear]=x;}

    float remove(queue *qptr)

  • 8/3/2019 Queues Son

    48/61

    float remove(queue *qptr)

    {if(qptr->front == qptr->rear){printf("Queue underflow");

    exit(1);}if(qptr->front == MAXQUEUE-1)qptr->front=0;else

    qptr->front++;return qptr->items[qptr->front];}

    void clear(queue *qptr)

    {qptr->front=MAXQUEUE-1;qptr->rear =MAXQUEUE-1;printf("Now the Queue is Empty\n");}

  • 8/3/2019 Queues Son

    49/61

    void display(queue *qptr){int f,r;

    f=qptr->front;r=qptr->rear;

    while(qptr->front !=qptr->rear){if(qptr->front == MAXQUEUE-1)qptr->front =0;elseqptr->front++;printf("%5.2f",qptr->items[qptr->front]);}printf("\n");qptr->front=f;

    qptr->rear=r;}

  • 8/3/2019 Queues Son

    50/61

    EXAMPLE

    Assume that we have a queue of integernumbers. Write a function, QueueSearchtosearch a given key element in the queue until

    the search key is found. Once the search keyis found, the function returns its position inthe queue, otherwise returns -1 to indicate

    that the searched key is not in the queue.

  • 8/3/2019 Queues Son

    51/61

    typedef struct{int front;int rear;int items[MAXQUEUE]; /* Assume thatMAXQUEUE is defined*/}QUEUE;

    Assume that the Queue contains integer elements andhas the following structure:

  • 8/3/2019 Queues Son

    52/61

    int QueueSearch(QUEUE *qptr, int searchkey){int pos = -1,f;f=qptr->front;while(qptr->front !=qptr->rear){if(qptr->front == MAXQUEUE-1)qptr->front =0;elseqptr->front++;if(qptr->items[qptr->front] == searchkey){pos = qptr->front;qptr->front = f;return pos;}}qptr->front=f;return pos;

    }

    Then the following function can be written:

  • 8/3/2019 Queues Son

    53/61

    Example

    Write a function, QueueCopyReverse, tocopy the integer elements of Queue 1 toQueue 2 in reverse order.

    Assume that there is enough space in Queue2 for copying and the size of both of theQueues is MAXQUEUE.

    void QueueCopyReverse(QUEUE *qptr1, QUEUE *qptr2){

  • 8/3/2019 Queues Son

    54/61

    {int r,x;r=qptr1->rear;do{x = qptr1->items[qptr1->rear];

    insert(qptr2,x);if(qptr1->rear ==0)qptr1->rear = MAXQUEUE-1;elseqptr1->rear --;}while(qptr1->rear != qptr1->front);

    qptr1->rear = r;}void insert(QUEUE *qptr, int x){if(qptr->rear == MAXQUEUE-1)qptr->rear=0;

    elseqptr->rear++;if(qptr->rear == qptr->front){printf("Queue overflow");exit(1);}

    qptr->items[qptr->rear]=x;}

  • 8/3/2019 Queues Son

    55/61

    PRIORITY QUEUES

    The priority queue is a data structure in whichintrinsic ordering of the elements determinesthe results of its basic operations.

    An ascending priority queueis a collectionof items into which items can be insertedarbitrarily and from which only the smallest

    item can be removed. On the other hand adescending priority queueallows only thelargest item to be removed.

  • 8/3/2019 Queues Son

    56/61

    Priority QUEUE Operations

    Insertion The insertion in Priority queues is the same as in non-

    priority queues.

    Deletion

    Deletion requires a search for the element of highestpriority and deletes the element with highest priority. Thefollowing methods can be used for deletion/removal from agiven Priority Queue: An empty indicator replaces deleted elements.

    After each deletion elements can be moved up in the

    array decrementing the rear. The array in the queue can be maintained as an ordered

    circular array

  • 8/3/2019 Queues Son

    57/61

    Priority Queue Declaration

    Queue data type of Priority Queue is thesame as the Non-priority Queue.

    #define MAXQUEUE 10 /* size of the queue

    items*/

    typedef struct {

    int front, rear;

    int items[MAXQUEUE];

    }QUEUE;

  • 8/3/2019 Queues Son

    58/61

    REMOVE OPERATION

    PriQremoveOperation using removing theelement with highest priority and shiftingthe elements up in the array and

    decrementing rear. Consider AscendingPriority Queue.

  • 8/3/2019 Queues Son

    59/61

    int PRiQremove(QUEUE *qptr){int smallest, loc, f, i;f=qptr->front;if(qptr->front == qptr->rear){printf("Queue underflow");exit(1);}

    smallest = qptr->items[(qptr->front+1)%MAXQUEUE];loc = (qptr->front+1)%MAXQUEUE;(qptr->front++)%MAXQUEUE; /* Circular increment*/while(qptr->front != qptr->rear){if(qptr->items[(qptr->front+1)%MAXQUEUE] items[(qptr->front+1)%MAXQUEUE];loc = (qptr->front+1)%MAXQUEUE;}qptr->front =(qptr->front+1)%MAXQUEUE; /* Circular inc.*/}

  • 8/3/2019 Queues Son

    60/61

    while(loc != qptr->rear){qptr->items[loc] = qptr->items[(loc+1)%MAXQUEUE];(loc++)%MAXQUEUE;}qptr->front=f;

    if(qptr->rear == 0) /*Decrement rear after removing oneitem*/qptr->rear = MAXQUEUE -1;elseqptr->rear--;

    return smallest;}

  • 8/3/2019 Queues Son

    61/61

    InsertOperation of Priority Queue is the same as theinsert of the non-priority queues.

    void insert(struct queue *qptr, int x){qptr->rear = (qptr->rear++)%MAXQUEUE;

    /*Circular increment*/if(qptr->rear == qptr->front){printf("Queue overflow");exit(1);

    }qptr->items[qptr->rear]=x;}