.队列:只允许在一端进行插入,在另一端进行删除的线性表。
链队列:使用链表实现的队列;具有队头指针和队尾指针,指示队列元素所在的位置。
链队列特性:
优点:
缺点:
时间复杂度
帆哥的代码:
#include <stdio.h>
#include <malloc.h>/*** 链队列的节点.*/
typedef struct LinkNode{int data;LinkNode* next;
}*LinkNodePtr;/*** 链队列.*/
typedef struct LinkQueue{LinkNodePtr front;LinkNodePtr rear;
}*LinkQueuePtr;/*** Construct an empty queue.*/
LinkQueuePtr initQueue(){LinkQueuePtr resultPtr = (LinkQueuePtr)malloc(sizeof(struct LinkQueue));//The header, the data is not useful.LinkNodePtr headerPtr = (LinkNodePtr)malloc(sizeof(struct LinkNodePtr));headerPtr->next = NULL;resultPtr->front = headerPtr;resultPtr->rear = headerPtr;return resultPtr;
}//Of initQueue/*** Output the queue.*/
void outputLinkQueue(LinkQueuePtr paraQueuePtr){LinkNodePtr tempPtr = paraQueuePtr->front->next;while (tempPtr != NULL) {printf("%d ", tempPtr->data);tempPtr = tempPtr->next;}//Of whileprintf("rn");
}//Of outputLinkQueue/*** Enqueue.*/
void enqueue(LinkQueuePtr paraQueuePtr, int paraElement) {//Step 1. Create a new nodeLinkNodePtr tempNodePtr = (LinkNodePtr)malloc(sizeof(struct LinkNode));tempNodePtr->data = paraElement;tempNodePtr->next = NULL;//Step 2. Link to the existing rearparaQueuePtr->rear->next = tempNodePtr;//Step 3.
本文发布于:2024-02-05 08:58:50,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170728405965117.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |