Date post: | 03-Jul-2015 |
Category: |
Education |
Upload: | maju-islamabad |
View: | 360 times |
Download: | 2 times |
BY : SADIA ZAR
Two Basic Operations in Queue
Enque : Inserting new element from rear end
Deque : Removing existing element from front end
Front End
Rear End
0 1 2 3 4 5 6
#include <iostream>
using namespace std;
class Queue
{
int* array,size,f,r,count;
public:
Queue();
void Enque(int value);
int Deque();
bool is_Empty();
bool is_Full();
};
Queue::Queue()
{
cout<<"Enter size : ";
cin>>size;
array=new int[size];
f=r=-1;
count=0;
}
void Queue::Enque(int value){
if(!is_Full()){
array[++r]=value;count++;
}else{
cout<<"\n\nOverFlow ! \n\n";}
}
int Queue::Deque()
{
if(!is_Empty())
{
count--;
return array[++f];
}
else
{
cout<<"\n\nUnderFlow ! \n\n";
}
}
bool Queue::is_Empty()
{
return f==r==-1;
}
bool Queue::is_Full()
{
return count==size;
}
void main(){Queue obj;obj.Enque(1);obj.Enque(2);obj.Enque(3);obj.Deque();
}
1 2 3
0 1 2 3 4 5 6
#include <iostream>using namespace std;class Node{
int value;Node*next;friend class Queue;public:Node(){
next=NULL;}
};
class Queue{
Node *first,*last;public:Queue(){
first=last=NULL;}void Enque(int v);bool is_Empty();void Deque();void Display();
};
first
last
void Queue::Enque(int v){
Node *n=new Node(); n->value=v;if(first==NULL){
last=first=n;}else{
last->next=n;last=n;
}}
DATA
NEXT
void Queue::Deque(){
if(!is_Empty()){
Node *temp=first;first=first->next;cout<<"\n\nThe data Dequeued is : "<<temp->value<<endl;delete temp;
}}
bool Queue::is_Empty()
{
return first==NULL||last==NULL;
}
void Queue::Display()
{
Node *p = new Node;
p = first;
cout<<"\n\nQueue Elements are : \n\n";
while(p!=NULL)
{
cout<<p->value<<endl;
p = p->next;
}
}
Void main(){
Queue Q;Q.Enque(12); Q.Enque(15);Q. Deque();Q .Display();
}
100
200
12
15
200
NULL
100
200
first
last
200