SlideShare uma empresa Scribd logo
1 de 73
QUEUE
Course Objectives


At the end of the lesson students are expected to be able
  to:

• Understand queue concepts and applications.

• Understand queue structure and operations that can be
  done on queue.

• Understand and know how to implement queue using
  array and linked list : linear array, circular array, linear
  link list and circular list.
1.0 Introduction to Queue
Introduction to Queue


• New items enter at the back, or rear, of the

  queue

• Items leave from the front of the queue

• First-in, first-out (FIFO) property

   – The first item inserted into a queue is the

     first item to leave

   – Middle elements are logically inaccessible
Introduction to Queue


• Important in simulation & analyzing the

  behavior of complex systems
Queue Applications


• Real-World Applications
  – Cashier lines in any store

  – Check out at a bookstore

  – Bank / ATM

  – Call an airline
Queue Applications


• Computer Science Applications
  – Print lines of a document

  – Printer sharing between computers

  – Recognizing palindromes

  – Shared resource usage (CPU, memory
    access, …)
Queue Applications


•    Simulation
    – A study to see how to reduce the wait
      involved in an application
Queue implementation


Remove/                                    Add/
                  A       B     C          Enqueue
Dequeue

             Front/Head       Back/Rear


  Basic Structure of a Queue:
     •Data structure that hold the queue
     •head
     •rear
Queue implementation


                                                        Add/
                   A          B       C          D      Enqueue


                 Head                            Rear


  Insert D into Queue (enQueue) : D is inserted at rear
Remove/
Dequeue   A             B         C       D


                       Head               Rear

      Delete from Queue (deQueue) : A is removed
Queue operations
•   Queue operations
    – Create an empty queue

    – Destroy a queue

    – Determine whether a queue is full

    – Add a new item to the queue (enQueue)

    – Determine whether a queue is empty

    – Remove the item that was added earliest(deQueue)

    – Retrieve at Front(getFront)

    – Retrieve at Back the item that was added
      earliest(getRear)
Queue Implementation


Implementation:
 – Array-based (Linear or Circular)
 – Pointer-based : Link list (Linear or
   Circular)
2.0 Queue Implementation Using
        Array(Linear)
Queue Implementation Using
Array(Linear)
• Number of elements in Queue are fixed
  during declaration.
• Need isFull() operation to determine
  whether a queue is full or not.
Queue Implementation Using
  Array(Linear)

• Queue structure need at least 3 elements:

1) Element to store items in Queue

2) Element to store index at head

3) Element to store index at rear
Create Queue Operation

     • Declare

        – front & back are indexes in the array

        – Initial condition: front =0 & back = -1

        – Size of an array in queue
                          Queue
 0         0       1        2       3    Max size    -1
front                                               back
Create Queue operation
Example Code 1


#include <iostream>
using namespace std;
#define max 5


int front = 0, back = -1;
                                Create Queue
char item[max], newitem;

                                            item

                  0              0      1          2   3   4    -1
                front                                          back


             Front refer to index 0


                                                                 Continue…
enQueue operation
void enQueue(){
         cout<<"nt#################n";
         cout<<"nt1. enQueuen";
         //check queue is full
         if(back == max - 1){
                   cout<<"ntQueue Is Full, Cannot Add Item In Queuen";
         }else{
                   cout<<"nttEnter Item:";
                   cin>>newitem;
                   back++;
                   item[back]=newitem;
                   cout<<endl;                enQueue
         }
}                                                item                     back++
                     0              0           1        2     3   4        0
                   front            A                                     back
                                                                       back = -1+1
                                                                       back = 0
           Front refer to index 0       From back/rear
                                        item[back] = newitem

                                                                           Continue…
enQueue operation
                                          item                 back++

     0              0      1          2          3       4        1
   front            A      B                                    back

                                                               back = 0 +1
                                                               back = 1
Front refer to index 0         From back/rear
                               item[back] = newitem

                                          item                   back++

    0               0      1          2          3       4         2

  front             A      B          C                          back

                                                                back = 1 +1
Front refer to index 0                                          back = 2
                                        From back/rear
                                        item[back] = newitem




                                                                             Continue…
enQueue operation

                                      item                          back++
     0              0      1          2      3          4                   3
   front            A      B      C          D                      back

                                                                   back = 2 +1
                                                                   back = 3
Front refer to index 0
                                                 From back/rear
                                                 item[back] = newitem


                               item                               back++
     0              0      1          2      3          4               4
   front            A      B      C          D         E           back

                                                                back = 3 +1
                                                                back = 4
Front refer to index 0
                                                     From back/rear
                                                     item[back] = newitem



                                                                                 Continue…
deQueue operation
     void deQueue(){
              cout<<"nt#################n";
              cout<<"nt2.deQueuen";
              if(back < front){
                         cout<<"ntThere is no data to remove from queuen";
              }else{
                         char itemdeleted;
                         itemdeleted=item[front];     deQueue
                         item[front] = NULL;
                         cout<<"ntItem Remove From Queue:"<<itemdeleted<<endl;
                         front++;

                 }
                 cout<<endl;                                 item
     }                       0              0            1    2     3   4          4
                            front           A            B    C     D   E     back

                                                                             back = 3 + 1
itemdeleted = item[front]    Front refer to index 0                          back = 4
front = 0

                                    From front/head
                                    item[front] = NULL                       Continue…
deQueue operation
             front++                               item
              1               0           1          2         3       4          4
             front          NULL          B          C         D       E        back
        front = 0 +                                                            back = 3 + 1
        1                                                                      back = 4
        front = 1
               Front refer to index 1
                                                    item
             1                 0           1          2        3       4           4
            front           NULL           B         C         D       E         back

                                                                               back = 3 + 1
             Front refer to index 1                                            back = 4

itemdeleted = item[front]
                                       From front/head
front = 1
                                       item[front] = NULL
            front++                                     item
             2                     0           1          2        3       4           4
            front             NULL         NULL           C        D       E          back
       front = 1 +                                                                back = 3 + 1
       1                                                                          back = 4
       front = 2
                     Front refer to index 2                                                   Continue…
