📄 数据结构实现-队列-循环队列-用链表实现[anank].cpp
字号:
/*********************************************************************************
** Program Name : implementation of circle queue ( by chain )
** Author Name : Lu Jian Hua
** Time : 2007-5-23
**********************************************************************************/
#include <iostream>
using namespace std ;
class Node
{
public:
Node() : data(0), next(NULL) {}
int data ;
Node *next ;
} ;
class Queue // class of Queue
{
public:
Queue() ;
Node* GetValue() const ; // get the current value of Queue
void Delete() ; // out_Queue
void Append(int value) ; // In__Queue
void Cleaner() ; // cleaner of Queue
bool IsEmpty() const ; // is empty ?
bool IsFull() const ; // is full ?
private:
Node* front ; // front
Node* rear ; // rear
} ;
Queue::Queue() : front(NULL), rear(NULL) {}
Node* Queue::GetValue() const
{
if ( IsEmpty() )
{
cout << "The queue is empty!" << endl << endl ;
return 0 ;
}
return front ;
}
void Queue::Append(int value)
{
Node *temp = new Node() ;
temp->data = value ;
if (front == NULL) // no element in the queue
{
front = temp ;
rear = front ;
rear->next = NULL ;
}
else
{
rear->next = temp ;
rear = temp ;
rear->next = NULL ;
}
}
void Queue::Delete()
{
if ( IsEmpty() )
{
cout << "The queue is empty!" << endl << endl ;
return ;
}
Node *record = front ;
front = front->next ;
delete record ;
cout << "The specific node has beend deleted!" << endl << endl ;
}
void Queue::Cleaner()
{
while (front)
{
Node *record = front->next ;
delete front ;
front = record ;
}
cout << "Cleaner has done it's work...\n\n" ;
}
bool Queue::IsEmpty() const
{
return (front == NULL) ;
}
bool Queue::IsFull() const
{
return ( false ) ; // never full until no enough memory accommodated
}
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? " << endl
<< "6> Release the queue? " ;
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':
ptr->Delete() ;
break ;
case '3':
cout << "The value getted : " ;
if ( ptr->IsEmpty() )
cout << "NULL" << endl << endl ;
else
cout << (ptr->GetValue())->data << 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 ;
case '6':
ptr->Cleaner() ;
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 + -