aimn20034
29-11-2007, 01:59 PM
السلام عليكم
شباب انا عندي تكليف من دكتور المادة ( داتا ستركشر) عمل circular queue
الحقيقة انا سويت بس عن طريق المصفوفات وهذا هو الكود
#include <iostream>
#include <stdlib.h>
using namespace std;
class Queue
{
private:
enum {MAX=10};
int *pArray;
int back,front;
int size;
public:
Queue(int s=MAX);
~Queue();
void enqueue(int e);
int dequeue();
bool isFull();
bool isEmpty();
};
Queue::Queue(int s):
size(s),back(0),front(0)
{
pArray=new int[s];
}
Queue::~Queue()
{
delete [] pArray;
pArray=0;
}
void Queue::enqueue(int e)
{
if(isFull())
{
cout<<"\nQueue is Full";
exit(1);
}
back=(back+1)%size;
pArray[back]=e;
}
int Queue::dequeue()
{
if(isEmpty())
{
cout<<"\nQueue is Empty";
exit(1);
}
front=(front+1)%size;
return pArray[front];
}
bool Queue::isFull()
{
if( (back+1)%size == front )
return true;
else
return false;
}
bool Queue::isEmpty()
{
if( back == front )
return true;
else
return false;
}
int main()
{
Queue q(10);
cout<<"\nenqueue(2)";
q.enqueue(2);
cout<<"\nenqueue(4)";
q.enqueue(4);
cout<<"\nenqueue(6)";
q.enqueue(6);
cout<<"\nDequeue elements\n";
cout<<"dequeue: "<<q.dequeue()<<endl;
cout<<"dequeue: "<<q.dequeue()<<endl;
cout<<"dequeue: "<<q.dequeue()<<endl;
return 0;
}
شباب انا عندي تكليف من دكتور المادة ( داتا ستركشر) عمل circular queue
الحقيقة انا سويت بس عن طريق المصفوفات وهذا هو الكود
#include <iostream>
#include <stdlib.h>
using namespace std;
class Queue
{
private:
enum {MAX=10};
int *pArray;
int back,front;
int size;
public:
Queue(int s=MAX);
~Queue();
void enqueue(int e);
int dequeue();
bool isFull();
bool isEmpty();
};
Queue::Queue(int s):
size(s),back(0),front(0)
{
pArray=new int[s];
}
Queue::~Queue()
{
delete [] pArray;
pArray=0;
}
void Queue::enqueue(int e)
{
if(isFull())
{
cout<<"\nQueue is Full";
exit(1);
}
back=(back+1)%size;
pArray[back]=e;
}
int Queue::dequeue()
{
if(isEmpty())
{
cout<<"\nQueue is Empty";
exit(1);
}
front=(front+1)%size;
return pArray[front];
}
bool Queue::isFull()
{
if( (back+1)%size == front )
return true;
else
return false;
}
bool Queue::isEmpty()
{
if( back == front )
return true;
else
return false;
}
int main()
{
Queue q(10);
cout<<"\nenqueue(2)";
q.enqueue(2);
cout<<"\nenqueue(4)";
q.enqueue(4);
cout<<"\nenqueue(6)";
q.enqueue(6);
cout<<"\nDequeue elements\n";
cout<<"dequeue: "<<q.dequeue()<<endl;
cout<<"dequeue: "<<q.dequeue()<<endl;
cout<<"dequeue: "<<q.dequeue()<<endl;
return 0;
}