链队的总结

#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#include<string>
using namespace std;
#define ERROR '#'
typedef char QElementType;
typedef struct QNode *QueuePtr;
//结构是带有头结点(头结点数据域无有意义的值,下一个节点才是队头元素)的单链表,但通过队头指针(指向单链表的头结点)和队尾指针(指向队尾元素)操作队列
//带头结点是因为有无元素的情况,无元素时不能队头队尾都指向NULL否则无法插入
//和单链表一样的数据结构+队头队尾指针的结构体
typedef struct QNode{
	QElementType data;
	QueuePtr  next;
}QNode;
typedef struct LinkQueue{
	QueuePtr front;//队头指针
	QueuePtr rear;//队尾指针
}LinkQueue;
typedef enum {enqueue,dequeue,gethead,END,wrong} op;

//初始化
void InitQueue(LinkQueue &Q){
	Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));//生成新节点作为头结点,对头和队尾指针指向此节点
	Q.front->next = NULL;//初始化头结点的指针域置空,数据域无意义也不用写
	return;
}

//入队
//和循环队列不同,不需要判断队满,链式结构没有最大容量
void EnQueue(LinkQueue &Q, QElementType e){
	QueuePtr p = (QueuePtr)malloc(sizeof(QNode));//入队元素p
	p->data = e;//将新节点数据域置e
	p->next = NULL; Q.rear->next = p;//将新节点插入到队尾
	Q.rear = p;//修改队尾指针
	return;
}

//出队
bool DeQueue(LinkQueue &Q, QElementType &e){
	if (Q.front == Q.rear)return false;//队空则返回false
	QueuePtr p = Q.front->next;//p指向队头元素(头结点的下一个节点)
	e = p->data;
	Q.front->next = p->next;//修改头结点的指针域,指向队头元素的下一个元素
	
	if (Q.rear == p)Q.rear = Q.front;//如果最后一个元素被删(队头元素就是队尾元素),队尾指针指向队头指针(即队空)

	delete p;//释放原队头元素(被删了)的空间
	return true;
}

//取队头元素
QElementType GetHead(LinkQueue Q){
	if (Q.front == Q.rear)
		return ERROR;
	return Q.front->next->data;
}
//选择操作
op opwhitch(string a){
	if (a == "enqueue")return enqueue;
	else if (a == "dequeue")return dequeue;
	else if (a == "gethead")return gethead;
	else if (a == "end")return END;
	else
		return wrong;
}
int main()
{
	LinkQueue Q;
	InitQueue(Q);
	string a;
	QElementType e;
	while (1){
		cin >> a;
		switch (opwhitch(a))
		{
			case enqueue:
				cin >> e;
				EnQueue(Q, e);
				break;
			case dequeue:
				if (DeQueue(Q, e))
					printf("DeQueue:%c\n", e);
				else
					printf("DeQueue:NULL\n");
				break;
			case gethead:
				e = GetHead(Q);
				if (e!=ERROR)
					printf("Head is:%c\n", e);
				else
					printf("Head is:NULL\n");
				break;
			case END:
				printf("end\n");
				system("PAUSE");
				return 0;
			default:
				printf("WRONG\n");
			    break;
		}
	}
	system("PAUSE");
	return 0;
}

0.结构体

结构是带有头结点(头结点数据域无有意义的值,下一个节点才是队头元素)的单链表,但通过队头指针(指向单链表的头结点)和队尾指针(指向队尾元素)操作队列

带头结点是因为有无元素的情况,无元素时不能队头队尾都指向NULL否则无法插入

//和单链表一样的数据结构+队头队尾指针的结构体
typedef char QElementType;
typedef struct QNode *QueuePtr;
typedef struct QNode{
	QElementType data;
	QueuePtr  next;
}QNode;
typedef struct LinkQueue{
	QueuePtr front;//队头指针
	QueuePtr rear;//队尾指针
}LinkQueue;

1.初始化

void InitQueue(LinkQueue &Q){
	Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));//生成新节点作为头结点,对头和队尾指针指向此节点
	Q.front->next = NULL;//初始化头结点的指针域置空,数据域无意义也不用写
	return;
}

主函数

LinkQueue Q;
	InitQueue(Q);

2.入队

和循环队列不同,不需要判断队满,链式结构没有最大容量

typedef char QElementType;
typedef struct QNode *QueuePtr;
typedef struct LinkQueue{
	QueuePtr front;//队头指针
	QueuePtr rear;//队尾指针
}LinkQueue;

void EnQueue(LinkQueue &Q, QElementType e){
	QueuePtr p = (QueuePtr)malloc(sizeof(QNode));//入队元素p
	p->data = e;//将新节点数据域置e
	p->next = NULL; Q.rear->next = p;//将新节点插入到队尾
	Q.rear = p;//修改队尾指针
	return;
}

3.出队

typedef char QElementType;
typedef struct QNode *QueuePtr;
typedef struct LinkQueue{
	QueuePtr front;//队头指针
	QueuePtr rear;//队尾指针
}LinkQueue;

bool DeQueue(LinkQueue &Q, QElementType &e){
	if (Q.front == Q.rear)return false;//队空则返回false
	QueuePtr p = Q.front->next;//p指向队头元素(头结点的下一个节点)
	e = p->data;
	Q.front->next = p->next;//修改头结点的指针域,指向队头元素的下一个元素
	
	if (Q.rear == p)Q.rear = Q.front;//如果最后一个元素被删(队头元素就是队尾元素),队尾指针指向队头指针(即队空)

	delete p;//释放原队头元素(被删了)的空间
	return true;
}

4.取队头元素

#define ERROR '#'
typedef char QElementType;
typedef struct QNode *QueuePtr;
typedef struct LinkQueue{
	QueuePtr front;//队头指针
	QueuePtr rear;//队尾指针
}LinkQueue;

QElementType GetHead(LinkQueue Q){
	if (Q.front == Q.rear)
		return ERROR;
	return Q.front->next->data;
}

5.选择操作

typedef enum {enqueue,dequeue,gethead,END,wrong} op;

op opwhitch(string a){
	if (a == "enqueue")return enqueue;
	else if (a == "dequeue")return dequeue;
	else if (a == "gethead")return gethead;
	else if (a == "end")return END;
	else
		return wrong;
}

主函数

string a;
	QElementType e;
	while (1){
		cin >> a;
		switch (opwhitch(a))
		{
			case enqueue:
				cin >> e;
				EnQueue(Q, e);
				break;
			case dequeue:
				if (DeQueue(Q, e))
					printf("DeQueue:%c\n", e);
				else
					printf("DeQueue:NULL\n");
				break;
			case gethead:
				e = GetHead(Q);
				if (e!=ERROR)
					printf("Head is:%c\n", e);
				else
					printf("Head is:NULL\n");
				break;
			case END:
				printf("end\n");
				system("PAUSE");
				return 0;
			default:
				printf("WRONG\n");
			    break;
		}
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deosiree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值