c++链队列的基本操作——有详细注解

#include <iostream>
using namespace std;
//链队列
typedef int DataType;
#define Node ElemType

class Node
{
public:
	DataType data;//结点数据域
	ElemType *next;//结点指针域
};//定义一个结点类(链类需要)

class QueueLinkList 
{
public:
	QueueLinkList();				//初始化队列
	~QueueLinkList();				//摧毁队列
	int IsEmpty();					//判断队列是否为空
	void Enqueue(DataType data);	//入队
	void Dequeue();					//出对
	void PrintfQueue();				//打印队列
private:
	ElemType *front;//对头指针
	ElemType *rear;//队尾指针
};

//初始化队列
QueueLinkList::QueueLinkList()
{
	front = new ElemType;//使对头指针指向新创建的结点
	front->data = 0;//为队头指针的数据域赋值
	front->next = NULL;//指向下一个为空
	rear = front;//队头与队尾重合
}

//摧毁队列
QueueLinkList::~QueueLinkList()//析构函数实现
{
	delete front;
}

//判断队列是否为空
int QueueLinkList::IsEmpty()
{
	return front->next == NULL;//下一个指针域为空,数据域没值
}

//入队
void QueueLinkList::Enqueue(DataType data)
{
	ElemType *newNode = new ElemType;//每次入队都要新创一个结点
	newNode->data = data;//为新创建的数据域赋值
	newNode->next = NULL;//然后将下一个指针域置空
	rear->next = newNode;//使队尾指针指向新创建的结点
	rear = newNode;//使新创建的结点成为队尾
}

//出队
void QueueLinkList::Dequeue()
{
	if (front->next == NULL)
	{
		cout << "队为空" << endl;
	}
	ElemType *ptemp = front->next;//先进先出,创建一个新的指针承接第二个位置的指针域
	front->next = ptemp->next;//使指向第二个元素的指针变成指向第三个元素的指针
	delete ptemp;//删除新创建的,然后就完成了对头指针的交接
}

//遍历队列
void QueueLinkList::PrintfQueue()
{
	ElemType *p = front;//使新创建的指向对头
	if (front->next == NULL)
	{
		cout << "队为空" << endl;
		return;
	}
	while (p->next != NULL)
	{
		p = p->next;//逐个指向
		cout << p->data << " ";//逐个输出完成遍历打印
	}
	cout << endl;
}

//测试函数
int main()
{
	QueueLinkList q;//创建对象q
	int i;
	cout << "1.判断队是否为空   2.入队   3.出队   4.打印队   0.退出" << endl;
	do//循环函数加开关函数巧妙完成
	{
		cout << "请输入要执行的操作:";
		cin >> i;
		switch (i)
		{
		case 1:
			if (q.IsEmpty() == 1)
				cout << "空队" << endl;
			else
			{
				cout << "不是空队" << endl;
			}
			break;
		case 2:
			int n;
			cout << "请输入要入队的值(一次只能入一个值哦):";
			cin >> n;
			q.Enqueue(n);
			break;
		case 3:
			q.Dequeue();
			break;
		case 4:
			cout << "打印队列" << endl;
			q.PrintfQueue();
			break;
		default:
			cout << "输入的操作值错误" << endl;
			break;
		}
	} while (i != 0);
	system("pause");
	return 0;
}


  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链队列是通过链表实现的队列,链队列基本操作包括: 1. 初始化:初始化一个空的链队列。 2. 入队:将一个元素插入队列尾部。 3. 出队:删除队头元素,并返回其值。 4. 取队头元素:返回队头元素的值。 5. 判空:判断链队列是否为空。 6. 遍历:按顺序输出队列中的所有元素。 以下是链队列基本操作实现的 C 语言代码: ```c #include <stdio.h> #include <stdlib.h> // 链队列结点的结构体 struct Node { int data; struct Node *next; }; // 链队列的结构体 typedef struct { struct Node *front; // 队头指针 struct Node *rear; // 队尾指针 } LinkQueue; // 初始化链队列 void initQueue(LinkQueue *Q) { Q->front = Q->rear = (struct Node *) malloc(sizeof(struct Node)); Q->front->next = NULL; } // 判断链队列是否为空 int isEmpty(LinkQueue *Q) { return Q->front == Q->rear; } // 元素入队 void enQueue(LinkQueue *Q, int x) { struct Node *newNode = (struct Node *) malloc(sizeof(struct Node)); newNode->data = x; newNode->next = NULL; Q->rear->next = newNode; Q->rear = newNode; } // 元素出队 int deQueue(LinkQueue *Q) { if (isEmpty(Q)) { printf("队列已空,不能删除元素!\n"); exit(1); } else { struct Node *p = Q->front->next; int x = p->data; Q->front->next = p->next; if (Q->rear == p) { Q->rear = Q->front; } free(p); return x; } } // 取队头元素 int getFront(LinkQueue *Q) { if (isEmpty(Q)) { printf("队列已空,没有队头元素!\n"); exit(1); } else { return Q->front->next->data; } } // 遍历链队列 void traverseQueue(LinkQueue *Q) { struct Node *p = Q->front->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { LinkQueue Q; initQueue(&Q); enQueue(&Q, 1); enQueue(&Q, 2); enQueue(&Q, 3); enQueue(&Q, 4); printf("遍历队列:"); traverseQueue(&Q); printf("队头元素:%d\n", getFront(&Q)); printf("出队元素:%d\n", deQueue(&Q)); printf("出队元素:%d\n", deQueue(&Q)); printf("遍历队列:"); traverseQueue(&Q); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值