mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:26:29 +08:00
renfined!
This commit is contained in:
parent
2a9db6d039
commit
96b551c4d0
2 changed files with 38 additions and 17 deletions
|
@ -53,6 +53,7 @@ int dequeIndex(ArrayDeque *deque, int i) {
|
||||||
return ((i + capacity(deque)) % capacity(deque));
|
return ((i + capacity(deque)) % capacity(deque));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* 队首入队 */
|
/* 队首入队 */
|
||||||
void pushFirst(ArrayDeque *deque, int num) {
|
void pushFirst(ArrayDeque *deque, int num) {
|
||||||
if (deque->queSize == capacity(deque)) {
|
if (deque->queSize == capacity(deque)) {
|
||||||
|
@ -110,7 +111,17 @@ int popLast(ArrayDeque *deque) {
|
||||||
deque->queSize--;
|
deque->queSize--;
|
||||||
return num;
|
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]);
|
||||||
|
}
|
||||||
|
//printf("\n");
|
||||||
|
printf("%d]\n",deque->nums[(deque->front+deque->queSize-1)%deque->queCapacity]);
|
||||||
|
}
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
/* 初始化队列 */
|
/* 初始化队列 */
|
||||||
|
@ -119,8 +130,8 @@ int main() {
|
||||||
pushLast(deque, 3);
|
pushLast(deque, 3);
|
||||||
pushLast(deque, 2);
|
pushLast(deque, 2);
|
||||||
pushLast(deque, 5);
|
pushLast(deque, 5);
|
||||||
printf("双向队列 deque = ");
|
printf("双向队列 deque = [");
|
||||||
printArray(deque->nums, deque->queSize);
|
print_deque(deque);
|
||||||
|
|
||||||
/* 访问元素 */
|
/* 访问元素 */
|
||||||
int peekFirstNum = peekFirst(deque);
|
int peekFirstNum = peekFirst(deque);
|
||||||
|
@ -130,19 +141,19 @@ int main() {
|
||||||
|
|
||||||
/* 元素入队 */
|
/* 元素入队 */
|
||||||
pushLast(deque, 4);
|
pushLast(deque, 4);
|
||||||
printf("元素 4 队尾入队后 deque = ");
|
printf("元素 4 队尾入队后 deque = [");
|
||||||
printArray(deque->nums, deque->queSize);
|
print_deque(deque);
|
||||||
pushFirst(deque, 1);
|
pushFirst(deque, 1);
|
||||||
printf("元素 1 队首入队后 deque = ");
|
printf("元素 1 队首入队后 deque = [");
|
||||||
printArray(deque->nums, deque->queSize);
|
print_deque(deque);
|
||||||
|
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
int popLastNum = popLast(deque);
|
int popLastNum = popLast(deque);
|
||||||
printf("队尾出队元素 = %d ,队尾出队后 deque= ", popLastNum);
|
printf("队尾出队元素 = %d ,队尾出队后 deque= [", popLastNum);
|
||||||
printArray(deque->nums, deque->queSize);
|
print_deque(deque);
|
||||||
int popFirstNum = popFirst(deque);
|
int popFirstNum = popFirst(deque);
|
||||||
printf("队首出队元素 = %d ,队首出队后 deque= ", popFirstNum);
|
printf("队首出队元素 = %d ,队首出队后 deque= [", popFirstNum);
|
||||||
printArray(deque->nums, deque->queSize);
|
print_deque(deque);
|
||||||
|
|
||||||
/* 获取队列的长度 */
|
/* 获取队列的长度 */
|
||||||
int dequeSize = size(deque);
|
int dequeSize = size(deque);
|
||||||
|
|
|
@ -73,7 +73,17 @@ int pop(ArrayQueue *queue) {
|
||||||
queue->queSize--;
|
queue->queSize--;
|
||||||
return num;
|
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]);
|
||||||
|
}
|
||||||
|
//printf("\n");
|
||||||
|
printf("%d]\n",queue->nums[(queue->front+queue->queSize-1)%queue->queCapacity]);
|
||||||
|
}
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
/* 初始化队列 */
|
/* 初始化队列 */
|
||||||
|
@ -95,8 +105,8 @@ int main() {
|
||||||
|
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
peekNum = pop(queue);
|
peekNum = pop(queue);
|
||||||
printf("出队元素 pop = %d ,出队后 queue = ", peekNum);
|
printf("出队元素 pop = %d ,出队后 queue = [", peekNum);
|
||||||
printArray(queue->nums, queue->queSize);
|
print_queue(queue);
|
||||||
|
|
||||||
/* 获取队列的长度 */
|
/* 获取队列的长度 */
|
||||||
int queueSize = size(queue);
|
int queueSize = size(queue);
|
||||||
|
@ -111,7 +121,7 @@ int main() {
|
||||||
push(queue, i);
|
push(queue, i);
|
||||||
pop(queue);
|
pop(queue);
|
||||||
printf("第 %d 轮入队 + 出队后 queue = ", i);
|
printf("第 %d 轮入队 + 出队后 queue = ", i);
|
||||||
printArray(queue->nums, queue->queSize);
|
print_queue(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 释放内存
|
// 释放内存
|
||||||
|
|
Loading…
Reference in a new issue