#include <iostream>
using namespace std;
#define ulli size_t
template<class T>class DualStack {
private:
struct Node {
T data;
Node* next;
};
Node* head, *tail;
Node* p;
ulli length;
public:
DualStack() {
head = NULL;
length = 0;
tail = NULL;
p = NULL;
}
~DualStack() {
Node* ptr = NULL;
while (head != NULL) {
ptr = head->next;
delete head;
head = ptr;
}
}
void push_back(T n) {
Node* q = new Node;
q->data = n;
if (head == NULL) {
q->next = head;
head = q;
p = q;
}
else {
q->next = p;
p = q;
}
length++;
}
void push_front(T n) {
Node* q = new Node;
q->data = n;
q->next = tail;
tail = q;
length++;
}
void pop_front() {
if (length <= 0) {
abort();
}
Node* q;
T data;
q = tail;
data = tail->data;
tail = tail->next;
delete(q);
length--;
}
void pop_back() {
if (length <= 0) {
abort();
}
Node* q;
T data;
q = head;
data = head->data;
head = head->next;
delete(q);
length--;
}
ulli size() {
return length;
}
T front() {
return tail->data;
}
T back() {
return head->data;
}
bool empty() {
if (length == 0) {
return true;
}
else {
return false;
}
}
void clear() {
while (length > 0) {
pop_back();
pop_front();
}
}
};
标签:Node,head,队列,双端,tail,C++,next,length,data
From: https://blog.csdn.net/j5486545648564/article/details/143216863