deQueue operation

                                                    item
             2                 0          1           2          3       4          4
            front           NULL       NULL           C          D       E        back

                                                                                 back = 3 + 1
                     Front refer to index 2                                      back = 4


                                                  From front/head
itemdeleted = item[front]                         item[front] = NULL
front = 2

           front++                                        item
             3                     0          1           2          3       4          4
            front             NULL       NULL        NULL            D       E       back
       front = 2 +                                                                 back = 3 + 1
       1                                                                           back = 4
       front = 3
                     Front refer to index 3

                                                                                                  Continue…
deQueue operation
                                                 item
            3                0          1         2            3         4          4
          front           NULL       NULL       NULL           D         E        back

                                                                                 back = 3 + 1
                   Front refer to index 3                                        back = 4


itemdeleted = item[front]                                  From front/head
front = 3                                                  item[front] = NULL


         front++                                       item
            4                    0          1          2           3         4          4
          front             NULL       NULL       NULL         NULL          E       back
     front = 3 +                                                                   back = 3 + 1
     1                                                                             back = 4
     front = 4

                              Front refer to index 4



                                                                                                  Continue…
deQueue operation
                                                item
            4                0          1        2           3             4              4
          front           NULL       NULL       NULL        NULL           E         back

                                                                                    back = 3 + 1
                   Front refer to index 4                                           back = 4


itemdeleted = item[front]                                            From front/head
front = 4                                                            item[front] = NULL


         front++                                     item
            5                    0          1        2           3             4              4
          front             NULL       NULL      NULL        NULL         NULL            back
     front = 4 +                                                                      back = 3 + 1
     1                                                                                back = 4
     front = 5




                                                                                                     Continue…
Retrieve at front(getFront) operation

void getFront(){
          cout<<"nt#################n";
          cout<<"nt3.getFrontn";
          if(back < front){
                     cout<<"ntThere is no data to at frontn";
          }else{
                     cout<<"ntItem At Front:"<<item[front]<<endl;

          }
}




                                                                      Continue…
Retrieve at back(getRear) operation

void getRear(){
          cout<<"nt#################n";
          cout<<"nt4.getRearn";
          if(back < front){
                     cout<<"ntThere is no data to at rearn";
          }else{
                     cout<<"ntItem At Rear:"<<item[back]<<endl;

          }
}




                                                                    Continue…
destroyQueue operation

void destroyQueue(){

         delete [] item;

}




                                                Continue…
displayQueue operation

void displayQueue(){
           cout<<"ntDisplay Item In Queuen";
           if(back < front){
                      cout<<"ntThere is no data in queue to be displayedn";
           }else{
                      cout<<"t";
                      for(int i=0; i < max; i++ ){
                                  cout<<"t"<<item[i];
                      }
                      cout<<endl;
           }

}




                                                                                 Continue…
Queue Implementation Using Array(Linear)
int main()
{
int selection;
menu:
            cout<<"nPlease Choose Your Selectionn";
            cout<<"n1tenQueuen";
            cout<<"n2tdeQueuen";
            cout<<"n3tGetFrontn";
            cout<<"n4tGetRearn";
            cout<<"n5tDestroyQueuen";
            cout<<"n6tDisplayn";
            cout<<"ntSelection is:";
            cin>>selection;




                                                        Continue…
Queue Implementation Using Array(Linear)

switch(selection){
                     case 1:   enQueue();
                               displayQueue();
                               goto menu;
                               break;

                     case 2:   deQueue();
                               displayQueue();
                               goto menu;
                               break;

                     case 3:   getFront();
                               displayQueue();
                               goto menu;
                               break;




                                                 Continue…
Queue Implementation Using Array(Linear)

                case 4:             getRear();
                                    displayQueue();
                                    goto menu;
                                    break;

                case 5:             destroyQueue();
                                    displayQueue();
                                    goto menu;
                                    break;

                case 6:             displayQueue();
                                    goto menu;
                                    break;

                default:cout<<"ntWrong Selectionn";
            }
return 0;
}
Queue Implementation Using Array(Linear)

• Problem: Rightward-Drifting:

              • After a sequence of additions & removals,
               items will drift towards the end of the array
              • enQueue operation cannot be performed
               on the queue below, since back = max – 1.
   front++                        item
      5             0      1      2       3      4        4
    front          NULL   NULL   NULL    NULL   NULL    back
front = 4 +                                            back = 3 + 1
1                                                      back = 4
front = 5
Queue Implementation Using Array(Linear)

• Rightward drifting solutions
   – Shift array elements after each deletion
      • Shifting dominates the cost of the
        implementation
Queue Implementation Using Array(Linear)

 – Use a circular array: When Front or Back
   reach the end of the array, wrap them around
   to the beginning of the array
    • Problem:
       – Front & Back can't be used to
         distinguish between queue-full & queue-
         empty conditions
Queue Implementation Using Array(Linear)

    • Solution:
       – Use a counter
       – Count == 0 means empty queue
       – Count == MAX_QUEUE means full
         queue
3.0 Queue Implementation Using
        Array(Circular)
Queue Implementation Using
Array(Circular)
• Number of elements in Queue are fixed
  during declaration.
• Need isFull() operation to determine
  whether a queue is full or not.
Queue Implementation Using
  Array(Circular)

• Queue structure need at least 3 elements:

1) Element to store items in Queue

2) Element to store index at head

3) Element to store index at rear

4) Element to store index in counter
Create Queue Operation
• Declare

  – front & back are indexes in the array

  – count to store index

  – Initial condition: front =0 , back = -1, count = 0

  – Size of an array in queue
Queue Implementation Using Array(Circular)


 – The Wrap-around effect is obtained by using
   modulo arithmetic (%-operator)
                                          front = 0


                      7             0


                  6                        1




                  5                         2


                          4         3

                              back = -1         count = 0
Queue Implementation Using Array(Circular)

 – enQueue
    • Increment back, using modulo arithmetic
    • Insert item
    • Increment count
 – deQueue
    • Increment front using modulo arithmetic
    • Decrement count
 – Disadvantage
       • Overhead of maintaining a counter or
         flag
Queue Implementation Using Array(Circular)

Example Code 2:
                                                   queue
#include <iostream>                                             front = 0

