📄 数据结构实现-队列-循环队列-用数组实现.txt
字号:
/*********************************************************************************
** Program Name : implementation of circle queue ( by array )
** Author Name : Lu Jian Hua
** Time : 2007-5-23
**********************************************************************************/
#include <iostream>
using namespace std ;
class Queue // class of Queue
{
public:
Queue() ;
int GetValue() const ; // get the current value of Queue
int Delete() ; // out_Queue
void Append(int value) ; // In__Queue
bool IsEmpty() const ; // is empty ?
bool IsFull() const ; // is full ?
private:
int front ; // front
int rear ; // rear
enum { MAX_SIZE = 10 } ; // Notice:remember ; at the end
int element[MAX_SIZE] ; // implement with array
} ;
Queue::Queue() : front(0), rear(0) {}
int Queue::GetValue() const
{
if ( IsEmpty() )
{
cout << "The queue is empty!" << endl << endl ;
return 0 ;
}
int temp = front ;
return element[++temp] ;
}
void Queue::Append(int value)
{
if ( IsFull() )
{
cout << "The queue is full!" << endl << endl ;
return ;
}
rear = (rear+1) % MAX_SIZE ;
element[rear] = value ;
}
int Queue::Delete()
{
if ( IsEmpty() )
{
cout << "The queue is empty!" << endl << endl ;
return 0 ;
}
//return element[(++front) % MAX_SIZE] ; // different from the next sentence??
front = (front+1) % MAX_SIZE ;
return element[front] ;
}
bool Queue::IsEmpty() const
{
return (rear == front) ;
}
bool Queue::IsFull() const
{
return ( (rear+1) % MAX_SIZE == front ) ;
}
Queue* CreateQueue()
{
Queue *ptr = new Queue() ;
return ptr ;
}
int main()
{
Queue *ptr = CreateQueue() ;
while (true)
{
cout << "-------------------------------------------------" << endl
<< "1> Append" << endl
<< "2> Delete" << endl
<< "3> Get the value" << endl
<< "4> Is empty ? " << endl
<< "5> Is Full? " ;
char c = 0 ;
cin >> c ;
cout << endl ;
int value = 0 ;
switch (c)
{
case '1':
cout << "Enter the value : " ;
cin >> value ;
ptr->Append(value) ;
break ;
case '2':
cout << "The value deleted : " ;
if ( ptr->IsEmpty() )
cout << "NULL" << endl << endl ;
else
cout << ptr->Delete() << endl << endl ;
break ;
case '3':
cout << "The value getted : " ;
if ( ptr->IsEmpty() )
cout << "NULL" << endl << endl ;
else
cout << ptr->GetValue() << endl << endl ;
break ;
case '4':
cout << "Is empty ? " << ( ptr->IsEmpty() ? "Yes" : "No" ) << endl << endl ;
break ;
case '5':
cout << "Is full ? " << ( ptr->IsFull() ? "Yes" : "No" ) << endl << endl ;
break ;
default:
break ;
}
}
return 0 ;
}
/********************************************************************************
*
* Notice : This Program Can Be Launched In VC6.0 Environment
*
********************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -