将队列(二)改写为长尾词,如何表达?

2026-04-12 04:061阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计612个文字,预计阅读时间需要3分钟。

将队列(二)改写为长尾词,如何表达?

3. 入队函数 +void QueuePush(Queue *pq, QDataType x) // 入队{ assert(pq); QNode *newnode=(QNode *)malloc(sizeof(QNode)); // 创建节点 if (newnode==NULL) { perror(malloc); exit(-1); } // 节点赋值 newnode->val=x; newnode->next=NULL; // 当队列为空时,新节点即为头节点 if (pq->head==NULL) { pq->head=newnode; pq->tail=newnode; } else { // 否则,将新节点添加到队尾 pq->tail->next=newnode; pq->tail=newnode; }}

3.入队

void QueuePush(Queue* pq, QDataType x)//入队 { assert(pq); QNode* newnode = (QNode*)malloc(sizeof(QNode)); //创建节点 if (newnode == NULL) { perror("malloc"); exit(-1); } //将节点赋值 newnode->val = x; newnode->next = NULL; //当队列为空时 if (pq->head == NULL) { pq->head = pq->tail = newnode; } //当队列不为空时 else { pq->tail->next = newnode; pq->tail = newnode; } pq->size++; }

入队时判断当队列为空时pq->head = pq->tail = NULL,不能对pq->tail解引用。

4.出队

void QueuePop(Queue* pq)//出队列 { assert(pq); assert(!QueueEmpty(pq)); //判断队列是否为空 //当队列中只剩一个结点时 if (pq->head->next == NULL) { free(pq->head);//释放节点 pq->head = pq->tail = NULL;//队列置空 } //队列中有多个结点时 else { QNode* cur = pq->head;//记录下一个节点 pq->head = cur->next; free(cur);//释放头节点 } pq->size--;//有效个数-- }

出队时要判断队列是否为空,assert(!QueueEmpty(pq))

5.打印

void QueuePrint(Queue* pq)//打印 { assert(pq); QNode* cur = pq->head; while (cur) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); }

注意循环条件while (cur),不能写为while(cur!=pq->tail)

6.返回队头的值

QDataType QueueFront(Queue* pq)//返回队头的值 { assert(pq); assert(!QueueEmpty(pq)); return pq->head->val; }

7.返回队尾的值

QDataType QueueBack(Queue* pq)//返回队尾的值 { assert(pq); assert(!QueueEmpty(pq)); return pq->tail->val; }

8.统计队列中有效值个数

int QueueSize(Queue* pq)//返回队列中有效值个数 { assert(pq); //int size = 0; //QNode* cur = pq->head; //while (cur) //{ // size++; // cur = cur->next; //} //return size; return pq->size; }

9.销毁

void QueueDestroy(Queue* pq)//销毁 { assert(pq); QNode* cur = pq->head; //遍历,一个一个的释放空间 while (cur) { QNode* del = cur->next; free(cur); cur = del; } pq->head = pq->tail = NULL;//置空 }

将队列(二)改写为长尾词,如何表达?

本文共计612个文字,预计阅读时间需要3分钟。

将队列(二)改写为长尾词,如何表达?

3. 入队函数 +void QueuePush(Queue *pq, QDataType x) // 入队{ assert(pq); QNode *newnode=(QNode *)malloc(sizeof(QNode)); // 创建节点 if (newnode==NULL) { perror(malloc); exit(-1); } // 节点赋值 newnode->val=x; newnode->next=NULL; // 当队列为空时,新节点即为头节点 if (pq->head==NULL) { pq->head=newnode; pq->tail=newnode; } else { // 否则,将新节点添加到队尾 pq->tail->next=newnode; pq->tail=newnode; }}

3.入队

void QueuePush(Queue* pq, QDataType x)//入队 { assert(pq); QNode* newnode = (QNode*)malloc(sizeof(QNode)); //创建节点 if (newnode == NULL) { perror("malloc"); exit(-1); } //将节点赋值 newnode->val = x; newnode->next = NULL; //当队列为空时 if (pq->head == NULL) { pq->head = pq->tail = newnode; } //当队列不为空时 else { pq->tail->next = newnode; pq->tail = newnode; } pq->size++; }

入队时判断当队列为空时pq->head = pq->tail = NULL,不能对pq->tail解引用。

4.出队

void QueuePop(Queue* pq)//出队列 { assert(pq); assert(!QueueEmpty(pq)); //判断队列是否为空 //当队列中只剩一个结点时 if (pq->head->next == NULL) { free(pq->head);//释放节点 pq->head = pq->tail = NULL;//队列置空 } //队列中有多个结点时 else { QNode* cur = pq->head;//记录下一个节点 pq->head = cur->next; free(cur);//释放头节点 } pq->size--;//有效个数-- }

出队时要判断队列是否为空,assert(!QueueEmpty(pq))

5.打印

void QueuePrint(Queue* pq)//打印 { assert(pq); QNode* cur = pq->head; while (cur) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); }

注意循环条件while (cur),不能写为while(cur!=pq->tail)

6.返回队头的值

QDataType QueueFront(Queue* pq)//返回队头的值 { assert(pq); assert(!QueueEmpty(pq)); return pq->head->val; }

7.返回队尾的值

QDataType QueueBack(Queue* pq)//返回队尾的值 { assert(pq); assert(!QueueEmpty(pq)); return pq->tail->val; }

8.统计队列中有效值个数

int QueueSize(Queue* pq)//返回队列中有效值个数 { assert(pq); //int size = 0; //QNode* cur = pq->head; //while (cur) //{ // size++; // cur = cur->next; //} //return size; return pq->size; }

9.销毁

void QueueDestroy(Queue* pq)//销毁 { assert(pq); QNode* cur = pq->head; //遍历,一个一个的释放空间 while (cur) { QNode* del = cur->next; free(cur); cur = del; } pq->head = pq->tail = NULL;//置空 }

将队列(二)改写为长尾词,如何表达?