mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Add the initial EN translation for C++ code (#1346)
This commit is contained in:
9
en/codes/cpp/chapter_stack_and_queue/CMakeLists.txt
Normal file
9
en/codes/cpp/chapter_stack_and_queue/CMakeLists.txt
Normal file
@ -0,0 +1,9 @@
|
||||
add_executable(array_deque array_deque.cpp)
|
||||
add_executable(array_queue array_queue.cpp)
|
||||
add_executable(array_stack array_stack.cpp)
|
||||
add_executable(deque deque.cpp)
|
||||
add_executable(linkedlist_deque linkedlist_deque.cpp)
|
||||
add_executable(linkedlist_queue linkedlist_queue.cpp)
|
||||
add_executable(linkedlist_stack linkedlist_stack.cpp)
|
||||
add_executable(queue queue.cpp)
|
||||
add_executable(stack stack.cpp)
|
||||
156
en/codes/cpp/chapter_stack_and_queue/array_deque.cpp
Normal file
156
en/codes/cpp/chapter_stack_and_queue/array_deque.cpp
Normal file
@ -0,0 +1,156 @@
|
||||
/**
|
||||
* File: array_deque.cpp
|
||||
* Created Time: 2023-03-02
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Double-ended queue class based on circular array */
|
||||
class ArrayDeque {
|
||||
private:
|
||||
vector<int> nums; // Array used to store elements of the double-ended queue
|
||||
int front; // Front pointer, pointing to the front element
|
||||
int queSize; // Length of the double-ended queue
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
ArrayDeque(int capacity) {
|
||||
nums.resize(capacity);
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* Get the capacity of the double-ended queue */
|
||||
int capacity() {
|
||||
return nums.size();
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
bool isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
int index(int i) {
|
||||
// Implement circular array by modulo operation
|
||||
// When i exceeds the tail of the array, return to the head
|
||||
// When i exceeds the head of the array, return to the tail
|
||||
return (i + capacity()) % capacity();
|
||||
}
|
||||
|
||||
/* Front enqueue */
|
||||
void pushFirst(int num) {
|
||||
if (queSize == capacity()) {
|
||||
cout << "Double-ended queue is full" << endl;
|
||||
return;
|
||||
}
|
||||
// Move the front pointer one position to the left
|
||||
// Implement front crossing the head of the array to return to the tail by modulo operation
|
||||
front = index(front - 1);
|
||||
// Add num to the front
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Rear enqueue */
|
||||
void pushLast(int num) {
|
||||
if (queSize == capacity()) {
|
||||
cout << "Double-ended queue is full" << endl;
|
||||
return;
|
||||
}
|
||||
// Calculate rear pointer, pointing to rear index + 1
|
||||
int rear = index(front + queSize);
|
||||
// Add num to the rear
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Front dequeue */
|
||||
int popFirst() {
|
||||
int num = peekFirst();
|
||||
// Move front pointer one position backward
|
||||
front = index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Rear dequeue */
|
||||
int popLast() {
|
||||
int num = peekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Access rear element */
|
||||
int peekLast() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
// Calculate rear element index
|
||||
int last = index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
vector<int> toVector() {
|
||||
// Only convert elements within valid length range
|
||||
vector<int> res(queSize);
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[index(j)];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize double-ended queue */
|
||||
ArrayDeque *deque = new ArrayDeque(10);
|
||||
deque->pushLast(3);
|
||||
deque->pushLast(2);
|
||||
deque->pushLast(5);
|
||||
cout << "Double-ended queue deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Access element */
|
||||
int peekFirst = deque->peekFirst();
|
||||
cout << "Front element peekFirst = " << peekFirst << endl;
|
||||
int peekLast = deque->peekLast();
|
||||
cout << "Back element peekLast = " << peekLast << endl;
|
||||
|
||||
/* Element enqueue */
|
||||
deque->pushLast(4);
|
||||
cout << "Element 4 enqueued at the tail, deque = ";
|
||||
printVector(deque->toVector());
|
||||
deque->pushFirst(1);
|
||||
cout << "Element 1 enqueued at the head, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Element dequeue */
|
||||
int popLast = deque->popLast();
|
||||
cout << "Deque tail element = " << popLast << ", after dequeuing from the tail";
|
||||
printVector(deque->toVector());
|
||||
int popFirst = deque->popFirst();
|
||||
cout << "Deque front element = " << popFirst << ", after dequeuing from the front";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque->size();
|
||||
cout << "Length of the double-ended queue size = " << size << endl;
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
bool isEmpty = deque->isEmpty();
|
||||
cout << "Is the double-ended queue empty = " << boolalpha << isEmpty << endl;
|
||||
return 0;
|
||||
}
|
||||
129
en/codes/cpp/chapter_stack_and_queue/array_queue.cpp
Normal file
129
en/codes/cpp/chapter_stack_and_queue/array_queue.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
/**
|
||||
* File: array_queue.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Queue class based on circular array */
|
||||
class ArrayQueue {
|
||||
private:
|
||||
int *nums; // Array for storing queue elements
|
||||
int front; // Front pointer, pointing to the front element
|
||||
int queSize; // Queue length
|
||||
int queCapacity; // Queue capacity
|
||||
|
||||
public:
|
||||
ArrayQueue(int capacity) {
|
||||
// Initialize an array
|
||||
nums = new int[capacity];
|
||||
queCapacity = capacity;
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
~ArrayQueue() {
|
||||
delete[] nums;
|
||||
}
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
int capacity() {
|
||||
return queCapacity;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
void push(int num) {
|
||||
if (queSize == queCapacity) {
|
||||
cout << "Queue is full" << endl;
|
||||
return;
|
||||
}
|
||||
// Calculate rear pointer, pointing to rear index + 1
|
||||
// Use modulo operation to wrap the rear pointer from the end of the array back to the start
|
||||
int rear = (front + queSize) % queCapacity;
|
||||
// Add num to the rear
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
int pop() {
|
||||
int num = peek();
|
||||
// Move front pointer one position backward, returning to the head of the array if it exceeds the tail
|
||||
front = (front + 1) % queCapacity;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
int peek() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Queue is empty");
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Convert array to Vector and return */
|
||||
vector<int> toVector() {
|
||||
// Only convert elements within valid length range
|
||||
vector<int> arr(queSize);
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
arr[i] = nums[j % queCapacity];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize queue */
|
||||
int capacity = 10;
|
||||
ArrayQueue *queue = new ArrayQueue(capacity);
|
||||
|
||||
/* Element enqueue */
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
queue->push(5);
|
||||
queue->push(4);
|
||||
cout << "Queue queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Access front element */
|
||||
int peek = queue->peek();
|
||||
cout << "Front element peek = " << peek << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
peek = queue->pop();
|
||||
cout << "Element dequeued = " << peek << ", after dequeuing";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue->size();
|
||||
cout << "Length of the queue size = " << size << endl;
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
|
||||
/* Test circular array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue->push(i);
|
||||
queue->pop();
|
||||
cout << "After the " << i << "th round of enqueueing + dequeuing, queue = ";
|
||||
printVector(queue->toVector());
|
||||
}
|
||||
|
||||
// Free memory
|
||||
delete queue;
|
||||
|
||||
return 0;
|
||||
}
|
||||
85
en/codes/cpp/chapter_stack_and_queue/array_stack.cpp
Normal file
85
en/codes/cpp/chapter_stack_and_queue/array_stack.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
/**
|
||||
* File: array_stack.cpp
|
||||
* Created Time: 2022-11-28
|
||||
* Author: qualifier1024 (2539244001@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Stack class based on array */
|
||||
class ArrayStack {
|
||||
private:
|
||||
vector<int> stack;
|
||||
|
||||
public:
|
||||
/* Get the length of the stack */
|
||||
int size() {
|
||||
return stack.size();
|
||||
}
|
||||
|
||||
/* Determine if the stack is empty */
|
||||
bool isEmpty() {
|
||||
return stack.size() == 0;
|
||||
}
|
||||
|
||||
/* Push */
|
||||
void push(int num) {
|
||||
stack.push_back(num);
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
int pop() {
|
||||
int num = top();
|
||||
stack.pop_back();
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access stack top element */
|
||||
int top() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Stack is empty");
|
||||
return stack.back();
|
||||
}
|
||||
|
||||
/* Return Vector */
|
||||
vector<int> toVector() {
|
||||
return stack;
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize stack */
|
||||
ArrayStack *stack = new ArrayStack();
|
||||
|
||||
/* Element push */
|
||||
stack->push(1);
|
||||
stack->push(3);
|
||||
stack->push(2);
|
||||
stack->push(5);
|
||||
stack->push(4);
|
||||
cout << "Stack stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Access stack top element */
|
||||
int top = stack->top();
|
||||
cout << "Top element of the stack top = " << top << endl;
|
||||
|
||||
/* Element pop */
|
||||
top = stack->pop();
|
||||
cout << "Element popped from the stack = " << top << ", after popping";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack->size();
|
||||
cout << "Length of the stack size = " << size << endl;
|
||||
|
||||
/* Determine if it's empty */
|
||||
bool empty = stack->isEmpty();
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
|
||||
// Free memory
|
||||
delete stack;
|
||||
|
||||
return 0;
|
||||
}
|
||||
46
en/codes/cpp/chapter_stack_and_queue/deque.cpp
Normal file
46
en/codes/cpp/chapter_stack_and_queue/deque.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* File: deque.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize double-ended queue */
|
||||
deque<int> deque;
|
||||
|
||||
/* Element enqueue */
|
||||
deque.push_back(2);
|
||||
deque.push_back(5);
|
||||
deque.push_back(4);
|
||||
deque.push_front(3);
|
||||
deque.push_front(1);
|
||||
cout << "Double-ended queue deque = ";
|
||||
printDeque(deque);
|
||||
|
||||
/* Access element */
|
||||
int front = deque.front();
|
||||
cout << "Front element of the queue front = " << front << endl;
|
||||
int back = deque.back();
|
||||
cout << "Back element of the queue back = " << back << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
deque.pop_front();
|
||||
cout << "Front element dequeued = " << front << ", after dequeuing from the front";
|
||||
printDeque(deque);
|
||||
deque.pop_back();
|
||||
cout << "Back element dequeued = " << back << ", after dequeuing from the back";
|
||||
printDeque(deque);
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque.size();
|
||||
cout << "Length of the double-ended queue size = " << size << endl;
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
bool empty = deque.empty();
|
||||
cout << "Is the double-ended queue empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
194
en/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp
Normal file
194
en/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp
Normal file
@ -0,0 +1,194 @@
|
||||
/**
|
||||
* File: linkedlist_deque.cpp
|
||||
* Created Time: 2023-03-02
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Double-linked list node */
|
||||
struct DoublyListNode {
|
||||
int val; // Node value
|
||||
DoublyListNode *next; // Pointer to successor node
|
||||
DoublyListNode *prev; // Pointer to predecessor node
|
||||
DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {
|
||||
}
|
||||
};
|
||||
|
||||
/* Double-ended queue class based on double-linked list */
|
||||
class LinkedListDeque {
|
||||
private:
|
||||
DoublyListNode *front, *rear; // Front node front, back node rear
|
||||
int queSize = 0; // Length of the double-ended queue
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
LinkedListDeque() : front(nullptr), rear(nullptr) {
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
~LinkedListDeque() {
|
||||
// Traverse the linked list, remove nodes, free memory
|
||||
DoublyListNode *pre, *cur = front;
|
||||
while (cur != nullptr) {
|
||||
pre = cur;
|
||||
cur = cur->next;
|
||||
delete pre;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Enqueue operation */
|
||||
void push(int num, bool isFront) {
|
||||
DoublyListNode *node = new DoublyListNode(num);
|
||||
// If the list is empty, make front and rear both point to node
|
||||
if (isEmpty())
|
||||
front = rear = node;
|
||||
// Front enqueue operation
|
||||
else if (isFront) {
|
||||
// Add node to the head of the list
|
||||
front->prev = node;
|
||||
node->next = front;
|
||||
front = node; // Update head node
|
||||
// Rear enqueue operation
|
||||
} else {
|
||||
// Add node to the tail of the list
|
||||
rear->next = node;
|
||||
node->prev = rear;
|
||||
rear = node; // Update tail node
|
||||
}
|
||||
queSize++; // Update queue length
|
||||
}
|
||||
|
||||
/* Front enqueue */
|
||||
void pushFirst(int num) {
|
||||
push(num, true);
|
||||
}
|
||||
|
||||
/* Rear enqueue */
|
||||
void pushLast(int num) {
|
||||
push(num, false);
|
||||
}
|
||||
|
||||
/* Dequeue operation */
|
||||
int pop(bool isFront) {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Queue is empty");
|
||||
int val;
|
||||
// Front dequeue operation
|
||||
if (isFront) {
|
||||
val = front->val; // Temporarily store the head node value
|
||||
// Remove head node
|
||||
DoublyListNode *fNext = front->next;
|
||||
if (fNext != nullptr) {
|
||||
fNext->prev = nullptr;
|
||||
front->next = nullptr;
|
||||
}
|
||||
delete front;
|
||||
front = fNext; // Update head node
|
||||
// Rear dequeue operation
|
||||
} else {
|
||||
val = rear->val; // Temporarily store the tail node value
|
||||
// Remove tail node
|
||||
DoublyListNode *rPrev = rear->prev;
|
||||
if (rPrev != nullptr) {
|
||||
rPrev->next = nullptr;
|
||||
rear->prev = nullptr;
|
||||
}
|
||||
delete rear;
|
||||
rear = rPrev; // Update tail node
|
||||
}
|
||||
queSize--; // Update queue length
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Front dequeue */
|
||||
int popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* Rear dequeue */
|
||||
int popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
return front->val;
|
||||
}
|
||||
|
||||
/* Access rear element */
|
||||
int peekLast() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
return rear->val;
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
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() {
|
||||
/* Initialize double-ended queue */
|
||||
LinkedListDeque *deque = new LinkedListDeque();
|
||||
deque->pushLast(3);
|
||||
deque->pushLast(2);
|
||||
deque->pushLast(5);
|
||||
cout << "Double-ended queue deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Access element */
|
||||
int peekFirst = deque->peekFirst();
|
||||
cout << "Front element peekFirst = " << peekFirst << endl;
|
||||
int peekLast = deque->peekLast();
|
||||
cout << "Back element peekLast = " << peekLast << endl;
|
||||
|
||||
/* Element enqueue */
|
||||
deque->pushLast(4);
|
||||
cout << "Element 4 rear enqueued, deque =";
|
||||
printVector(deque->toVector());
|
||||
deque->pushFirst(1);
|
||||
cout << "Element 1 enqueued at the head, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Element dequeue */
|
||||
int popLast = deque->popLast();
|
||||
cout << "Deque tail element = " << popLast << ", after dequeuing from the tail";
|
||||
printVector(deque->toVector());
|
||||
int popFirst = deque->popFirst();
|
||||
cout << "Deque front element = " << popFirst << ", after dequeuing from the front";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque->size();
|
||||
cout << "Length of the double-ended queue size = " << size << endl;
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
bool isEmpty = deque->isEmpty();
|
||||
cout << "Is the double-ended queue empty = " << boolalpha << isEmpty << endl;
|
||||
|
||||
// Free memory
|
||||
delete deque;
|
||||
|
||||
return 0;
|
||||
}
|
||||
120
en/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp
Normal file
120
en/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp
Normal file
@ -0,0 +1,120 @@
|
||||
/**
|
||||
* File: linkedlist_queue.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Queue class based on linked list */
|
||||
class LinkedListQueue {
|
||||
private:
|
||||
ListNode *front, *rear; // Front node front, back node rear
|
||||
int queSize;
|
||||
|
||||
public:
|
||||
LinkedListQueue() {
|
||||
front = nullptr;
|
||||
rear = nullptr;
|
||||
queSize = 0;
|
||||
}
|
||||
|
||||
~LinkedListQueue() {
|
||||
// Traverse the linked list, remove nodes, free memory
|
||||
freeMemoryLinkedList(front);
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
bool isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
void push(int num) {
|
||||
// Add num behind the tail node
|
||||
ListNode *node = new ListNode(num);
|
||||
// If the queue is empty, make the head and tail nodes both point to that node
|
||||
if (front == nullptr) {
|
||||
front = node;
|
||||
rear = node;
|
||||
}
|
||||
// If the queue is not empty, add that node behind the tail node
|
||||
else {
|
||||
rear->next = node;
|
||||
rear = node;
|
||||
}
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
int pop() {
|
||||
int num = peek();
|
||||
// Remove head node
|
||||
ListNode *tmp = front;
|
||||
front = front->next;
|
||||
// Free memory
|
||||
delete tmp;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
int peek() {
|
||||
if (size() == 0)
|
||||
throw out_of_range("Queue is empty");
|
||||
return front->val;
|
||||
}
|
||||
|
||||
/* Convert the linked list to Vector and return */
|
||||
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() {
|
||||
/* Initialize queue */
|
||||
LinkedListQueue *queue = new LinkedListQueue();
|
||||
|
||||
/* Element enqueue */
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
queue->push(5);
|
||||
queue->push(4);
|
||||
cout << "Queue queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Access front element */
|
||||
int peek = queue->peek();
|
||||
cout << "Front element peek = " << peek << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
peek = queue->pop();
|
||||
cout << "Element dequeued = " << peek << ", after dequeuing";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue->size();
|
||||
cout << "Length of the queue size = " << size << endl;
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
|
||||
// Free memory
|
||||
delete queue;
|
||||
|
||||
return 0;
|
||||
}
|
||||
109
en/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp
Normal file
109
en/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp
Normal file
@ -0,0 +1,109 @@
|
||||
/**
|
||||
* File: linkedlist_stack.cpp
|
||||
* Created Time: 2022-11-28
|
||||
* Author: qualifier1024 (2539244001@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Stack class based on linked list */
|
||||
class LinkedListStack {
|
||||
private:
|
||||
ListNode *stackTop; // Use the head node as the top of the stack
|
||||
int stkSize; // Length of the stack
|
||||
|
||||
public:
|
||||
LinkedListStack() {
|
||||
stackTop = nullptr;
|
||||
stkSize = 0;
|
||||
}
|
||||
|
||||
~LinkedListStack() {
|
||||
// Traverse the linked list, remove nodes, free memory
|
||||
freeMemoryLinkedList(stackTop);
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size() {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* Determine if the stack is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Push */
|
||||
void push(int num) {
|
||||
ListNode *node = new ListNode(num);
|
||||
node->next = stackTop;
|
||||
stackTop = node;
|
||||
stkSize++;
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
int pop() {
|
||||
int num = top();
|
||||
ListNode *tmp = stackTop;
|
||||
stackTop = stackTop->next;
|
||||
// Free memory
|
||||
delete tmp;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access stack top element */
|
||||
int top() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Stack is empty");
|
||||
return stackTop->val;
|
||||
}
|
||||
|
||||
/* Convert the List to Array and return */
|
||||
vector<int> toVector() {
|
||||
ListNode *node = stackTop;
|
||||
vector<int> res(size());
|
||||
for (int i = res.size() - 1; i >= 0; i--) {
|
||||
res[i] = node->val;
|
||||
node = node->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize stack */
|
||||
LinkedListStack *stack = new LinkedListStack();
|
||||
|
||||
/* Element push */
|
||||
stack->push(1);
|
||||
stack->push(3);
|
||||
stack->push(2);
|
||||
stack->push(5);
|
||||
stack->push(4);
|
||||
cout << "Stack stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Access stack top element */
|
||||
int top = stack->top();
|
||||
cout << "Top element of the stack top = " << top << endl;
|
||||
|
||||
/* Element pop */
|
||||
top = stack->pop();
|
||||
cout << "Element popped from the stack = " << top << ", after popping";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack->size();
|
||||
cout << "Length of the stack size = " << size << endl;
|
||||
|
||||
/* Determine if it's empty */
|
||||
bool empty = stack->isEmpty();
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
|
||||
// Free memory
|
||||
delete stack;
|
||||
|
||||
return 0;
|
||||
}
|
||||
41
en/codes/cpp/chapter_stack_and_queue/queue.cpp
Normal file
41
en/codes/cpp/chapter_stack_and_queue/queue.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* File: queue.cpp
|
||||
* Created Time: 2022-11-28
|
||||
* Author: qualifier1024 (2539244001@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize queue */
|
||||
queue<int> queue;
|
||||
|
||||
/* Element enqueue */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
cout << "Queue queue = ";
|
||||
printQueue(queue);
|
||||
|
||||
/* Access front element */
|
||||
int front = queue.front();
|
||||
cout << "Front element of the queue front = " << front << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
queue.pop();
|
||||
cout << "Element dequeued = " << front << ", after dequeuing";
|
||||
printQueue(queue);
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue.size();
|
||||
cout << "Length of the queue size = " << size << endl;
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
bool empty = queue.empty();
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
41
en/codes/cpp/chapter_stack_and_queue/stack.cpp
Normal file
41
en/codes/cpp/chapter_stack_and_queue/stack.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* File: stack.cpp
|
||||
* Created Time: 2022-11-28
|
||||
* Author: qualifier1024 (2539244001@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize stack */
|
||||
stack<int> stack;
|
||||
|
||||
/* Element push */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
cout << "Stack stack = ";
|
||||
printStack(stack);
|
||||
|
||||
/* Access stack top element */
|
||||
int top = stack.top();
|
||||
cout << "Top element of the stack top = " << top << endl;
|
||||
|
||||
/* Element pop */
|
||||
stack.pop(); // No return value
|
||||
cout << "Element popped from the stack = " << top << ", after popping";
|
||||
printStack(stack);
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack.size();
|
||||
cout << "Length of the stack size = " << size << endl;
|
||||
|
||||
/* Determine if it's empty */
|
||||
bool empty = stack.empty();
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user