cpp-algo-cases/chapter_stack_and_queue/linkedlist_deque.cpp

195 lines
4.7 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: linkedlist_deque.cpp
* Created Time: 2023-03-02
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* ˫<><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> */
struct DoublyListNode {
int val; // <20>ڵ<EFBFBD>ֵ
DoublyListNode *next; // <20><><EFBFBD>̽ڵ<CCBD>ָ<EFBFBD><D6B8>
DoublyListNode *prev; // ǰ<><C7B0><EFBFBD>ڵ<EFBFBD>ָ<EFBFBD><D6B8>
DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {
}
};
/* <20><><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
class LinkedListDeque {
private:
DoublyListNode *front, *rear; // ͷ<>ڵ<EFBFBD> front <20><>β<EFBFBD>ڵ<EFBFBD> rear
int queSize = 0; // ˫<><CBAB><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD>
public:
/* <20><><EFBFBD><EFBFBD><ECB7BD> */
LinkedListDeque() : front(nullptr), rear(nullptr) {
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
~LinkedListDeque() {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ڵ㣬<DAB5>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
DoublyListNode *pre, *cur = front;
while (cur != nullptr) {
pre = cur;
cur = cur->next;
delete pre;
}
}
/* <20><>ȡ˫<C8A1><CBAB><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
int size() {
return queSize;
}
/* <20>ж<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
bool isEmpty() {
return size() == 0;
}
/* <20><><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD> */
void push(int num, bool isFront) {
DoublyListNode *node = new DoublyListNode(num);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><D5A3><EFBFBD><EFBFBD><EFBFBD> front <20><> rear <20><>ָ<EFBFBD><D6B8> node
if (isEmpty())
front = rear = node;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD>
else if (isFront) {
// <20><> node <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>
front->prev = node;
node->next = front;
front = node; // <20><><EFBFBD><EFBFBD>ͷ<EFBFBD>ڵ<EFBFBD>
// <20><>β<EFBFBD><CEB2><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD>
} else {
// <20><> node <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>
rear->next = node;
node->prev = rear;
rear = node; // <20><><EFBFBD><EFBFBD>β<EFBFBD>ڵ<EFBFBD>
}
queSize++; // <20><><EFBFBD><EFBFBD><C2B6>г<EFBFBD><D0B3><EFBFBD>
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void pushFirst(int num) {
push(num, true);
}
/* <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD> */
void pushLast(int num) {
push(num, false);
}
/* <20><><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD> */
int pop(bool isFront) {
if (isEmpty())
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
int val;
// <20><><EFBFBD>׳<EFBFBD><D7B3>Ӳ<EFBFBD><D3B2><EFBFBD>
if (isFront) {
val = front->val; // <20>ݴ<EFBFBD>ͷ<EFBFBD>ڵ<EFBFBD>ֵ
// ɾ<><C9BE>ͷ<EFBFBD>ڵ<EFBFBD>
DoublyListNode *fNext = front->next;
if (fNext != nullptr) {
fNext->prev = nullptr;
front->next = nullptr;
}
delete front;
front = fNext; // <20><><EFBFBD><EFBFBD>ͷ<EFBFBD>ڵ<EFBFBD>
// <20><>β<EFBFBD><CEB2><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD>
} else {
val = rear->val; // <20>ݴ<EFBFBD>β<EFBFBD>ڵ<EFBFBD>ֵ
// ɾ<><C9BE>β<EFBFBD>ڵ<EFBFBD>
DoublyListNode *rPrev = rear->prev;
if (rPrev != nullptr) {
rPrev->next = nullptr;
rear->prev = nullptr;
}
delete rear;
rear = rPrev; // <20><><EFBFBD><EFBFBD>β<EFBFBD>ڵ<EFBFBD>
}
queSize--; // <20><><EFBFBD><EFBFBD><C2B6>г<EFBFBD><D0B3><EFBFBD>
return val;
}
/* <20><><EFBFBD>׳<EFBFBD><D7B3><EFBFBD> */
int popFirst() {
return pop(true);
}
/* <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD> */
int popLast() {
return pop(false);
}
/* <20><><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>Ԫ<EFBFBD><D4AA> */
int peekFirst() {
if (isEmpty())
throw out_of_range("˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
return front->val;
}
/* <20><><EFBFBD>ʶ<EFBFBD>βԪ<CEB2><D4AA> */
int peekLast() {
if (isEmpty())
throw out_of_range("˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
return rear->val;
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>ӡ */
vector<int> toVector() {
DoublyListNode *node = front;
vector<int> res(size());
for (int i = 0; i < res.size(); i++) {
res[i] = node->val;
node = node->next;
}
return res;
}
};
/* Driver Code */
int main() {
/* <20><>ʼ<EFBFBD><CABC>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
LinkedListDeque *deque = new LinkedListDeque();
deque->pushLast(3);
deque->pushLast(2);
deque->pushLast(5);
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> deque = ";
printVector(deque->toVector());
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
int peekFirst = deque->peekFirst();
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> peekFirst = " << peekFirst << endl;
int peekLast = deque->peekLast();
cout << "<EFBFBD><EFBFBD>βԪ<EFBFBD><EFBFBD> peekLast = " << peekLast << endl;
/* Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
deque->pushLast(4);
cout << "Ԫ<EFBFBD><EFBFBD> 4 <20><>β<EFBFBD><CEB2><EFBFBD>Ӻ<EFBFBD> deque =";
printVector(deque->toVector());
deque->pushFirst(1);
cout << "Ԫ<EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD> deque = ";
printVector(deque->toVector());
/* Ԫ<>س<EFBFBD><D8B3><EFBFBD> */
int popLast = deque->popLast();
cout << "<EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> = " << popLast << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD> deque = ";
printVector(deque->toVector());
int popFirst = deque->popFirst();
cout << "<EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> = " << popFirst << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD>Ӻ<EFBFBD> deque = ";
printVector(deque->toVector());
/* <20><>ȡ˫<C8A1><CBAB><EFBFBD><EFBFBD><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
int size = deque->size();
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD> size = " << size << endl;
/* <20>ж<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
bool isEmpty = deque->isEmpty();
cout << "˫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD> = " << boolalpha << isEmpty << endl;
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
delete deque;
return 0;
}