Queue oop

Preview:

Citation preview

Queue

Prepared by:

Eng. Ahmed & Mohamed Taha

Agenda

Introduction to Queue.

How Queue works.

Operations performed on Queue.

Queue Implementation.

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)

Company Logo

Introduction to Queue

Company Logo

Introduction to Queue

Company Logo

Introduction to Queue

Queue Implementation using Object Oriented

Programming

Queue Implementation using Array

#include <iostream.h>#include <conio.h>#include <stdlib.h>

#define max_size 100#define TRUE 1#define FALSE 0typedef int boolean;

/************************************/Class queue{ private:

int items[max_size];int front,rear;

public:

};/************************************/

Queue Implementation using address

/////////////////////////////funcions// Initialize Functionvoid queue:: queue(){

front=0;rear=-1;

}

// Is_empty Functionboolean queue:: is_empty(){

if (rear < front)return TRUE;

return FALSE;}

Queue Implementation using address

// Insert Functionvoid queue:: insert(int item){

if (rear == max_size-1){

cout<<"Queue is Overflow";exit(0);

}elserear++;items[rear]=item;

}

Queue Implementation using address

// Remove Functionint queue:: remove(){

if (is_empty()){

cout<<"Queue is underflow";return -1;

}int item=items[front];front++;return item;

}

Queue Implementation using address

// Print all elements Funcionvoid 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;

}

Queue Implementation using address

void main(){

clrscr();queue q;q.insert(1);q.insert(2);q.insert(3);q.insert(4);q.print_all_elements(q);int val=q.remove();val=q.remove();q.print_all_elements();q.insert(5);q.insert(6);q.print_all_elements();getch();

}

Recommended