using namespace std;                       7               0

                                       6                        1
#define max 8

char queue[max], newitem;
                                       5                        2
int front = 0, back = -1, count = 0;
                                               4           3

                                                    back = -1       count = 0




                                                                            Continue…
Queue Implementation Using Array(Circular)
void enQueue(){

cout<<"nt#### enQueue Circular ####n";

   if(count == max){

            cout<<"ntQueue Circular Is Full!!!n";

   }else{

   cout<<"ntfront:"<<front<<"t"<<"back:"<<back<<"tcount:"<<count<<“tmax:”<<max<<"n";

            cout<<"ntEnter Item:";                                                     front = 0

                                                                         7           0        back = 0
            cin>>newitem;
                                                                                 A
            back = (back + 1)% max;                                 6                     1
                                           back = (-1 + 1) % 8
                                           back = 0 % 8
            queue[back] = newitem;
                                           back = 0                  5                    2
            count++;            0          queue[0] = A
                             8√ 0                                            4       3
            } }                            count = 0 + 1
                                 0         count = 1
                                                                 count = 1
                                 0                                                       Continue…
enQueue Implementation Using Array(Circular)
From previous slide: front = 0, back = 0, count = 1           queue
                                                                              front = 0

                                                          7           0
                                                                  A                       back = 1
                                                      6                       1
                        back = (0 + 1) % 8                                B
                        back = 1 % 8
                        back = 1                      5                        2
             0          queue[1] = B
          8√ 1                                                4       3
                        count = 1 + 1
              0         count = 2
              1                              count = 2




                                                                                               Continue…
enQueue Implementation Using Array(Circular)
From previous slide: front = 0, back = 1, count = 2

                                                              queue           front = 0

                                                          7           0
                                                                  A
                                                      6                       1
                         back = (1 + 1) % 8                           B
                         back = 2 % 8
                                                                          C
                         back = 2                     5                        2          back = 2
              0          queue[2] = C
           8√ 2                                               4       3
                         count = 2 + 1
               0         count = 3
               2                              count = 3




                                                                                              Continue…
enQueue Implementation Using Array(Circular)
From previous slide: front = 0, back = 2, count = 3
                                                           queue
                                                                               front = 0

                                                           7           0
                                                                   A
                                                       6                       1
                           back = (2 + 1) % 8                          B
                           back = 3 % 8
                           back = 3                                        C
                                                       5                        2
               0           queue[3] = D                            D
            8√ 3                                               4       3
                           count = 3 + 1
                 0         count = 4
                 3                         count = 4                       back = 3




                                                                                           Continue…
enQueue Implementation Using Array(Circular)
From previous slide: front = 0, back = 3, count = 4
                                                           queue
                                                                               front = 0

                                                           7           0
                                                                   A
                                                       6                       1
                           back = (3 + 1) % 8                          B
                           back = 4 % 8
                           back = 4                                        C
                                                       5                        2
               0           queue[4] = E                        E   D
            8√ 4                                               4       3
                           count = 4 + 1
                 0         count = 5
                 4                         count = 5           back = 4




                                                                                           Continue…
enQueue Implementation Using Array(Circular)
From previous slide: front = 0, back = 4, count = 5
                                                                queue
                                                                                    front = 0

                                                            7               0
                                                                        A
                                                       6                            1
                           back = (4 + 1) % 8                               B
                           back = 5 % 8
                           back = 5                         F                   C
                                                       5                             2
               0           queue[5] = F                           E     D
            8√ 5                                 back = 5        4          3
                           count = 5 + 1
                 0         count = 6
                 5                         count = 6




                                                                                                Continue…
enQueue Implementation Using Array(Circular)
From previous slide: front = 0, back = 5, count = 6
                                                               queue
                                                                                   front = 0
                                           back = 6        7               0
                                                                       A
                                                       6                           1
                           back = (5 + 1) % 8              G               B
                           back = 6 % 8
                           back = 6                        F                   C
                                                       5                            2
               0           queue[6] = G                          E     D
            8√ 6                                                4          3
                           count = 6 + 1
                 0         count = 7
                 6                         count = 7




                                                                                               Continue…
enQueue Implementation Using Array(Circular)
From previous slide: front = 0, back = 6, count = 7
                                                               queue
                                                back = 7                           front = 0

                                                           7               0
                                                                H      A
                                                       6                           1
                           back = (6 + 1) % 8              G               B
                           back = 7 % 8
                           back = 7                        F                   C
                                                       5                            2
               0           queue[7] = H                          E     D
            8√ 7                                                4          3
                           count = 7 + 1
                 0         count = 8
                 7                         count = 8




                                                                                               Continue…
deQueue Implementation Using Array(Circular)
void deQueue(){

cout<<"nt#### deQueue Circular ####n";

    if(count == 0){

             cout<<"ntQueue Circular Is Empty, No Data To Be Deleted!!!n";

    }else{                                                                   queue
                                                              back = 7
             queue[front] = NULL;
                                                                         7               0
             front=(front + 1) % max;                                         H                      front = 1
                                                                    6                            1
             count--;                   queue[0] = NULL                  G               B
                                        front = (0 + 1) % 8
                                        front = 1 % 8                    F                   C
    }                                                               5                            2
                           0            front = 1                              E     D
}                       8√ 1                                                  4          3
                                        count = 8 - 1
                           0            count = 7
                           1                            count = 7


                                                                                                 Continue…
deQueue Implementation Using Array(Circular)
From previous slide: front = 1, back = 7 , count = 7



                                                               queue
                                              back = 7

                                                           7               0
                                                                H
                                                    6                              1
                        queue[1] = NULL                    G
                        front = (1 + 1) % 8
                        front = 2% 8                       F                   C
                                                       5                           2
            0           front = 2                                E     D               front = 2
         8√ 2                                                   4          3
                        count = 7 - 1
             0          count = 6
             2                          count = 6




                                                                                                   Continue…
