用链表实现一个双端的队列

既然是双端的队列,那使用一个双向的链表来实现不过分ba///

(请原谅我使用如此恶劣的词汇)

双端队列的基本操作///

访问队头元素  _top_()

访问队尾元素 _back_()

在队头插入一个元素 _push_top_()

在队尾插入一个元素 _push_back_()

删除队头元素  _pop_top_()

删除队尾元素  _pop_back_()

链表的基本结构///

typedef struct node {
	int data;
	struct node* next;
	struct node* before;
}NODE;

//<双端队列>//
NODE* head = nullptr;
NODE* tail = nullptr;

访问队头和队尾的元素///

在这里,我们把链表头作为队头,把链表尾作为队尾///

inline int _top_() { return head->data; }
inline int _back_() { return tail->data; }

 插入元素///

//队头插入

即在链表头指针的前面插入节点,然后把头指针向前移动一个节点就好了///

void _push_top_(int data)
{
	NODE* _temp = new NODE();
	if (_temp == NULL)
	{
		cout << "no enough memory..\n";
		system("pause");
		exit(0);
	}
	_temp->before = NULL;
	_temp->data = data;
	if (head == NULL)
	{
		head = _temp;
		head->next = NULL;
		tail = head;
		return;
	}
	//如果已经存在了一些节点,只要把新的放到第一个就行了
	_temp->next = head;			//连接整个链表
	head->before = _temp;		//回连
	head = _temp;				//更新链表头
	return;
}

//队尾插入

void _push_back_(int data)
{
	NODE* _temp = new NODE();
	if (_temp == NULL)
	{
		cout << "no enough memory..\n";
		system("pause");
		exit(0);
	}
	_temp->next = NULL;
	_temp->data = data;
	if (head == NULL)
	{
		head = _temp;
		head->before = NULL;
		tail = head;
		return;
	}
	//以前有过节点
	tail->next = _temp;
	_temp->before = tail;
	tail = _temp;
	return;
}

原理和队头插入原理相差不大///

在这里,因为链表的头尾指针都设置成了全局变量,所以我们的函数不需要返回值///

删除元素///

//删除队头

即把链表头指针 head 指向的节点删除,然后让head指向下一个节点就好了///

void _pop_top_()
{
	NODE* _temp = head;
	head = head->next;
	head->before = NULL;
	delete _temp;
	return;
}

//删除队尾

(同理),把链表的尾指针所指向的节点删除,然后让尾指针指向它的前一个节点就好了///

void _pop_back_()
{
	NODE* _temp = tail;
	tail = tail->before;
	tail->next = NULL;
	delete _temp;
	return;
}

总体的代码实现///

#include <iostream>
using namespace std;
typedef struct node {
	int data;
	struct node* next;
	struct node* before;
}NODE;

//<双端队列>//
NODE* head = nullptr;
NODE* tail = nullptr;

inline int _top_() { return head->data; }
inline int _back_() { return tail->data; }
void _push_top_(int data)
{
	NODE* _temp = new NODE();
	if (_temp == NULL)
	{
		cout << "no enough memory..\n";
		system("pause");
		exit(0);
	}
	_temp->before = NULL;
	_temp->data = data;
	if (head == NULL)
	{
		head = _temp;
		head->next = NULL;
		tail = head;
		return;
	}
	//如果已经存在了一些节点,只要把新的放到第一个就行了
	_temp->next = head;			//连接整个链表
	head->before = _temp;		//回连
	head = _temp;				//更新链表头
	return;
}
void _push_back_(int data)
{
	NODE* _temp = new NODE();
	if (_temp == NULL)
	{
		cout << "no enough memory..\n";
		system("pause");
		exit(0);
	}
	_temp->next = NULL;
	_temp->data = data;
	if (head == NULL)
	{
		head = _temp;
		head->before = NULL;
		tail = head;
		return;
	}
	//以前有过节点
	tail->next = _temp;
	_temp->before = tail;
	tail = _temp;
	return;
}

void _pop_top_()
{
	NODE* _temp = head;
	head = head->next;
	head->before = NULL;
	delete _temp;
	return;
}

void _pop_back_()
{
	NODE* _temp = tail;
	tail = tail->before;
	tail->next = NULL;
	delete _temp;
	return;
}

void _show_()		//展示队列,检查错误
{
	NODE* p = head;
	while (p != NULL)
	{
		cout << p->data << ' ';
		p = p->next;
	}
	putchar('\n');
	return;
}

int main()
{
	//插入元素
	for (int i = 0; i <= 9; ++i)
		_push_back_(i);
	//展示队列,检查错误
	_show_();
	//访问队头和队尾
	cout<<_top_()<<"    " << _back_() << '\n';
	//删除队头
	_pop_top_();
	//删除队尾
	_pop_back_();
	//展示队列
	_show_();
	system("pause");
	return 0;
}

再见 = =///

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值