03队列及其操作
03队列及其操作
1、队列结构体
typedef struct Queue { int *data; int head, tail, length, count; } Queue;
2、队列初始化
Queue *init(int n) { Queue *q = (Queue*)malloc(sizeof(Queue)); q->data = (int*)malloc(sizeof(int) * n); q->head = q->tail = q->count = 0; q->length = n; return q; }
3、获取当前队列头的位置
int front(Queue *q) { return q->data[(q->head-1)%q->length]; }
4、判断队列是否为空
int empty(Queue *q) { return q->count == 0; }
5、入队操作
int push(Queue *q, int val) { if (q == NULL) return 0; if (q->count == q->length) { if (!expand(q)) return 0; printf("expand success! Queue->size(%d)\n", q->length); } q->data[q->tail++] = val; if (q->tail == q->length) q->tail = 0; q->count += 1; return 1; }
6、出队操作
int pop(Queue *q) { if (q == NULL) return 0; if (empty(q)) return 0; q->head++; if (q->head == q->length) q->head = 0; q->count -= 1; return 1; }
7、队列扩容
int expand(Queue *q) { int extra_size = q->length; int *p; while (extra_size) { p = (int *)malloc(sizeof(int) * (q->length + extra_size)); if (p) break; // 扩容成功则退出 extra_size >> 2; // 扩容不成功则大小缩小一倍再继续扩容 } if (p == NULL) return 0; // 扩容成功,将原队列的数据拷贝过来 int i = q->head; int j; for (j=0;j<q->count;j++) { p[j] = q->data[(i+j) % q->length]; } free(q->data); q->data = p; q->head = 0; q->tail = q->count; q->length += extra_size; return 1; }
8、输出/打印队列元素
void output(Queue *q) { printf("Queue : ["); int i, j; for (i=q->head,j=0;j<q->count;i++,j++) { j && printf(", "); printf("%d", q->data[i % q->length]); } printf("]\n"); return; }
9、清除队列
void clear(Queue *q) { if (q == NULL) return; free(q->data); free(q); return; }
10、测试及整体代码
#include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct Queue { int *data; int head, tail, length, count; } Queue; Queue *init(int n) { Queue *q = (Queue*)malloc(sizeof(Queue)); q->data = (int*)malloc(sizeof(int) * n); q->head = q->tail = q->count = 0; q->length = n; return q; } int front(Queue *q) { return q->data[(q->head-1)%q->length]; } int empty(Queue *q) { return q->count == 0; } int expand(Queue *q) { int extra_size = q->length; int *p; while (extra_size) { p = (int *)malloc(sizeof(int) * (q->length + extra_size)); if (p) break; // 扩容成功则退出 extra_size >> 2; // 扩容不成功则大小缩小一倍再继续扩容 } if (p == NULL) return 0; // 扩容成功,将原队列的数据拷贝过来 int i = q->head; int j; for (j=0;j<q->count;j++) { p[j] = q->data[(i+j) % q->length]; } free(q->data); q->data = p; q->head = 0; q->tail = q->count; q->length += extra_size; return 1; } int push(Queue *q, int val) { if (q == NULL) return 0; if (q->count == q->length) { if (!expand(q)) return 0; printf("expand success! Queue->size(%d)\n", q->length); } q->data[q->tail++] = val; if (q->tail == q->length) q->tail = 0; q->count += 1; return 1; } int pop(Queue *q) { if (q == NULL) return 0; if (empty(q)) return 0; q->head++; if (q->head == q->length) q->head = 0; q->count -= 1; return 1; } void output(Queue *q) { printf("Queue : ["); int i, j; for (i=q->head,j=0;j<q->count;i++,j++) { j && printf(", "); printf("%d", q->data[i % q->length]); } printf("]\n"); return; } void clear(Queue *q) { if (q == NULL) return; free(q->data); free(q); return; } int main() { srand(time(0)); #define max_op 20 Queue *q = init(max_op); int i; for (i=0;i<max_op*2;i++) { int val = rand() % 100; int op = rand() % 4; switch(op) { case 0: case 1: case 2: { printf("push %d to Queue = %d\n", val, push(q, val)); break; } case 3: { printf("pop %d from the Queue = %d\n", front(q), pop(q)); break; } } output(q); printf("\n"); } #undef max_op clear(q); return 0; }
测试输出:
知行合一,
翻万卷书,游千里路,会百家才