Queue Implementation Using Array(Circular)
void displayQueue(){

cout<<"nt#### Display Queue Circular ####n";

cout<<"ntfront:"<<front<<"t"<<"back:"<<back<<"tcount:"<<count<<“tmax:”<<max<<"n";

    if(count == 0){

             cout<<"ntQueue Circular Is Empty, No Data To Be Displayn";

    }else{

             cout<<"ntItem In Queue Circularn";

             for(int i = 0; i < max; i++){

             cout<<"t"<<queue[i];

    }

    }

}

                                                                             Continue…
Queue Implementation Using Array(Circular)
int main(){

int selection;

menu:

    cout<<"nnPlease Choose Your Selectionn";

    cout<<"n1tenQueue Circularn";

    cout<<"n2tdeQueue Circularn";

    cout<<"n3tDisplay Queuen";

    cout<<"ntSelection is:";

    cin>>selection;




                                                  Continue…
Queue Implementation Using Array(Circular)
switch(selection){

   case 1:           enQueue();

                     displayQueue();

                     goto menu;

                     break;

   case 2:           deQueue();

                     displayQueue();

                     goto menu;

                     break;

   case 3:           displayQueue();

                     goto menu;

                     break;

                                              Continue…
Queue Implementation Using Array(Circular)
default:cout<<"ntWrong Selectionn";

    }

return 0;

}
4.0 Queue Implementation Using
      Linked List(Linear)
Queue Implementation Using Linked List(Linear)
Pointer-Based Implementation
• More straightforward than array-based
• Need Two external pointer (Front & Back) which front to
  trace deQueue operation and back to trace deQueue
  operation.
Create Queue Implementation Using Linked
                                  List(Linear)
Example Code 1:

#include <iostream>

using namespace std;

struct nodeQueue{

     char name;

     int age;              name         age         next

     nodeQueue *next;
                        Compiler get the initial illustrated structure of node
};




                                                                                 Continue…
Create Queue Implementation Using Linked
                              List(Linear)
nodeQueue *back_ptr = NULL;         NULL

nodeQueue *front_ptr=NULL;         back_ptr


                                    NULL

                                    front_ptr




                                                Continue…
enQueue Implementation Using Linked
                                  List(Linear)
void enQueue(){
                                                       0110
//create new node
                                       0110      Ali    29    NULL
   nodeQueue *newnode;
                                       newnode
   newnode = new nodeQueue;

cout<<"nt####enQueue####n";

//assign data field for name and age

   cout<<"Enter Name:";

   cin>>newnode->name;

   cout<<"Enter Age:";

   cin>>newnode->age;

   newnode->next = NULL;


                                                              Continue…
enQueue Implementation Using Linked
                                  List(Linear)
//insert newnode into queue                          Insertion to an empty queue
//check whether queue is empty

if((front_ptr == NULL) && (back_ptr == NULL)){                   0110

    front_ptr = newnode;                0110        Ali          29         NULL

    back_ptr = newnode;                newnode            name   age               next

}else{                                   0110
                                                                             0110
    back_ptr->next = newnode;           front_ptr
                                                                           back_ptr
    back_ptr = newnode;

}




                                                                           Continue…
enQueue Implementation Using Linked

Insertion to a non empty queue
                                 List(Linear)
                                 0111

    0111              Tina        30         NULL

   newnode             name       age         next

                                  0110


     0110               Ali        29        NULL     0110

    front_ptr                                        back_ptr
                      name         age        next


                         back_ptr->next = newnode;
                         back_ptr=newnode;




                                                                Continue…
enQueue Implementation Using Linked
                                         List(Linear)
        Insertion to a non empty queue

                                0110                     0111

 0110                Ali        29       0111     Tina    30    NULL        0111

front_ptr                                                                 back_ptr
                   name         age       next    name    age    next




                                                                        Continue…
deQueue Implementation Using Linked List(Linear)




                                          Continue…
void deQueue(){

         cout<<"nt####deQueue####n";

         //check whether queue is empty

         if((front_ptr == NULL) && (back_ptr == NULL)){

                  cout<<"ntQueue Is Empty!!!n";

         }else{

                  nodeQueue *temp;

                  temp = front_ptr;

                  if(front_ptr->next == NULL){

                            front_ptr = NULL;

                            back_ptr = NULL;         If the queue contains one item only

                            delete temp;

                  }else{

                            front_ptr = front_ptr->next;

                            delete temp; } } }

                                                                                Continue…
deQueue Implementation Using Linked List(Linear)
 If the queue contains one item only to be deleted

  nodeQueue *temp;
  temp = front_ptr;

                                        0110

     0110                  Ali          29           NULL     0110
   front_ptr                                                back_ptr
                                 name   age          next

     0110
                      if(front_ptr->next == NULL){
     temp
                      front_ptr = NULL;         NULL         NULL
                      back_ptr = NULL;         front_ptr    back_ptr
                      delete temp;
                      }else{
                      …}


                                                                       Continue…
deQueue Implementation Using Linked
                                    List(Linear)
   If the queue contains more than one item

    nodeQueue *temp;
    temp = front_ptr;
                            0110                     0111
  0110             Ali      29       0111     Tina    30    NULL      0111
front_ptr        name        age       next   name    age    next   back_ptr

  0110
  temp




                                                                    Continue…
…}else{
front_ptr = front_ptr->next;
delete temp; }

                                 0110                      0111
   0111              Ali          29    0111       Tina     30       NULL         0111
front_ptr         name            age    next       name    age          next   back_ptr

   0110
   temp

                                        0111
    0111                   Tina         30      NULL              0111
  front_ptr                    name      age     next       back_ptr




                                                                                Continue…
displayQueue Implementation Using Linked
                        List(Linear)
