既然是双端的队列,那使用一个双向的链表来实现不过分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;
}
再见 = =///