z
Presentation Topic
Dynamic Queue.
z
Queues
 A Queue is a special kind of list, where
items are inserted at one end (the rear)
And deleted at the other end (the front).
 Accessing the elements of queues follows
a First In First Out (FIFO) order.
 Example
 Like customers standing in a check-out
line in a store, the first customer in is
the first customer served.
2
z
Common Operations on Queues
 getFRONT(Q): Returns the first element on
Queue Q.
 ENQUEUE(x,Q): Inserts element x at the
end of Queue Q.
 DEQUEUE(Q): Deletes the first element of
Q.
 ISEMPTY(Q): Returns true if and only if Q
is an empty queue.
 ISFULL(Q): Returns true if and only if Q is
full.
3
z
Enqueue and Dequeue
 Primary queue operations: Enqueue and
Dequeue
 Enqueue – insert an element at the rear of
the queue.
 Dequeue – remove an element from the
front of the queue.
4
z
Queues Implementations
Static:
Queue is implemented by an array, and size
of queue remains fix
Dynamic:
A queue can be implemented as a linked
list and expand or shrink with each enqueue
or dequeue operation.
5
z
Static Implementation of Queues
6
z
Dynamic Implementation of Queues
Dynamic implementation is done using pointers.
• Front : A pointer to the first element of the
queue.
• Rear : A pointer to the last element of the
queue.
7
x y z
Front
Rear
.
z
Dynamic Implemenatation
• Enqueue (X)
• Enqueue (Y)
8
Q.front
Q.Rear
.
x
Q.front
Q.Rear
x .
y
z
Dynamic Implementation
• Dequeue:
• MakeNULL:
9
Q.front
Q.Rear
.
y
Q.front
Q.Rear
NULL
z
Dynamic implementation of Queue
struct queueNode
{
int num;
queueNode next;
};
queueNode front = NULL;
queueNode rear = NULL;
class dynQueue{
int size;
10
public:
dynQueue();
void enqueue();
void dequeue();
bool isEmpty();
void displayQueue();
void makeNull();
};
z
Constructor
dynQueue()
{
size=0;
}
11
z Enqueue( ) Function
Void enqueue(int x)
{
/*Linked list node*/
queueNode *n = new Node();
n->setData(x);
n->Next= NULL;
12
if (front == NULL)
{
front = n;
rear = n;
size++;
}
else
{
rear->Next=n;
rear = n;
size++;
}
}
z
Dequeue( ) Function
int dequeue()
{
if(front == NULL)
{
cout<<“Queue is Empty“;
}
else
{ queueNode *temp=front;
int x = front->getData();
front = front->getNext();
Delete temp;
return x;
}
}
13
z
getFront() and isEmpty()
int getFront()
{
return front->Data;
}
int isEmpty()
{
return ( front == NULL );
}
14
z
Display( ) Function
void display()
{
if(isEmpty())
cout<<“Queue is empty“;
else
{
for (int i=0; i<counter; i++)
cout<<queue[(front+ i)% maxSize];
}
}
15
z
Use of Queues
Out of the numerous uses of the queues, one of the most
useful is simulation.
A simulation program attempts to model a real-world
phenomenon.
Many popular video games are simulations, e.g., SimCity,
Flight Simulator.
Each object and action in the simulation has a counterpart in
real world.
16
z
Uses of Queues
If the simulation is accurate, the result of the program should
mirror the results of the real-world event.
Thus it is possible to understand what occurs in the real-world
without actually observing its occurrence.
17

Dynamic Queue.pptx

  • 1.
  • 2.
    z Queues  A Queueis a special kind of list, where items are inserted at one end (the rear) And deleted at the other end (the front).  Accessing the elements of queues follows a First In First Out (FIFO) order.  Example  Like customers standing in a check-out line in a store, the first customer in is the first customer served. 2
  • 3.
    z Common Operations onQueues  getFRONT(Q): Returns the first element on Queue Q.  ENQUEUE(x,Q): Inserts element x at the end of Queue Q.  DEQUEUE(Q): Deletes the first element of Q.  ISEMPTY(Q): Returns true if and only if Q is an empty queue.  ISFULL(Q): Returns true if and only if Q is full. 3
  • 4.
    z Enqueue and Dequeue Primary queue operations: Enqueue and Dequeue  Enqueue – insert an element at the rear of the queue.  Dequeue – remove an element from the front of the queue. 4
  • 5.
    z Queues Implementations Static: Queue isimplemented by an array, and size of queue remains fix Dynamic: A queue can be implemented as a linked list and expand or shrink with each enqueue or dequeue operation. 5
  • 6.
  • 7.
    z Dynamic Implementation ofQueues Dynamic implementation is done using pointers. • Front : A pointer to the first element of the queue. • Rear : A pointer to the last element of the queue. 7 x y z Front Rear .
  • 8.
    z Dynamic Implemenatation • Enqueue(X) • Enqueue (Y) 8 Q.front Q.Rear . x Q.front Q.Rear x . y
  • 9.
    z Dynamic Implementation • Dequeue: •MakeNULL: 9 Q.front Q.Rear . y Q.front Q.Rear NULL
  • 10.
    z Dynamic implementation ofQueue struct queueNode { int num; queueNode next; }; queueNode front = NULL; queueNode rear = NULL; class dynQueue{ int size; 10 public: dynQueue(); void enqueue(); void dequeue(); bool isEmpty(); void displayQueue(); void makeNull(); };
  • 11.
  • 12.
    z Enqueue( )Function Void enqueue(int x) { /*Linked list node*/ queueNode *n = new Node(); n->setData(x); n->Next= NULL; 12 if (front == NULL) { front = n; rear = n; size++; } else { rear->Next=n; rear = n; size++; } }
  • 13.
    z Dequeue( ) Function intdequeue() { if(front == NULL) { cout<<“Queue is Empty“; } else { queueNode *temp=front; int x = front->getData(); front = front->getNext(); Delete temp; return x; } } 13
  • 14.
    z getFront() and isEmpty() intgetFront() { return front->Data; } int isEmpty() { return ( front == NULL ); } 14
  • 15.
    z Display( ) Function voiddisplay() { if(isEmpty()) cout<<“Queue is empty“; else { for (int i=0; i<counter; i++) cout<<queue[(front+ i)% maxSize]; } } 15
  • 16.
    z Use of Queues Outof the numerous uses of the queues, one of the most useful is simulation. A simulation program attempts to model a real-world phenomenon. Many popular video games are simulations, e.g., SimCity, Flight Simulator. Each object and action in the simulation has a counterpart in real world. 16
  • 17.
    z Uses of Queues Ifthe simulation is accurate, the result of the program should mirror the results of the real-world event. Thus it is possible to understand what occurs in the real-world without actually observing its occurrence. 17