121 lines
2.5 KiB
C++
121 lines
2.5 KiB
C++
|
/**
|
|||
|
* File: linkedlist_queue.cpp
|
|||
|
* Created Time: 2022-11-25
|
|||
|
* Author: krahets (krahets@163.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵĶ<D6B5><C4B6><EFBFBD> */
|
|||
|
class LinkedListQueue {
|
|||
|
private:
|
|||
|
ListNode *front, *rear; // ͷ<>ڵ<EFBFBD> front <20><>β<EFBFBD>ڵ<EFBFBD> rear
|
|||
|
int queSize;
|
|||
|
|
|||
|
public:
|
|||
|
LinkedListQueue() {
|
|||
|
front = nullptr;
|
|||
|
rear = nullptr;
|
|||
|
queSize = 0;
|
|||
|
}
|
|||
|
|
|||
|
~LinkedListQueue() {
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ڵ㣬<DAB5>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
freeMemoryLinkedList(front);
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
|
|||
|
int size() {
|
|||
|
return queSize;
|
|||
|
}
|
|||
|
|
|||
|
/* <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool isEmpty() {
|
|||
|
return queSize == 0;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD> */
|
|||
|
void push(int num) {
|
|||
|
// <20><>β<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> num
|
|||
|
ListNode *node = new ListNode(num);
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><D5A3><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>β<EFBFBD>ڵ㶼ָ<E3B6BC><D6B8><EFBFBD>ýڵ<C3BD>
|
|||
|
if (front == nullptr) {
|
|||
|
front = node;
|
|||
|
rear = node;
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><D5A3>ýڵ<C3BD><DAB5><EFBFBD><EFBFBD>ӵ<EFBFBD>β<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>
|
|||
|
else {
|
|||
|
rear->next = node;
|
|||
|
rear = node;
|
|||
|
}
|
|||
|
queSize++;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD> */
|
|||
|
int pop() {
|
|||
|
int num = peek();
|
|||
|
// ɾ<><C9BE>ͷ<EFBFBD>ڵ<EFBFBD>
|
|||
|
ListNode *tmp = front;
|
|||
|
front = front->next;
|
|||
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
delete tmp;
|
|||
|
queSize--;
|
|||
|
return num;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>Ԫ<EFBFBD><D4AA> */
|
|||
|
int peek() {
|
|||
|
if (size() == 0)
|
|||
|
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>");
|
|||
|
return front->val;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ Vector <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
vector<int> toVector() {
|
|||
|
ListNode *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><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
LinkedListQueue *queue = new LinkedListQueue();
|
|||
|
|
|||
|
/* Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
queue->push(1);
|
|||
|
queue->push(3);
|
|||
|
queue->push(2);
|
|||
|
queue->push(5);
|
|||
|
queue->push(4);
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD> queue = ";
|
|||
|
printVector(queue->toVector());
|
|||
|
|
|||
|
/* <20><><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD>Ԫ<EFBFBD><D4AA> */
|
|||
|
int peek = queue->peek();
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> peek = " << peek << endl;
|
|||
|
|
|||
|
/* Ԫ<>س<EFBFBD><D8B3><EFBFBD> */
|
|||
|
peek = queue->pop();
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> pop = " << peek << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD> queue = ";
|
|||
|
printVector(queue->toVector());
|
|||
|
|
|||
|
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD>еij<D0B5><C4B3><EFBFBD> */
|
|||
|
int size = queue->size();
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD> size = " << size << endl;
|
|||
|
|
|||
|
/* <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool empty = queue->isEmpty();
|
|||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD> = " << empty << endl;
|
|||
|
|
|||
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
delete queue;
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|