void displayQueue(){
cout<<"nt####Display Queue####n";
if((front_ptr == NULL) && (back_ptr == NULL)){
         cout<<"ntQueue Is Empty!!!n";
         cout<<"ntfront_ptr :"<<front_ptr<<"tback_ptr :"<<back_ptr<<endl;
}else{
         nodeQueue *cursor;
         cursor=front_ptr;
         cout<<"ntThe Elements In Queue Aren";
         cout<<"ntfront_ptr :"<<front_ptr<<"tback_ptr :"<<back_ptr<<endl;
         int node=1;
         while(cursor){
         cout<<"ntNode :"<<node++<<"tName :"<<cursor->name<<"tAge :"<<cursor-
>age<<"tcursor-next:"<<cursor->next<<endl;
         cursor=cursor->next; } }                                              Continue…
Queue Implementation Using Linked List(Linear)

int main()
{
int selection;
menu:
             cout<<"nnMenu Selectionn";
             cout<<"n1tenQueuen";
             cout<<"n2tdeQueuen";
             cout<<"n3tDisplay Queuen";
             cout<<"ntSelection is:";
             cin>>selection;




                                              Continue…
Queue Implementation Using Linked List(Linear)
    switch(selection){
              case 1:     enQueue();
                          displayQueue();
                          goto menu;
                          break;
              case 2:     deQueue();
                          displayQueue();
                          goto menu;
                          break;
              case 3:     displayQueue();
                          goto menu;
                          break;
              default:cout<<"ntWrong Selectionn";   }
              return 0;
                          }
                                                           Continue…

Mais conteúdo relacionado

Mais procurados

Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureBalwant Gorad
 
Queue data structure
Queue data structureQueue data structure
Queue data structureanooppjoseph
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operationsKamran Zafar
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturergomathi chlm
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESSowmya Jyothi
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTSoumen Santra
 
Unit 5 internal sorting &amp; files
Unit 5  internal sorting &amp; filesUnit 5  internal sorting &amp; files
Unit 5 internal sorting &amp; filesDrkhanchanaR
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked listsAbdullah Al-hazmy
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary searchNisha Soms
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 

Mais procurados (20)

Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Unit 5 internal sorting &amp; files
Unit 5  internal sorting &amp; filesUnit 5  internal sorting &amp; files
Unit 5 internal sorting &amp; files
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Queue
QueueQueue
Queue
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Queue
QueueQueue
Queue
 

Destaque

Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Roman Rodomansky
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queuesBuxoo Abdullah
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queueRojan Pariyar
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacksgrahamwell
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queuesurya pandian
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queueTareq Hasan
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 

Destaque (20)

Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Stack
StackStack
Stack
 
Stack & queue
Stack & queueStack & queue
Stack & queue
 
Queue
QueueQueue
Queue
 
Queue and stacks
Queue and stacksQueue and stacks
Queue and stacks
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Queues
QueuesQueues
Queues
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 

Semelhante a Understand Queue Concepts and Operations

4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data StructureMandeep Singh
 
Queues presentation
Queues presentationQueues presentation
Queues presentationToseef Hasan
 
CD3291 2.6 Queue.pptx
CD3291 2.6 Queue.pptxCD3291 2.6 Queue.pptx
CD3291 2.6 Queue.pptxmareeswari15
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertexSadiaSharmin40
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures LavanyaJ28
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptxsinghprpg
 
Queue data structures and operation on data structures
Queue data structures and operation on data structuresQueue data structures and operation on data structures
Queue data structures and operation on data structuresmuskans14
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationRAtna29
 

Semelhante a Understand Queue Concepts and Operations (20)

Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
CD3291 2.6 Queue.pptx
CD3291 2.6 Queue.pptxCD3291 2.6 Queue.pptx
CD3291 2.6 Queue.pptx
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Queue
QueueQueue
Queue
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
queues.pptx
queues.pptxqueues.pptx
queues.pptx
 
Queue data structures and operation on data structures
Queue data structures and operation on data structuresQueue data structures and operation on data structures
Queue data structures and operation on data structures
 
Queue
QueueQueue
Queue
 
Queues & ITS TYPES
Queues & ITS TYPESQueues & ITS TYPES
Queues & ITS TYPES
 
QUEUE.pptx
QUEUE.pptxQUEUE.pptx
QUEUE.pptx
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 

Último

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Último (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

Understand Queue Concepts and Operations

  • 2. Course Objectives At the end of the lesson students are expected to be able to: • Understand queue concepts and applications. • Understand queue structure and operations that can be done on queue. • Understand and know how to implement queue using array and linked list : linear array, circular array, linear link list and circular list.
  • 4. Introduction to Queue • New items enter at the back, or rear, of the queue • Items leave from the front of the queue • First-in, first-out (FIFO) property – The first item inserted into a queue is the first item to leave – Middle elements are logically inaccessible
  • 5. Introduction to Queue • Important in simulation & analyzing the behavior of complex systems
  • 6. Queue Applications • Real-World Applications – Cashier lines in any store – Check out at a bookstore – Bank / ATM – Call an airline
  • 7. Queue Applications • Computer Science Applications – Print lines of a document – Printer sharing between computers – Recognizing palindromes – Shared resource usage (CPU, memory access, …)
  • 8. Queue Applications • Simulation – A study to see how to reduce the wait involved in an application
  • 9. Queue implementation Remove/ Add/ A B C Enqueue Dequeue Front/Head Back/Rear Basic Structure of a Queue: •Data structure that hold the queue •head •rear
  • 10. Queue implementation Add/ A B C D Enqueue Head Rear Insert D into Queue (enQueue) : D is inserted at rear Remove/ Dequeue A B C D Head Rear Delete from Queue (deQueue) : A is removed
  • 11. Queue operations • Queue operations – Create an empty queue – Destroy a queue – Determine whether a queue is full – Add a new item to the queue (enQueue) – Determine whether a queue is empty – Remove the item that was added earliest(deQueue) – Retrieve at Front(getFront) – Retrieve at Back the item that was added earliest(getRear)
  • 12. Queue Implementation Implementation: – Array-based (Linear or Circular) – Pointer-based : Link list (Linear or Circular)
  • 13. 2.0 Queue Implementation Using Array(Linear)
  • 14. Queue Implementation Using Array(Linear) • Number of elements in Queue are fixed during declaration. • Need isFull() operation to determine whether a queue is full or not.
  • 15. Queue Implementation Using Array(Linear) • Queue structure need at least 3 elements: 1) Element to store items in Queue 2) Element to store index at head 3) Element to store index at rear
  • 16. Create Queue Operation • Declare – front & back are indexes in the array – Initial condition: front =0 & back = -1 – Size of an array in queue Queue 0 0 1 2 3 Max size -1 front back
  • 17. Create Queue operation Example Code 1 #include <iostream> using namespace std; #define max 5 int front = 0, back = -1; Create Queue char item[max], newitem; item 0 0 1 2 3 4 -1 front back Front refer to index 0 Continue…
  • 18. enQueue operation void enQueue(){ cout<<"nt#################n"; cout<<"nt1. enQueuen"; //check queue is full if(back == max - 1){ cout<<"ntQueue Is Full, Cannot Add Item In Queuen"; }else{ cout<<"nttEnter Item:"; cin>>newitem; back++; item[back]=newitem; cout<<endl; enQueue } } item back++ 0 0 1 2 3 4 0 front A back back = -1+1 back = 0 Front refer to index 0 From back/rear item[back] = newitem Continue…
  • 19. enQueue operation item back++ 0 0 1 2 3 4 1 front A B back back = 0 +1 back = 1 Front refer to index 0 From back/rear item[back] = newitem item back++ 0 0 1 2 3 4 2 front A B C back back = 1 +1 Front refer to index 0 back = 2 From back/rear item[back] = newitem Continue…
  • 20. enQueue operation item back++ 0 0 1 2 3 4 3 front A B C D back back = 2 +1 back = 3 Front refer to index 0 From back/rear item[back] = newitem item back++ 0 0 1 2 3 4 4 front A B C D E back back = 3 +1 back = 4 Front refer to index 0 From back/rear item[back] = newitem Continue…
  • 21. deQueue operation void deQueue(){ cout<<"nt#################n"; cout<<"nt2.deQueuen"; if(back < front){ cout<<"ntThere is no data to remove from queuen"; }else{ char itemdeleted; itemdeleted=item[front]; deQueue item[front] = NULL; cout<<"ntItem Remove From Queue:"<<itemdeleted<<endl; front++; } cout<<endl; item } 0 0 1 2 3 4 4 front A B C D E back back = 3 + 1 itemdeleted = item[front] Front refer to index 0 back = 4 front = 0 From front/head item[front] = NULL Continue…
  • 22. deQueue operation front++ item 1 0 1 2 3 4 4 front NULL B C D E back front = 0 + back = 3 + 1 1 back = 4 front = 1 Front refer to index 1 item 1 0 1 2 3 4 4 front NULL B C D E back back = 3 + 1 Front refer to index 1 back = 4 itemdeleted = item[front] From front/head front = 1 item[front] = NULL front++ item 2 0 1 2 3 4 4 front NULL NULL C D E back front = 1 + back = 3 + 1 1 back = 4 front = 2 Front refer to index 2 Continue…
  • 23. deQueue operation item 2 0 1 2 3 4 4 front NULL NULL C D E back back = 3 + 1 Front refer to index 2 back = 4 From front/head itemdeleted = item[front] item[front] = NULL front = 2 front++ item 3 0 1 2 3 4 4 front NULL NULL NULL D E back front = 2 + back = 3 + 1 1 back = 4 front = 3 Front refer to index 3 Continue…
  • 24. deQueue operation item 3 0 1 2 3 4 4 front NULL NULL NULL D E back back = 3 + 1 Front refer to index 3 back = 4 itemdeleted = item[front] From front/head front = 3 item[front] = NULL front++ item 4 0 1 2 3 4 4 front NULL NULL NULL NULL E back front = 3 + back = 3 + 1 1 back = 4 front = 4 Front refer to index 4 Continue…
  • 25. deQueue operation item 4 0 1 2 3 4 4 front NULL NULL NULL NULL E back back = 3 + 1 Front refer to index 4 back = 4 itemdeleted = item[front] From front/head front = 4 item[front] = NULL front++ item 5 0 1 2 3 4 4 front NULL NULL NULL NULL NULL back front = 4 + back = 3 + 1 1 back = 4 front = 5 Continue…
  • 26. Retrieve at front(getFront) operation void getFront(){ cout<<"nt#################n"; cout<<"nt3.getFrontn"; if(back < front){ cout<<"ntThere is no data to at frontn"; }else{ cout<<"ntItem At Front:"<<item[front]<<endl; } } Continue…
  • 27. Retrieve at back(getRear) operation void getRear(){ cout<<"nt#################n"; cout<<"nt4.getRearn"; if(back < front){ cout<<"ntThere is no data to at rearn"; }else{ cout<<"ntItem At Rear:"<<item[back]<<endl; } } Continue…
  • 28. destroyQueue operation void destroyQueue(){ delete [] item; } Continue…
  • 29. displayQueue operation void displayQueue(){ cout<<"ntDisplay Item In Queuen"; if(back < front){ cout<<"ntThere is no data in queue to be displayedn"; }else{ cout<<"t"; for(int i=0; i < max; i++ ){ cout<<"t"<<item[i]; } cout<<endl; } } Continue…
  • 30. Queue Implementation Using Array(Linear) int main() { int selection; menu: cout<<"nPlease Choose Your Selectionn"; cout<<"n1tenQueuen"; cout<<"n2tdeQueuen"; cout<<"n3tGetFrontn"; cout<<"n4tGetRearn"; cout<<"n5tDestroyQueuen"; cout<<"n6tDisplayn"; cout<<"ntSelection is:"; cin>>selection; Continue…
  • 31. Queue Implementation Using Array(Linear) switch(selection){ case 1: enQueue(); displayQueue(); goto menu; break; case 2: deQueue(); displayQueue(); goto menu; break; case 3: getFront(); displayQueue(); goto menu; break; Continue…
  • 32. Queue Implementation Using Array(Linear) case 4: getRear(); displayQueue(); goto menu; break; case 5: destroyQueue(); displayQueue(); goto menu; break; case 6: displayQueue(); goto menu; break; default:cout<<"ntWrong Selectionn"; } return 0; }
  • 33. Queue Implementation Using Array(Linear) • Problem: Rightward-Drifting: • After a sequence of additions & removals, items will drift towards the end of the array • enQueue operation cannot be performed on the queue below, since back = max – 1. front++ item 5 0 1 2 3 4 4 front NULL NULL NULL NULL NULL back front = 4 + back = 3 + 1 1 back = 4 front = 5
  • 34. Queue Implementation Using Array(Linear) • Rightward drifting solutions – Shift array elements after each deletion • Shifting dominates the cost of the implementation
  • 35. Queue Implementation Using Array(Linear) – Use a circular array: When Front or Back reach the end of the array, wrap them around to the beginning of the array • Problem: – Front & Back can't be used to distinguish between queue-full & queue- empty conditions
  • 36. Queue Implementation Using Array(Linear) • Solution: – Use a counter – Count == 0 means empty queue – Count == MAX_QUEUE means full queue
  • 37. 3.0 Queue Implementation Using Array(Circular)
  • 38. Queue Implementation Using Array(Circular) • Number of elements in Queue are fixed during declaration. • Need isFull() operation to determine whether a queue is full or not.
  • 39. Queue Implementation Using Array(Circular) • Queue structure need at least 3 elements: 1) Element to store items in Queue 2) Element to store index at head 3) Element to store index at rear 4) Element to store index in counter
  • 40. Create Queue Operation • Declare – front & back are indexes in the array – count to store index – Initial condition: front =0 , back = -1, count = 0 – Size of an array in queue
  • 41. Queue Implementation Using Array(Circular) – The Wrap-around effect is obtained by using modulo arithmetic (%-operator) front = 0 7 0 6 1 5 2 4 3 back = -1 count = 0
  • 42. Queue Implementation Using Array(Circular) – enQueue • Increment back, using modulo arithmetic • Insert item • Increment count – deQueue • Increment front using modulo arithmetic • Decrement count – Disadvantage • Overhead of maintaining a counter or flag
  • 43. Queue Implementation Using Array(Circular) Example Code 2: queue #include <iostream> front = 0 using namespace std; 7 0 6 1 #define max 8 char queue[max], newitem; 5 2 int front = 0, back = -1, count = 0; 4 3 back = -1 count = 0 Continue…
  • 44. Queue Implementation Using Array(Circular) void enQueue(){ cout<<"nt#### enQueue Circular ####n"; if(count == max){ cout<<"ntQueue Circular Is Full!!!n"; }else{ cout<<"ntfront:"<<front<<"t"<<"back:"<<back<<"tcount:"<<count<<“tmax:”<<max<<"n"; cout<<"ntEnter Item:"; front = 0 7 0 back = 0 cin>>newitem; A back = (back + 1)% max; 6 1 back = (-1 + 1) % 8 back = 0 % 8 queue[back] = newitem; back = 0 5 2 count++; 0 queue[0] = A 8√ 0 4 3 } } count = 0 + 1 0 count = 1 count = 1 0 Continue…
  • 45. enQueue Implementation Using Array(Circular) From previous slide: front = 0, back = 0, count = 1 queue front = 0 7 0 A back = 1 6 1 back = (0 + 1) % 8 B back = 1 % 8 back = 1 5 2 0 queue[1] = B 8√ 1 4 3 count = 1 + 1 0 count = 2 1 count = 2 Continue…
  • 46. enQueue Implementation Using Array(Circular) From previous slide: front = 0, back = 1, count = 2 queue front = 0 7 0 A 6 1 back = (1 + 1) % 8 B back = 2 % 8 C back = 2 5 2 back = 2 0 queue[2] = C 8√ 2 4 3 count = 2 + 1 0 count = 3 2 count = 3 Continue…
  • 47. enQueue Implementation Using Array(Circular) From previous slide: front = 0, back = 2, count = 3 queue front = 0 7 0 A 6 1 back = (2 + 1) % 8 B back = 3 % 8 back = 3 C 5 2 0 queue[3] = D D 8√ 3 4 3 count = 3 + 1 0 count = 4 3 count = 4 back = 3 Continue…
  • 48. enQueue Implementation Using Array(Circular) From previous slide: front = 0, back = 3, count = 4 queue front = 0 7 0 A 6 1 back = (3 + 1) % 8 B back = 4 % 8 back = 4 C 5 2 0 queue[4] = E E D 8√ 4 4 3 count = 4 + 1 0 count = 5 4 count = 5 back = 4 Continue…
  • 49. enQueue Implementation Using Array(Circular) From previous slide: front = 0, back = 4, count = 5 queue front = 0 7 0 A 6 1 back = (4 + 1) % 8 B back = 5 % 8 back = 5 F C 5 2 0 queue[5] = F E D 8√ 5 back = 5 4 3 count = 5 + 1 0 count = 6 5 count = 6 Continue…
  • 50. enQueue Implementation Using Array(Circular) From previous slide: front = 0, back = 5, count = 6 queue front = 0 back = 6 7 0 A 6 1 back = (5 + 1) % 8 G B back = 6 % 8 back = 6 F C 5 2 0 queue[6] = G E D 8√ 6 4 3 count = 6 + 1 0 count = 7 6 count = 7 Continue…
  • 51. enQueue Implementation Using Array(Circular) From previous slide: front = 0, back = 6, count = 7 queue back = 7 front = 0 7 0 H A 6 1 back = (6 + 1) % 8 G B back = 7 % 8 back = 7 F C 5 2 0 queue[7] = H E D 8√ 7 4 3 count = 7 + 1 0 count = 8 7 count = 8 Continue…
  • 52. deQueue Implementation Using Array(Circular) void deQueue(){ cout<<"nt#### deQueue Circular ####n"; if(count == 0){ cout<<"ntQueue Circular Is Empty, No Data To Be Deleted!!!n"; }else{ queue back = 7 queue[front] = NULL; 7 0 front=(front + 1) % max; H front = 1 6 1 count--; queue[0] = NULL G B front = (0 + 1) % 8 front = 1 % 8 F C } 5 2 0 front = 1 E D } 8√ 1 4 3 count = 8 - 1 0 count = 7 1 count = 7 Continue…
  • 53. deQueue Implementation Using Array(Circular) From previous slide: front = 1, back = 7 , count = 7 queue back = 7 7 0 H 6 1 queue[1] = NULL G front = (1 + 1) % 8 front = 2% 8 F C 5 2 0 front = 2 E D front = 2 8√ 2 4 3 count = 7 - 1 0 count = 6 2 count = 6 Continue…
  • 54. Queue Implementation Using Array(Circular) void displayQueue(){ cout<<"nt#### Display Queue Circular ####n"; cout<<"ntfront:"<<front<<"t"<<"back:"<<back<<"tcount:"<<count<<“tmax:”<<max<<"n"; if(count == 0){ cout<<"ntQueue Circular Is Empty, No Data To Be Displayn"; }else{ cout<<"ntItem In Queue Circularn"; for(int i = 0; i < max; i++){ cout<<"t"<<queue[i]; } } } Continue…
  • 55. Queue Implementation Using Array(Circular) int main(){ int selection; menu: cout<<"nnPlease Choose Your Selectionn"; cout<<"n1tenQueue Circularn"; cout<<"n2tdeQueue Circularn"; cout<<"n3tDisplay Queuen"; cout<<"ntSelection is:"; cin>>selection; Continue…
  • 56. Queue Implementation Using Array(Circular) switch(selection){ case 1: enQueue(); displayQueue(); goto menu; break; case 2: deQueue(); displayQueue(); goto menu; break; case 3: displayQueue(); goto menu; break; Continue…
  • 57. Queue Implementation Using Array(Circular) default:cout<<"ntWrong Selectionn"; } return 0; }
  • 58. 4.0 Queue Implementation Using Linked List(Linear)
  • 59. Queue Implementation Using Linked List(Linear) Pointer-Based Implementation • More straightforward than array-based • Need Two external pointer (Front & Back) which front to trace deQueue operation and back to trace deQueue operation.
  • 60. Create Queue Implementation Using Linked List(Linear) Example Code 1: #include <iostream> using namespace std; struct nodeQueue{ char name; int age; name age next nodeQueue *next; Compiler get the initial illustrated structure of node }; Continue…
  • 61. Create Queue Implementation Using Linked List(Linear) nodeQueue *back_ptr = NULL; NULL nodeQueue *front_ptr=NULL; back_ptr NULL front_ptr Continue…
  • 62. enQueue Implementation Using Linked List(Linear) void enQueue(){ 0110 //create new node 0110 Ali 29 NULL nodeQueue *newnode; newnode newnode = new nodeQueue; cout<<"nt####enQueue####n"; //assign data field for name and age cout<<"Enter Name:"; cin>>newnode->name; cout<<"Enter Age:"; cin>>newnode->age; newnode->next = NULL; Continue…
  • 63. enQueue Implementation Using Linked List(Linear) //insert newnode into queue Insertion to an empty queue //check whether queue is empty if((front_ptr == NULL) && (back_ptr == NULL)){ 0110 front_ptr = newnode; 0110 Ali 29 NULL back_ptr = newnode; newnode name age next }else{ 0110 0110 back_ptr->next = newnode; front_ptr back_ptr back_ptr = newnode; } Continue…
  • 64. enQueue Implementation Using Linked Insertion to a non empty queue List(Linear) 0111 0111 Tina 30 NULL newnode name age next 0110 0110 Ali 29 NULL 0110 front_ptr back_ptr name age next back_ptr->next = newnode; back_ptr=newnode; Continue…
  • 65. enQueue Implementation Using Linked List(Linear) Insertion to a non empty queue 0110 0111 0110 Ali 29 0111 Tina 30 NULL 0111 front_ptr back_ptr name age next name age next Continue…
  • 66. deQueue Implementation Using Linked List(Linear) Continue…
  • 67. void deQueue(){ cout<<"nt####deQueue####n"; //check whether queue is empty if((front_ptr == NULL) && (back_ptr == NULL)){ cout<<"ntQueue Is Empty!!!n"; }else{ nodeQueue *temp; temp = front_ptr; if(front_ptr->next == NULL){ front_ptr = NULL; back_ptr = NULL; If the queue contains one item only delete temp; }else{ front_ptr = front_ptr->next; delete temp; } } } Continue…
  • 68. deQueue Implementation Using Linked List(Linear) If the queue contains one item only to be deleted nodeQueue *temp; temp = front_ptr; 0110 0110 Ali 29 NULL 0110 front_ptr back_ptr name age next 0110 if(front_ptr->next == NULL){ temp front_ptr = NULL; NULL NULL back_ptr = NULL; front_ptr back_ptr delete temp; }else{ …} Continue…
  • 69. deQueue Implementation Using Linked List(Linear) If the queue contains more than one item nodeQueue *temp; temp = front_ptr; 0110 0111 0110 Ali 29 0111 Tina 30 NULL 0111 front_ptr name age next name age next back_ptr 0110 temp Continue…
  • 70. …}else{ front_ptr = front_ptr->next; delete temp; } 0110 0111 0111 Ali 29 0111 Tina 30 NULL 0111 front_ptr name age next name age next back_ptr 0110 temp 0111 0111 Tina 30 NULL 0111 front_ptr name age next back_ptr Continue…
  • 71. displayQueue Implementation Using Linked List(Linear) void displayQueue(){ cout<<"nt####Display Queue####n"; if((front_ptr == NULL) && (back_ptr == NULL)){ cout<<"ntQueue Is Empty!!!n"; cout<<"ntfront_ptr :"<<front_ptr<<"tback_ptr :"<<back_ptr<<endl; }else{ nodeQueue *cursor; cursor=front_ptr; cout<<"ntThe Elements In Queue Aren"; cout<<"ntfront_ptr :"<<front_ptr<<"tback_ptr :"<<back_ptr<<endl; int node=1; while(cursor){ cout<<"ntNode :"<<node++<<"tName :"<<cursor->name<<"tAge :"<<cursor- >age<<"tcursor-next:"<<cursor->next<<endl; cursor=cursor->next; } } Continue…
  • 72. Queue Implementation Using Linked List(Linear) int main() { int selection; menu: cout<<"nnMenu Selectionn"; cout<<"n1tenQueuen"; cout<<"n2tdeQueuen"; cout<<"n3tDisplay Queuen"; cout<<"ntSelection is:"; cin>>selection; Continue…
  • 73. Queue Implementation Using Linked List(Linear) switch(selection){ case 1: enQueue(); displayQueue(); goto menu; break; case 2: deQueue(); displayQueue(); goto menu; break; case 3: displayQueue(); goto menu; break; default:cout<<"ntWrong Selectionn"; } return 0; } Continue…