mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-26 22:42:41 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -6,18 +6,18 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
/* Deque implemented with a circular array */
|
||||
typedef struct {
|
||||
int *nums; // Array for storing queue elements
|
||||
int front; // Front pointer, points to the front of the queue element
|
||||
int queSize; // Rear pointer, points to rear + 1
|
||||
int *nums; // Array that stores the queue elements
|
||||
int front; // Front pointer, pointing to the first element
|
||||
int queSize; // Current number of elements in the deque
|
||||
int queCapacity; // Queue capacity
|
||||
} ArrayDeque;
|
||||
|
||||
/* Constructor */
|
||||
ArrayDeque *newArrayDeque(int capacity) {
|
||||
ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));
|
||||
// Initialize array
|
||||
// Initialize the array
|
||||
deque->queCapacity = capacity;
|
||||
deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);
|
||||
deque->front = deque->queSize = 0;
|
||||
@@ -30,88 +30,88 @@ void delArrayDeque(ArrayDeque *deque) {
|
||||
free(deque);
|
||||
}
|
||||
|
||||
/* Get the capacity of the double-ended queue */
|
||||
/* Get the deque capacity */
|
||||
int capacity(ArrayDeque *deque) {
|
||||
return deque->queCapacity;
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size(ArrayDeque *deque) {
|
||||
return deque->queSize;
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool empty(ArrayDeque *deque) {
|
||||
return deque->queSize == 0;
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
/* Calculate an index in the circular array */
|
||||
int dequeIndex(ArrayDeque *deque, int i) {
|
||||
// Use modulo operation to wrap the array head and tail together
|
||||
// When i exceeds array end, wrap to head
|
||||
// When i passes the head of the array, return to the tail
|
||||
// Use modulo to connect the end of the array to its beginning
|
||||
// When i passes the end of the array, wrap to the beginning
|
||||
// When i passes the beginning of the array, wrap around to the end
|
||||
return ((i + capacity(deque)) % capacity(deque));
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
/* Enqueue at the front */
|
||||
void pushFirst(ArrayDeque *deque, int num) {
|
||||
if (deque->queSize == capacity(deque)) {
|
||||
printf("Deque is full\r\n");
|
||||
return;
|
||||
}
|
||||
// Use modulo operation to wrap front around to the tail after passing the head of the array
|
||||
// Use modulo to wrap front from array head to rear
|
||||
// Move the front pointer one position to the left
|
||||
// Use modulo to wrap front to the end when it passes the beginning
|
||||
deque->front = dequeIndex(deque, deque->front - 1);
|
||||
// Add num to queue front
|
||||
// Add num to the front
|
||||
deque->nums[deque->front] = num;
|
||||
deque->queSize++;
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
/* Enqueue at the rear */
|
||||
void pushLast(ArrayDeque *deque, int num) {
|
||||
if (deque->queSize == capacity(deque)) {
|
||||
printf("Deque is full\r\n");
|
||||
return;
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
// Calculate the rear pointer, which points one position past the last element
|
||||
int rear = dequeIndex(deque, deque->front + deque->queSize);
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
deque->nums[rear] = num;
|
||||
deque->queSize++;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peekFirst(ArrayDeque *deque) {
|
||||
// Access error: Deque is empty
|
||||
// Access error: the deque is empty
|
||||
assert(empty(deque) == 0);
|
||||
return deque->nums[deque->front];
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
int peekLast(ArrayDeque *deque) {
|
||||
// Access error: Deque is empty
|
||||
// Access error: the deque is empty
|
||||
assert(empty(deque) == 0);
|
||||
int last = dequeIndex(deque, deque->front + deque->queSize - 1);
|
||||
return deque->nums[last];
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
/* Dequeue from the front */
|
||||
int popFirst(ArrayDeque *deque) {
|
||||
int num = peekFirst(deque);
|
||||
// Move front pointer backward by one position
|
||||
// Advance the front pointer by one position
|
||||
deque->front = dequeIndex(deque, deque->front + 1);
|
||||
deque->queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
/* Dequeue from the rear */
|
||||
int popLast(ArrayDeque *deque) {
|
||||
int num = peekLast(deque);
|
||||
deque->queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
/* Return an array for printing */
|
||||
int *toArray(ArrayDeque *deque, int *queSize) {
|
||||
*queSize = deque->queSize;
|
||||
int *res = (int *)calloc(deque->queSize, sizeof(int));
|
||||
@@ -123,47 +123,47 @@ int *toArray(ArrayDeque *deque, int *queSize) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
int capacity = 10;
|
||||
int queSize;
|
||||
ArrayDeque *deque = newArrayDeque(capacity);
|
||||
pushLast(deque, 3);
|
||||
pushLast(deque, 2);
|
||||
pushLast(deque, 5);
|
||||
printf("Double-ended queue deque = ");
|
||||
printf("Deque deque = ");
|
||||
printArray(toArray(deque, &queSize), queSize);
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
int peekFirstNum = peekFirst(deque);
|
||||
printf("Front element peekFirst = %d\r\n", peekFirstNum);
|
||||
int peekLastNum = peekLast(deque);
|
||||
printf("Rear element peekLast = %d\r\n", peekLastNum);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
pushLast(deque, 4);
|
||||
printf("After element 4 enqueues at rear, deque = ");
|
||||
printf("After enqueuing element 4 at the rear, deque = ");
|
||||
printArray(toArray(deque, &queSize), queSize);
|
||||
pushFirst(deque, 1);
|
||||
printf("After element 1 enqueues at front, deque = ");
|
||||
printf("After enqueuing element 1 at the front, deque = ");
|
||||
printArray(toArray(deque, &queSize), queSize);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int popLastNum = popLast(deque);
|
||||
printf("Dequeue from rear = %d, deque after rear dequeue = ", popLastNum);
|
||||
printf("Element removed from the rear = %d; after removal, deque= ", popLastNum);
|
||||
printArray(toArray(deque, &queSize), queSize);
|
||||
int popFirstNum = popFirst(deque);
|
||||
printf("Dequeue from front = %d, deque after front dequeue = ", popFirstNum);
|
||||
printf("Element removed from the front = %d; after removal, deque= ", popFirstNum);
|
||||
printArray(toArray(deque, &queSize), queSize);
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int dequeSize = size(deque);
|
||||
printf("Deque size = %d\r\n", dequeSize);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool isEmpty = empty(deque);
|
||||
printf("Is queue empty = %s\r\n", isEmpty ? "true" : "false");
|
||||
printf("Is the queue empty = %s\r\n", isEmpty ? "true" : "false");
|
||||
|
||||
// Free memory
|
||||
delArrayDeque(deque);
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
/* Queue implemented with a circular array */
|
||||
typedef struct {
|
||||
int *nums; // Array for storing queue elements
|
||||
int front; // Front pointer, points to the front of the queue element
|
||||
int *nums; // Array that stores the queue elements
|
||||
int front; // Front pointer, pointing to the first element
|
||||
int queSize; // Current number of elements in the queue
|
||||
int queCapacity; // Queue capacity
|
||||
} ArrayQueue;
|
||||
@@ -17,7 +17,7 @@ typedef struct {
|
||||
/* Constructor */
|
||||
ArrayQueue *newArrayQueue(int capacity) {
|
||||
ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));
|
||||
// Initialize array
|
||||
// Initialize the array
|
||||
queue->queCapacity = capacity;
|
||||
queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);
|
||||
queue->front = queue->queSize = 0;
|
||||
@@ -30,22 +30,22 @@ void delArrayQueue(ArrayQueue *queue) {
|
||||
free(queue);
|
||||
}
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
/* Get the queue capacity */
|
||||
int capacity(ArrayQueue *queue) {
|
||||
return queue->queCapacity;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size(ArrayQueue *queue) {
|
||||
return queue->queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool empty(ArrayQueue *queue) {
|
||||
return queue->queSize == 0;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek(ArrayQueue *queue) {
|
||||
assert(size(queue) != 0);
|
||||
return queue->nums[queue->front];
|
||||
@@ -57,10 +57,10 @@ void push(ArrayQueue *queue, int num) {
|
||||
printf("Queue is full\r\n");
|
||||
return;
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
// Add num to the rear of the queue
|
||||
// Calculate the rear pointer, which points one position past the last element
|
||||
// Use modulo to wrap rear to the beginning when it passes the end of the array
|
||||
int rear = (queue->front + queue->queSize) % queue->queCapacity;
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
queue->nums[rear] = num;
|
||||
queue->queSize++;
|
||||
}
|
||||
@@ -68,13 +68,13 @@ void push(ArrayQueue *queue, int num) {
|
||||
/* Dequeue */
|
||||
int pop(ArrayQueue *queue) {
|
||||
int num = peek(queue);
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
// Advance the front pointer by one position, wrapping to the beginning if it passes the end
|
||||
queue->front = (queue->front + 1) % queue->queCapacity;
|
||||
queue->queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
/* Return an array for printing */
|
||||
int *toArray(ArrayQueue *queue, int *queSize) {
|
||||
*queSize = queue->queSize;
|
||||
int *res = (int *)calloc(queue->queSize, sizeof(int));
|
||||
@@ -86,14 +86,14 @@ int *toArray(ArrayQueue *queue, int *queSize) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
int capacity = 10;
|
||||
int queSize;
|
||||
ArrayQueue *queue = newArrayQueue(capacity);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
push(queue, 1);
|
||||
push(queue, 3);
|
||||
push(queue, 2);
|
||||
@@ -102,28 +102,28 @@ int main() {
|
||||
printf("Queue queue = ");
|
||||
printArray(toArray(queue, &queSize), queSize);
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peekNum = peek(queue);
|
||||
printf("Front element peek = %d\r\n", peekNum);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
peekNum = pop(queue);
|
||||
printf("Dequeue element pop = %d, queue after dequeue = ", peekNum);
|
||||
printf("Dequeued element pop = %d; after dequeuing, queue = ", peekNum);
|
||||
printArray(toArray(queue, &queSize), queSize);
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int queueSize = size(queue);
|
||||
printf("Queue size = %d\r\n", queueSize);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool isEmpty = empty(queue);
|
||||
printf("Is queue empty = %s\r\n", isEmpty ? "true" : "false");
|
||||
printf("Is the queue empty = %s\r\n", isEmpty ? "true" : "false");
|
||||
|
||||
/* Test circular array */
|
||||
/* Test the circular array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
push(queue, i);
|
||||
pop(queue);
|
||||
printf("After round %d enqueue + dequeue, queue = ", i);
|
||||
printf("After enqueueing and dequeueing in round %d, queue = ", i);
|
||||
printArray(toArray(queue, &queSize), queSize);
|
||||
}
|
||||
|
||||
@@ -131,4 +131,4 @@ int main() {
|
||||
delArrayQueue(queue);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#define MAX_SIZE 5000
|
||||
|
||||
/* Stack based on array implementation */
|
||||
/* Stack implemented with an array */
|
||||
typedef struct {
|
||||
int *data;
|
||||
int size;
|
||||
@@ -17,7 +17,7 @@ typedef struct {
|
||||
/* Constructor */
|
||||
ArrayStack *newArrayStack() {
|
||||
ArrayStack *stack = malloc(sizeof(ArrayStack));
|
||||
// Initialize with large capacity to avoid expansion
|
||||
// Initialize with a large capacity to avoid resizing
|
||||
stack->data = malloc(sizeof(int) * MAX_SIZE);
|
||||
stack->size = 0;
|
||||
return stack;
|
||||
@@ -29,12 +29,12 @@ void delArrayStack(ArrayStack *stack) {
|
||||
free(stack);
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size(ArrayStack *stack) {
|
||||
return stack->size;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
bool isEmpty(ArrayStack *stack) {
|
||||
return stack->size == 0;
|
||||
}
|
||||
@@ -49,7 +49,7 @@ void push(ArrayStack *stack, int num) {
|
||||
stack->size++;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int peek(ArrayStack *stack) {
|
||||
if (stack->size == 0) {
|
||||
printf("Stack is empty\n");
|
||||
@@ -65,12 +65,12 @@ int pop(ArrayStack *stack) {
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
ArrayStack *stack = newArrayStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
push(stack, 1);
|
||||
push(stack, 3);
|
||||
push(stack, 2);
|
||||
@@ -79,22 +79,22 @@ int main() {
|
||||
printf("Stack stack = ");
|
||||
printArray(stack->data, stack->size);
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int val = peek(stack);
|
||||
printf("Top element top = %d\n", val);
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
val = pop(stack);
|
||||
printf("Pop element pop = %d, stack after pop = ", val);
|
||||
printf("Popped element pop = %d; after popping, stack = ", val);
|
||||
printArray(stack->data, stack->size);
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack->size;
|
||||
printf("Stack size = %d\n", size);
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
bool empty = isEmpty(stack);
|
||||
printf("Is stack empty = %s\n", empty ? "true" : "false");
|
||||
printf("Is the stack empty = %s\n", empty ? "true" : "false");
|
||||
|
||||
// Free memory
|
||||
delArrayStack(stack);
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
/* Doubly linked list node */
|
||||
typedef struct DoublyListNode {
|
||||
int val; // Node value
|
||||
struct DoublyListNode *next; // Successor node
|
||||
struct DoublyListNode *prev; // Predecessor node
|
||||
struct DoublyListNode *next; // Next node
|
||||
struct DoublyListNode *prev; // Previous node
|
||||
} DoublyListNode;
|
||||
|
||||
/* Constructor */
|
||||
@@ -27,10 +27,10 @@ void delDoublyListNode(DoublyListNode *node) {
|
||||
free(node);
|
||||
}
|
||||
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
/* Deque implemented with a doubly linked list */
|
||||
typedef struct {
|
||||
DoublyListNode *front, *rear; // Head node front, tail node rear
|
||||
int queSize; // Length of the double-ended queue
|
||||
DoublyListNode *front, *rear; // Head node front and tail node rear
|
||||
int queSize; // Deque size
|
||||
} LinkedListDeque;
|
||||
|
||||
/* Constructor */
|
||||
@@ -50,16 +50,16 @@ void delLinkedListdeque(LinkedListDeque *deque) {
|
||||
deque->front = deque->front->next;
|
||||
free(tmp);
|
||||
}
|
||||
// Free deque structure
|
||||
// Free the deque structure
|
||||
free(deque);
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size(LinkedListDeque *deque) {
|
||||
return deque->queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool empty(LinkedListDeque *deque) {
|
||||
return (size(deque) == 0);
|
||||
}
|
||||
@@ -67,44 +67,44 @@ bool empty(LinkedListDeque *deque) {
|
||||
/* Enqueue */
|
||||
void push(LinkedListDeque *deque, int num, bool isFront) {
|
||||
DoublyListNode *node = newDoublyListNode(num);
|
||||
// If list is empty, set both front and rear to node
|
||||
// If the list is empty, make both front and rear point to node
|
||||
if (empty(deque)) {
|
||||
deque->front = deque->rear = node;
|
||||
}
|
||||
// Front of the queue enqueue operation
|
||||
// Enqueue at the front
|
||||
else if (isFront) {
|
||||
// Add node to the head of the linked list
|
||||
deque->front->prev = node;
|
||||
node->next = deque->front;
|
||||
deque->front = node; // Update head node
|
||||
deque->front = node; // Update the head node
|
||||
}
|
||||
// Rear of the queue enqueue operation
|
||||
// Enqueue at the rear
|
||||
else {
|
||||
// Add node to the tail of the linked list
|
||||
deque->rear->next = node;
|
||||
node->prev = deque->rear;
|
||||
deque->rear = node;
|
||||
}
|
||||
deque->queSize++; // Update queue length
|
||||
deque->queSize++; // Update the queue size
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
/* Enqueue at the front */
|
||||
void pushFirst(LinkedListDeque *deque, int num) {
|
||||
push(deque, num, true);
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
/* Enqueue at the rear */
|
||||
void pushLast(LinkedListDeque *deque, int num) {
|
||||
push(deque, num, false);
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peekFirst(LinkedListDeque *deque) {
|
||||
assert(size(deque) && deque->front);
|
||||
return deque->front->val;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
int peekLast(LinkedListDeque *deque) {
|
||||
assert(size(deque) && deque->rear);
|
||||
return deque->rear->val;
|
||||
@@ -115,49 +115,49 @@ int pop(LinkedListDeque *deque, bool isFront) {
|
||||
if (empty(deque))
|
||||
return -1;
|
||||
int val;
|
||||
// When the deque has one element, front and rear point to the same node
|
||||
// When the queue size is 1, the head and tail point to the same node
|
||||
if (deque->queSize == 1) {
|
||||
val = deque->front->val;
|
||||
delDoublyListNode(deque->front); // Free the shared node once
|
||||
delDoublyListNode(deque->front); // Both point to the same node, so free it only once
|
||||
deque->front = NULL;
|
||||
deque->rear = NULL;
|
||||
}
|
||||
// Temporarily store head node value
|
||||
// Dequeue from the front
|
||||
else if (isFront) {
|
||||
val = peekFirst(deque); // Delete head node
|
||||
val = peekFirst(deque); // Save the head node value
|
||||
DoublyListNode *fNext = deque->front->next;
|
||||
fNext->prev = NULL;
|
||||
deque->front->next = NULL;
|
||||
delDoublyListNode(deque->front);
|
||||
deque->front = fNext; // Update head node
|
||||
deque->front = fNext; // Update the head node
|
||||
}
|
||||
// Temporarily store tail node value
|
||||
// Dequeue from the rear
|
||||
else {
|
||||
val = peekLast(deque); // Delete tail node
|
||||
val = peekLast(deque); // Save the tail node value
|
||||
DoublyListNode *rPrev = deque->rear->prev;
|
||||
rPrev->next = NULL;
|
||||
deque->rear->prev = NULL;
|
||||
delDoublyListNode(deque->rear);
|
||||
deque->rear = rPrev; // Update tail node
|
||||
deque->rear = rPrev; // Update the tail node
|
||||
}
|
||||
deque->queSize--; // Update queue length
|
||||
deque->queSize--; // Update the queue size
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
/* Dequeue from the front */
|
||||
int popFirst(LinkedListDeque *deque) {
|
||||
return pop(deque, true);
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
/* Dequeue from the rear */
|
||||
int popLast(LinkedListDeque *deque) {
|
||||
return pop(deque, false);
|
||||
}
|
||||
|
||||
/* Print queue */
|
||||
/* Print the queue */
|
||||
void printLinkedListDeque(LinkedListDeque *deque) {
|
||||
int *arr = malloc(sizeof(int) * deque->queSize);
|
||||
// Copy data from list to array
|
||||
// Copy the linked list data into an array
|
||||
int i;
|
||||
DoublyListNode *node;
|
||||
for (i = 0, node = deque->front; i < deque->queSize; i++) {
|
||||
@@ -168,45 +168,45 @@ void printLinkedListDeque(LinkedListDeque *deque) {
|
||||
free(arr);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
LinkedListDeque *deque = newLinkedListDeque();
|
||||
pushLast(deque, 3);
|
||||
pushLast(deque, 2);
|
||||
pushLast(deque, 5);
|
||||
printf("Double-ended queue deque = ");
|
||||
printf("Deque deque = ");
|
||||
printLinkedListDeque(deque);
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
int peekFirstNum = peekFirst(deque);
|
||||
printf("Front element peekFirst = %d\r\n", peekFirstNum);
|
||||
int peekLastNum = peekLast(deque);
|
||||
printf("Front element peekLast = %d\r\n", peekLastNum);
|
||||
printf("Rear element peekLast = %d\r\n", peekLastNum);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
pushLast(deque, 4);
|
||||
printf("After element 4 enqueues at back, deque =");
|
||||
printf("After enqueuing element 4 at the rear, deque =");
|
||||
printLinkedListDeque(deque);
|
||||
pushFirst(deque, 1);
|
||||
printf("After element 1 enqueues at front, deque =");
|
||||
printf("After enqueuing element 1 at the front, deque =");
|
||||
printLinkedListDeque(deque);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int popLastNum = popLast(deque);
|
||||
printf("Dequeue from rear popLast = %d, deque after rear dequeue = ", popLastNum);
|
||||
printf("Element removed from the rear, popLast = %d; after removal, deque = ", popLastNum);
|
||||
printLinkedListDeque(deque);
|
||||
int popFirstNum = popFirst(deque);
|
||||
printf("Dequeue from front popFirst = %d, deque after front dequeue = ", popFirstNum);
|
||||
printf("Element removed from the front, popFirst = %d; after removal, deque = ", popFirstNum);
|
||||
printLinkedListDeque(deque);
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int dequeSize = size(deque);
|
||||
printf("Deque size = %d\r\n", dequeSize);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool isEmpty = empty(deque);
|
||||
printf("Is deque empty = %s\r\n", isEmpty ? "true" : "false");
|
||||
printf("Is the deque empty = %s\r\n", isEmpty ? "true" : "false");
|
||||
|
||||
// Free memory
|
||||
delLinkedListdeque(deque);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
/* Queue implemented with a linked list */
|
||||
typedef struct {
|
||||
ListNode *front, *rear;
|
||||
int queSize;
|
||||
@@ -29,30 +29,30 @@ void delLinkedListQueue(LinkedListQueue *queue) {
|
||||
queue->front = queue->front->next;
|
||||
free(tmp);
|
||||
}
|
||||
// Free queue structure
|
||||
// Free the queue structure
|
||||
free(queue);
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size(LinkedListQueue *queue) {
|
||||
return queue->queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool empty(LinkedListQueue *queue) {
|
||||
return (size(queue) == 0);
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
void push(LinkedListQueue *queue, int num) {
|
||||
// Add node at tail
|
||||
// Add node at the tail
|
||||
ListNode *node = newListNode(num);
|
||||
// If the queue is empty, make both front and rear point to the node
|
||||
// If the queue is empty, make both the head and tail point to this node
|
||||
if (queue->front == NULL) {
|
||||
queue->front = node;
|
||||
queue->rear = node;
|
||||
}
|
||||
// If the queue is not empty, add the node after the tail node
|
||||
// If the queue is not empty, add this node after the tail node
|
||||
else {
|
||||
queue->rear->next = node;
|
||||
queue->rear = node;
|
||||
@@ -60,7 +60,7 @@ void push(LinkedListQueue *queue, int num) {
|
||||
queue->queSize++;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek(LinkedListQueue *queue) {
|
||||
assert(size(queue) && queue->front);
|
||||
return queue->front->val;
|
||||
@@ -76,10 +76,10 @@ int pop(LinkedListQueue *queue) {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Print queue */
|
||||
/* Print the queue */
|
||||
void printLinkedListQueue(LinkedListQueue *queue) {
|
||||
int *arr = malloc(sizeof(int) * queue->queSize);
|
||||
// Copy data from list to array
|
||||
// Copy the linked list data into an array
|
||||
int i;
|
||||
ListNode *node;
|
||||
for (i = 0, node = queue->front; i < queue->queSize; i++) {
|
||||
@@ -90,12 +90,12 @@ void printLinkedListQueue(LinkedListQueue *queue) {
|
||||
free(arr);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
LinkedListQueue *queue = newLinkedListQueue();
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
push(queue, 1);
|
||||
push(queue, 3);
|
||||
push(queue, 2);
|
||||
@@ -104,22 +104,22 @@ int main() {
|
||||
printf("Queue queue = ");
|
||||
printLinkedListQueue(queue);
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peekNum = peek(queue);
|
||||
printf("Front element peek = %d\r\n", peekNum);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
peekNum = pop(queue);
|
||||
printf("Dequeue element pop = %d, queue after dequeue = ", peekNum);
|
||||
printf("Dequeued element pop = %d; after dequeuing, queue = ", peekNum);
|
||||
printLinkedListQueue(queue);
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int queueSize = size(queue);
|
||||
printf("Queue size = %d\r\n", queueSize);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool isEmpty = empty(queue);
|
||||
printf("Is queue empty = %s\r\n", isEmpty ? "true" : "false");
|
||||
printf("Is the queue empty = %s\r\n", isEmpty ? "true" : "false");
|
||||
|
||||
// Free memory
|
||||
delLinkedListQueue(queue);
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* Stack based on linked list implementation */
|
||||
/* Stack implemented with a linked list */
|
||||
typedef struct {
|
||||
ListNode *top; // Use head node as stack top
|
||||
int size; // Stack length
|
||||
ListNode *top; // Use the head node as the stack top
|
||||
int size; // Stack size
|
||||
} LinkedListStack;
|
||||
|
||||
/* Constructor */
|
||||
@@ -30,12 +30,12 @@ void delLinkedListStack(LinkedListStack *s) {
|
||||
free(s);
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size(LinkedListStack *s) {
|
||||
return s->size;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
bool isEmpty(LinkedListStack *s) {
|
||||
return size(s) == 0;
|
||||
}
|
||||
@@ -43,13 +43,13 @@ bool isEmpty(LinkedListStack *s) {
|
||||
/* Push */
|
||||
void push(LinkedListStack *s, int num) {
|
||||
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
|
||||
node->next = s->top; // Update new node's pointer field
|
||||
node->val = num; // Update new node's data field
|
||||
s->top = node; // Update stack top
|
||||
s->size++; // Update stack size
|
||||
node->next = s->top; // Set the new node's pointer field
|
||||
node->val = num; // Set the new node's data field
|
||||
s->top = node; // Update the stack top
|
||||
s->size++; // Update the stack size
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int peek(LinkedListStack *s) {
|
||||
if (s->size == 0) {
|
||||
printf("Stack is empty\n");
|
||||
@@ -69,12 +69,12 @@ int pop(LinkedListStack *s) {
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
LinkedListStack *stack = newLinkedListStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
push(stack, 1);
|
||||
push(stack, 3);
|
||||
push(stack, 2);
|
||||
@@ -84,21 +84,21 @@ int main() {
|
||||
printf("Stack stack = ");
|
||||
printLinkedList(stack->top);
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int val = peek(stack);
|
||||
printf("Top element top = %d\r\n", val);
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
val = pop(stack);
|
||||
printf("Pop element pop = %d, stack after pop = ", val);
|
||||
printf("Popped element pop = %d; after popping, stack = ", val);
|
||||
printLinkedList(stack->top);
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
printf("Stack size = %d\n", size(stack));
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
bool empty = isEmpty(stack);
|
||||
printf("Is stack empty = %s\n", empty ? "true" : "false");
|
||||
printf("Is the stack empty = %s\n", empty ? "true" : "false");
|
||||
|
||||
// Free memory
|
||||
delLinkedListStack(stack);
|
||||
|
||||
Reference in New Issue
Block a user