mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 10:36:29 +08:00
Add toArray() function to array_queue.c and array_deque.c
This commit is contained in:
parent
96b551c4d0
commit
6ed4187f7f
2 changed files with 38 additions and 33 deletions
|
@ -53,7 +53,6 @@ int dequeIndex(ArrayDeque *deque, int i) {
|
|||
return ((i + capacity(deque)) % capacity(deque));
|
||||
}
|
||||
|
||||
|
||||
/* 队首入队 */
|
||||
void pushFirst(ArrayDeque *deque, int num) {
|
||||
if (deque->queSize == capacity(deque)) {
|
||||
|
@ -111,27 +110,30 @@ int popLast(ArrayDeque *deque) {
|
|||
deque->queSize--;
|
||||
return num;
|
||||
}
|
||||
void print_deque(ArrayDeque *deque)
|
||||
{
|
||||
|
||||
for(int i =0;i<deque->queSize-1;i++)
|
||||
{
|
||||
int pptr = (deque->front+i)%deque->queCapacity;
|
||||
printf("%d,",deque->nums[pptr]);
|
||||
/* 返回数组用于打印 */
|
||||
int *toArray(ArrayDeque *deque, int *queSize) {
|
||||
*queSize = deque->queSize;
|
||||
int *res = (int *)calloc(deque->queSize, sizeof(int));
|
||||
int j = deque->front;
|
||||
for (int i = 0; i < deque->queSize; i++) {
|
||||
res[i] = deque->nums[j % deque->queCapacity];
|
||||
j++;
|
||||
}
|
||||
//printf("\n");
|
||||
printf("%d]\n",deque->nums[(deque->front+deque->queSize-1)%deque->queCapacity]);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化队列 */
|
||||
int capacity = 10;
|
||||
int queSize;
|
||||
ArrayDeque *deque = newArrayDeque(capacity);
|
||||
pushLast(deque, 3);
|
||||
pushLast(deque, 2);
|
||||
pushLast(deque, 5);
|
||||
printf("双向队列 deque = [");
|
||||
print_deque(deque);
|
||||
printf("双向队列 deque = ");
|
||||
printArray(toArray(deque, &queSize), queSize);
|
||||
|
||||
/* 访问元素 */
|
||||
int peekFirstNum = peekFirst(deque);
|
||||
|
@ -141,19 +143,19 @@ int main() {
|
|||
|
||||
/* 元素入队 */
|
||||
pushLast(deque, 4);
|
||||
printf("元素 4 队尾入队后 deque = [");
|
||||
print_deque(deque);
|
||||
printf("元素 4 队尾入队后 deque = ");
|
||||
printArray(toArray(deque, &queSize), queSize);
|
||||
pushFirst(deque, 1);
|
||||
printf("元素 1 队首入队后 deque = [");
|
||||
print_deque(deque);
|
||||
printf("元素 1 队首入队后 deque = ");
|
||||
printArray(toArray(deque, &queSize), queSize);
|
||||
|
||||
/* 元素出队 */
|
||||
int popLastNum = popLast(deque);
|
||||
printf("队尾出队元素 = %d ,队尾出队后 deque= [", popLastNum);
|
||||
print_deque(deque);
|
||||
printf("队尾出队元素 = %d ,队尾出队后 deque= ", popLastNum);
|
||||
printArray(toArray(deque, &queSize), queSize);
|
||||
int popFirstNum = popFirst(deque);
|
||||
printf("队首出队元素 = %d ,队首出队后 deque= [", popFirstNum);
|
||||
print_deque(deque);
|
||||
printf("队首出队元素 = %d ,队首出队后 deque= ", popFirstNum);
|
||||
printArray(toArray(deque, &queSize), queSize);
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int dequeSize = size(deque);
|
||||
|
|
|
@ -73,21 +73,24 @@ int pop(ArrayQueue *queue) {
|
|||
queue->queSize--;
|
||||
return num;
|
||||
}
|
||||
void print_queue(ArrayQueue *queue)
|
||||
{
|
||||
|
||||
for(int i =0;i<queue->queSize-1;i++)
|
||||
{
|
||||
int pptr = (queue->front+i)%queue->queCapacity;
|
||||
printf("%d,",queue->nums[pptr]);
|
||||
/* 返回数组用于打印 */
|
||||
int *toArray(ArrayQueue *queue, int *queSize) {
|
||||
*queSize = queue->queSize;
|
||||
int *res = (int *)calloc(queue->queSize, sizeof(int));
|
||||
int j = queue->front;
|
||||
for (int i = 0; i < queue->queSize; i++) {
|
||||
res[i] = queue->nums[j % queue->queCapacity];
|
||||
j++;
|
||||
}
|
||||
//printf("\n");
|
||||
printf("%d]\n",queue->nums[(queue->front+queue->queSize-1)%queue->queCapacity]);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化队列 */
|
||||
int capacity = 10;
|
||||
int queSize;
|
||||
ArrayQueue *queue = newArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
|
@ -97,7 +100,7 @@ int main() {
|
|||
push(queue, 5);
|
||||
push(queue, 4);
|
||||
printf("队列 queue = ");
|
||||
printArray(queue->nums, queue->queSize);
|
||||
printArray(toArray(queue, &queSize), queSize);
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peekNum = peek(queue);
|
||||
|
@ -105,8 +108,8 @@ int main() {
|
|||
|
||||
/* 元素出队 */
|
||||
peekNum = pop(queue);
|
||||
printf("出队元素 pop = %d ,出队后 queue = [", peekNum);
|
||||
print_queue(queue);
|
||||
printf("出队元素 pop = %d ,出队后 queue = ", peekNum);
|
||||
printArray(toArray(queue, &queSize), queSize);
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int queueSize = size(queue);
|
||||
|
@ -121,7 +124,7 @@ int main() {
|
|||
push(queue, i);
|
||||
pop(queue);
|
||||
printf("第 %d 轮入队 + 出队后 queue = ", i);
|
||||
print_queue(queue);
|
||||
printArray(toArray(queue, &queSize), queSize);
|
||||
}
|
||||
|
||||
// 释放内存
|
||||
|
|
Loading…
Reference in a new issue