实现队列运算程序(对字符队列进行操作)

队列的入队出队,0出队,@打印并结束,其他字符入队

在这里插入图片描述


#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct LinkNode//节点的结构体
{
	char data;
	LinkNode *next;
}LinkNode;
typedef struct LinkQueue//队列的结构体
{
	LinkNode *front, *rear;
}LinkQueue;

bool Emptyqueue(LinkQueue q)
{
	if (q.front == q.rear)//空返回true
	{
		return true;
	}
	else
	{
		return false;//不空
	}
}

void Clearqueue(LinkQueue &q)
{
	LinkNode *now;
	if (!Emptyqueue(q))
	{
		while (q.front != q.rear)
		{
			now = q.front->next;
			q.front->next = now->next;
			delete(now);
		}
	}
}

void Enqueue(LinkQueue &q,char c)
{
	LinkNode *n = new(LinkNode);
	n->data = c;
	n -> next = NULL;
	if (Emptyqueue(q))//如果队列为空,进行初始化
	{
		q.front = q.rear = new(LinkNode);
		q.front->next = NULL;
	}
	q.rear->next = n;
	q.rear = n;
}

char Dequeue(LinkQueue &q)
{
	if (q.front == q.rear)
	{
		cout << "队列为空" << endl;
		return '\0';
	}
	else
	{
		LinkNode *now = q.front->next;
		char c = now->data;
		q.front->next = now->next;
		if (q.rear == now)//表示原本只有一个点,并且该点已经剔除
			q.front = q.rear = NULL;
		delete(now);
		return c;
	}
}

void printQueue(LinkQueue &q)
{
	LinkNode *now;
	while (q.front != q.rear)
	{
		now = q.front->next;
		cout << now->data << " ";
		q.front->next = now->next;
		delete(now);
	}
	cout << endl;
}

int main()
{
	char c;
	LinkQueue q;
	while (true)
	{
		cin >> c;
		if (c == '0')//出队
		{
			char now = Dequeue(q);
			cout << now << endl;
		}
		else if (c == '@')
		{
			printQueue(q);
			break;
		}
		else
		{
			Enqueue(q,c);
		}
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值