2. Agenda
Introduction to Queue.
How Queue works.
Operations performed on Queue.
Queue Implementation.
3. Introduction to Queue
Queue is an ordered collection of items in which new data
items are added at the end, or tail, of the queue while
other data are removed from the front, or head, of the
queue. For this reason, a queue is referred to as a FIFO
structure (First-In First-Out)
The main primitive operations of a queue are known as:
Add adds a new node
Remove removes a node
Additional primitives can be defined:
IsEmpty: reports whether the queue is empty
IsFull: reports whether the queue is full
Initialize: creates/initializes the queue
Destroy: deletes the contents of the queue (may be
implemented by re-initializing the queue)
8. Queue Implementation using Array
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#define max_size 100
#define TRUE 1
#define FALSE 0
typedef int boolean;
/************************************/
Class queue
{
private:
int items[max_size];
int front,rear;
public:
};
9. Queue Implementation using address
/////////////////////////////funcions
// Initialize Function
void queue:: queue()
{
front=0;
rear=-1;
}
// Is_empty Function
boolean queue:: is_empty()
{
if (rear < front)
return TRUE;
return FALSE;
}
10. Queue Implementation using address
// Insert Function
void queue:: insert(int item)
{
if (rear == max_size-1)
{
cout<<"Queue is Overflow";
exit(0);
}
else
rear++;
items[rear]=item;
}
11. Queue Implementation using address
// Remove Function
int queue:: remove()
{
if (is_empty())
{
cout<<"Queue is underflow";
return -1;
}
int item=items[front];
front++;
return item;
}
12. Queue Implementation using address
// Print all elements Funcion
void queue:: print_all_elements ()
{
cout<<"the current items of the queue are:"<<endl;
for(int i=front;i<=rear;i++)
cout<<items[i]<<" ";
cout<<endl;
}