13

Click here to load reader

Queue oop

Embed Size (px)

Citation preview

Page 1: Queue   oop

Queue

Prepared by:

Eng. Ahmed & Mohamed Taha

Page 2: Queue   oop

Agenda

Introduction to Queue.

How Queue works.

Operations performed on Queue.

Queue Implementation.

Page 3: Queue   oop

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)

Page 4: Queue   oop

Company Logo

Introduction to Queue

Page 5: Queue   oop

Company Logo

Introduction to Queue

Page 6: Queue   oop

Company Logo

Introduction to Queue

Page 7: Queue   oop

Queue Implementation using Object Oriented

Programming

Page 8: Queue   oop

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:

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

Page 9: Queue   oop

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;}

Page 10: Queue   oop

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;

}

Page 11: Queue   oop

Queue Implementation using address

// Remove Functionint queue:: remove(){

if (is_empty()){

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

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

}

Page 12: Queue   oop

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;

}

Page 13: Queue   oop

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();

}