C++实现链式队列

#include<iostream>
using namespace std;
typedef int T;

//单链队列
class Qnode
{
public:
	T data;
	Qnode* next;
};

class LinkQueue
{
public:
	LinkQueue();
	~LinkQueue();
	void EnLinkQueue();  //入队
	void DeLinkQueue(); //出队
	void ShowLinkQueue(); //遍历
	void GetEleLinkQueue();  //查找指定数值

private:
	Qnode* front;
	Qnode* rear;
};

LinkQueue::LinkQueue()
{
	front = rear = new Qnode;
	front->next = NULL;
}
LinkQueue::~LinkQueue()
{
	while (front->next) {
		Qnode* ptemp;
		ptemp = front->next;
		front->next = ptemp->next;
		delete ptemp;
		ptemp = NULL;
	}
	delete front; 
	front = rear = NULL;
}

void LinkQueue::EnLinkQueue()  //入队
{
	Qnode* pnew = new Qnode;
	cout << "输入入队数据:" << " ";
	cin >> pnew->data;
	pnew->next = NULL;
	rear->next = pnew;
	rear = pnew;
	pnew = NULL;
}

void LinkQueue::DeLinkQueue()  //出队
{
	if (front == rear) {
		cout << "空队!" << endl;
	}
	Qnode* ptemp = front->next;
	cout << "出队数据为:" << ptemp->data << endl;
	front->next = ptemp->next;
	if (rear == ptemp) {
		rear = front;
	}
	delete ptemp;
	ptemp = NULL;
}

void LinkQueue::ShowLinkQueue()  //遍历
{
	Qnode* ptemp = front->next;
	cout << "队列元素为:" << endl;
	while (ptemp)
	{
		cout << ptemp->data << " ";
		ptemp = ptemp->next;
	}
	cout << endl;
}

void LinkQueue::GetEleLinkQueue()  //查找指定数值
{
	int i = 0;  int e = 0; cout << "输入查询值:" << " ";  cin >> e;
	Qnode* ptemp = front->next;
	while (ptemp) {
		if (ptemp->data == e) {
			i++;
		}
		ptemp = ptemp->next;
	}
	cout << "队列中存在" << i << "个" << e << endl;
}

int main()
{
	LinkQueue q;
	q.EnLinkQueue();
	q.EnLinkQueue();
	q.EnLinkQueue();
	q.EnLinkQueue();
	q.ShowLinkQueue();

	q.GetEleLinkQueue();
	q.ShowLinkQueue();

	q.DeLinkQueue();
	q.ShowLinkQueue();


	system("pause");
	return 0;
}

实现效果如下:

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。链式队列的基本操作包括:初始化队列、判断队列是否为空、入队、出队、取队头元素以及清空队列。以下是用 C 语言实现链式队列基本操作的示例代码: ``` #include<stdlib.h> #include<stdio.h> #define MaxSize 50 typedef struct node { int data; struct node *pNext; }Node, *pNode; typedef struct queue { pNode front; pNode rear; }Queue, *pQueue; //初始化队列 void InitQueue(pQueue q) { q->front = q->rear = (pNode)malloc(sizeof(Node)); if(NULL == q->front) { printf("初始化队列失败!"); exit(-1); } q->front->pNext = NULL; } //判断队列是否为空 int IsEmpty(pQueue q) { return q->front == q->rear ? 1 : 0; } //入队 void EnQueue(pQueue q, int value) { pNode pNew = (pNode)malloc(sizeof(Node)); if(NULL == pNew) { printf("入队失败!"); return; } pNew->data = value; pNew->pNext = NULL; q->rear->pNext = pNew; q->rear = pNew; } //出队 int DeQueue(pQueue q) { if(IsEmpty(q)) { printf("队列为空,无法出队!"); return -1; } pNode p = q->front->pNext; int value = p->data; q->front->pNext = p->pNext; if(q->rear == p) q->rear = q->front; //更新尾指针 free(p); return value; } //取队头元素 int GetHead(pQueue q) { if(IsEmpty(q)) { printf("队列为空,无法获取头元素!"); return -1; } return q->front->pNext->data; } //清空队列 void ClearQueue(pQueue q) { pNode p = q->front->pNext; pNode qNode; while(p) { qNode = p; p = p->pNext; free(qNode); } q->front = q->rear; } int main() { Queue q; InitQueue(&q); int i; for(i = 1; i <= MaxSize; ++i) EnQueue(&q, i); printf("队列头元素为:%d\n", GetHead(&q)); printf("出队的元素为:"); while(!IsEmpty(&q)) printf("%d ", DeQueue(&q)); printf("\n"); ClearQueue(&q); return 0; } ``` 请注意,这里的回答仅供参考,如有需要请自行验证代码的正确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值