8款口碑一键生成论文工具横向实测,本硕博避坑必备指南
2026/9/2 1:16:25
队列(Queue)是一种操作受限的线性表,遵循FIFO(First‑In‑First‑Out,先进先出)原则。它只允许在表的一端(队尾)进行插入操作,在另一端(队头)进行删除操作。
核心操作:
- 入队(Enqueue):在队尾添加新元素。
- 出队(Dequeue):从队头移除元素。
| 操作 | 顺序队列(循环队列) | 链式队列 |
|---|---|---|
| 入队(enqueue) | O(1) | O(1) |
| 出队(dequeue) | O(1) | O(1) |
| 获取队头(peek) | O(1) | O(1) |
| 判空(is_empty) | O(1) | O(1) |
| 遍历(traverse) | O(n) | O(n) |
Queue_t *create_queue(void) { Queue_t *pq = malloc(sizeof(Queue_t)); if(NULL == pq) { printf("malloc error\n"); return NULL; } pq->phead = NULL; pq->ptail = NULL; pq->clen = 0; return pq; } int queue_is_empty(Queue_t *pq) { return NULL == pq->phead; } void destroy_queue(Queue_t *pq) { if(NULL == pq) return; while(!queue_is_empty(pq)) { dequeue(pq); } free(pq); }int enqueue(Queue_t *pq, int data) { if(NULL == pq) return -1; QNode_t *pnew = malloc(sizeof(QNode_t)); if(NULL == pnew) { printf("malloc error\n"); return -1; } pnew->data = data; pnew->pnext = NULL; if(queue_is_empty(pq)) { pq->phead = pnew; pq->ptail = pnew; } else { pq->ptail->pnext = pnew; pq->ptail = pnew; } pq->clen++; return 0; }int dequeue(Queue_t *pq) { if(NULL == pq || queue_is_empty(pq)) { printf("队列空,无法出队\n"); return -1; } QNode_t *pdel = pq->phead; pq->phead = pq->phead->pnext; free(pdel); pq->clen--; //注意:删完变空队列,ptail置NULL,避免野指针 if(queue_is_empty(pq)) { pq->ptail = NULL; } return 0; }int get_front(Queue_t *pq,int *val) { if(NULL == pq || queue_is_empty(pq)) return -1; *val = pq->phead->data; return 0; }void show_queue(Queue_t *pq) { if(queue_is_empty(pq)) { printf("队列为空\n"); return; } QNode_t *p = pq->phead; printf("队列元素:"); while(p != NULL) { printf("%d ",p->data); p = p->pnext; } printf("\n"); }SQue_t *create_seq_queue(void) { SQue_t *psq = malloc(sizeof(SQue_t)); if (NULL == psq) { printf("malloc error\n"); return NULL; } //开辟存储数据的数组空间 psq->pbase = malloc(SEQ_MAX_LEN * sizeof(Data_t)); if (NULL == psq->pbase) { printf("malloc error\n"); free(psq); //分配数组失败,要释放已经申请的SQue_t,防止内存泄漏 return NULL; } psq->head = 0; psq->tail = 0; return psq; } //队列是否满 int is_full_seq_queue(SQue_t *psq) { return (psq->tail + 1) % SEQ_MAX_LEN == psq->head; } //队列是否空 int is_empty_seq_queue(SQue_t *psq) { return psq->head == psq->tail; } void destroy_seq_queue(SQue_t *psq) { if (psq == NULL) return; free(psq->pbase); //先释放数组 free(psq); //再释放管理结构体 }//入队:队尾添加元素 int push_seq_queue(SQue_t *psq, Data_t data) { if (is_full_seq_queue(psq)) { printf("队列已满,无法入队\n"); return -1; } psq->pbase[psq->tail] = data; psq->tail = (psq->tail + 1) % SEQ_MAX_LEN; return 0; } //出队:队头删除元素,pdata接收出队的数据,可以传NULL不接收 int pop_seq_queue(SQue_t *psq, Data_t *pdata) { if (is_empty_seq_queue(psq)) { printf("队列为空,无法出队\n"); return -1; } if (pdata != NULL) { *pdata = psq->pbase[psq->head]; } psq->head = (psq->head + 1) % SEQ_MAX_LEN; return 0; } //获取队头元素,不删除 int get_seq_queue_head(SQue_t *psq, Data_t *pdata) { if (is_empty_seq_queue(psq)) { return -1; } if (pdata != NULL) { *pdata = psq->pbase[psq->head]; return 0; } return -1; }void show_seq_queue(SQue_t *psq) { if(is_empty_seq_queue(psq)) { printf("队列为空\n"); return; } int i; for (i = psq->head; i != psq->tail; i = (i + 1) % SEQ_MAX_LEN) { printf("%d ", psq->pbase[i].num); } printf("\n"); }