Circular QUEUE In c++
Hai Teman-teman,disini saya akan sharing sedikit tentang contoh program dari Circular QUEUE,oke teman-teman ini dia contoh program nya:
CIRCULAR QUEUE
IN C++
|
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;
const int maxs = 5;
const int maxq = 5;
typedef struct
{ char isi[maxq];
int front,rear;
} queue;
void initqueue(queue &Q)
{ Q.front =0 ;Q.rear =-1 ;
for(int i=0;i<maxq;i++) Q.isi[i]=48;
}
void enqueue(queue &Q,char x)
{ if((Q.isi[(Q.rear+1)%maxq])==48) {
Q.rear++;
Q.rear=Q.rear % maxq;
Q.isi[Q.rear ]=x ;
cout<<"enqueue
q["<<Q.rear<<"]="<<x<<endl;
}
else cout<<"full\n";
}
void dequeue(queue &Q,char &x)
{ if(Q.isi[Q.front]==48) cout<<"Empty";
else {
Q.front=Q.front % maxq;
x=Q.isi[Q.front] ;
Q.isi[Q.front]=48;
cout<<"dequeque q["<<Q.front
<<"]= "<<x<<endl;
cout<<"Isi q["<<Q.front
<<"]="<<Q.isi[Q.front]<<endl;
Q.front++;
}
}
void PrintQueue(queue Q)
{
for(int i=0;i<maxq;i++){
if(Q.isi[(Q.front+i)%maxq]!= 48){
cout<<Q.isi[(Q.front+i)%maxq]<<" ";}}
cout<<endl;
}
main()
{
queue Q1;
int i,n;
char x;
cout<<"Masukkan Data : ";cin>>n;
initqueue(Q1);
for (i=0;i<n;i++) {
x=(65+rand()%26);
enqueue(Q1,x);
}
cout<<endl;
PrintQueue(Q1);
dequeue(Q1,x);
x='X';
enqueue(Q1,x);
cout<<endl;
PrintQueue(Q1);
dequeue(Q1,x);
x='Y';
enqueue(Q1,x);
cout<<endl;
PrintQueue(Q1);
dequeue(Q1,x);
x='T';
enqueue(Q1,x);
cout<<endl;
PrintQueue(Q1);
dequeue(Q1,x);
dequeue(Q1,x);
PrintQueue(Q1);
getch();
}
|
EmoticonEmoticon