2020-10-07

C++数据结构

队列的链式结构的实现与操作

定义一个链式队列,可以对队列进行“入队”、“出队”、“清空队列”、“获取队首元素”等操作。键盘输入一些命令,可以执行上述操作。本题中,队列的元素为字符。
#include
using namespace std;
struct Node{
char data;
Node* next;

};
class CirQueue{
public:
CirQueue();
void EnQueue(char x);
char DeQueue();
char GetHead();
void ClQueue();
private:
Node*front,rear;
};
CirQueue::CirQueue()
{
Node
s = NULL;
s = new Node;
s->next = NULL;
front = rear = s;
}

void CirQueue::EnQueue(char x) {
Node* s = NULL;
s = new Node;
s->data = x;
s->next = NULL;
rear->next = s;
rear = s;
}

char CirQueue::DeQueue() {
if (front == rear) {
throw"None";
}
Node* p = front->next;
char x = p->data;
front->next = p->next;

if (p->next == NULL) {
    rear = front;
}
delete p;
return x;

}

char CirQueue::GetHead() {
if (rear == front) {
throw"None";
}

return front->next->data;

}

void CirQueue::ClQueue() {
Node* s = new Node;
s->next = NULL;
rear = front = s;
}

int main()
{
char t;
CirQueue S;

while (1)
{
	cin >> t;
	if (t == 'E') {

		cin >> t;
        S.EnQueue(t);
		
	}
	else if (t == 'D')
	{
		try
		{
			cout << S.DeQueue() << endl;
		}
		catch (const char* str)
		{
			cout << str << endl;
		}
	}
	else if (t == 'G')
	{
		try
		{
			cout << S.GetHead() << endl;
		}
		catch (const char* str)
		{
			cout << str << endl;
		}
	}
	else if (t == 'C')
	{
		S.ClQueue();
	}
	else
	{
		break;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值