目录
栈是后进先出。队列是先进先出。
比如push1234四个数字,那么pop时就是先pop1,再pop2。First in first out。
队列的实现当然也可以使用数组和链表。不过链表实现更方便。因为队列的删除对于链表也就相当于头删,而数组则需要挪动数据。往后插入数据,链表可以使用双指针走即可解决尾插问题。
选定链表后,我们再想需要什么样的链表,需不需要哨兵位。如果双向链表,优势就是找前一个节点,但是队列貌似不需要这个优势,且论其头尾部操作,双链表不一定比带双指针的单链表简单,这里就敲定单链表。再来想一下哨兵位,其实这个可要可不要,不带头的话我们创建一个结构体,然后在后面插入数据即可,哨兵位带上也可,所以不是必要的东西。
学习了栈的实现后,队列也会相对简单些。原理都差不多。保持着先进先出的原则,两个指针,一个指向头,一个指向尾。由于要一直使用,所以除了建立一个基本的结构体,我们在建立一个结构体,里面包括head和tail两个结构体指针以及一个size,用来表示放入了几个数据。基本的结构体里还是一个整形,一个next指针,这个整形data用来表示这个节点放入的数据。
直接放代码
- #pragma once
- #define _CRT_SECURE_NO_WARNINGS 1
- #include
- #include
- #include
- #include
-
- typedef int QDataType;
- typedef struct QueueNode
- {
- QDataType data;
- struct QueueNode* next;
- }QNode;
-
- typedef struct Queue
- {
- QNode* head;
- QNode* tail;
- QDataType size;
- }Queue;
-
- void QueueInit(Queue* pq);
- void QueueDestroy(Queue* pq);
- void QueuePush(Queue* pq, QDataType x);
- void QueuePop(Queue* pq);
- QDataType QueueFront(Queue* pq);
- QDataType QueueBack(Queue* pq);
- bool QueueEmpty(Queue* pq);
- QDataType QueueSize(Queue* pq);
- void QueuePrint(Queue* pq);
- bool QueueEmpty(Queue* pq);
-
- void QueueInit(Queue* pq)
- {
- assert(pq);
- pq->head = NULL;
- pq->tail = NULL;
- pq->size = 0;
- }
-
- void QueueDestroy(Queue* pq)
- {
- assert(pq);
- QNode* cur = pq->head;
- while (cur)
- {
- //QNode* next = cur->next;
- //free(cur);
- //cur = next;
- QNode* del = cur;
- cur = cur->next;
- free(del);
- }
- pq->head = pq->tail = NULL;
- pq->size = 0;
- }
-
- void QueuePush(Queue* pq, QDataType x)
- {
- assert(pq);
- QNode* node = (QNode*)malloc(sizeof(QNode));
- if (node == NULL)
- {
- perror("malloc fail");
- exit(-1);
- }
- node->data = x;
- node->next = NULL;
- if (pq->tail == NULL)
- {
- pq->head = pq->tail = node;
- }
- else
- {
- pq->tail->next = node;
- pq->tail = node;
- }
- pq->size++;
- }
-
- void QueuePop(Queue* pq)
- {
- assert(pq);
- assert(!QueueEmpty(pq));
- if (pq->head->next == NULL)
- {
- free(pq->head);
- pq->head = pq->tail = NULL;
- }
- else
- {
- QNode* node = pq->head;
- pq->head = pq->head->next;
- free(node);
- }
- pq->size--;
- }
-
- QDataType QueueFront(Queue* pq)
- {
- assert(pq);
- assert(!QueueEmpty(pq));
- return pq->head->data;
- }
-
- QDataType QueueBack(Queue* pq)
- {
- assert(pq);
- assert(!QueueEmpty(pq));
- return pq->tail->data;
- }
-
- bool QueueEmpty(Queue* pq)
- {
- assert(pq);
- return pq->head == NULL && pq->tail == NULL;
- }
-
- QDataType QueueSize(Queue* pq)
- {
- assert(pq);
- return pq->size;
- }
测试代码
- void TestQueue()
- {
- Queue q;
- QueueInit(&q);
- QueuePush(&q, 1);
- QueuePush(&q, 2);
-
- printf("%d ", QueueFront(&q));
- QueuePop(&q);
-
- QueuePush(&q, 3);
- QueuePush(&q, 4);
-
- printf("%d\n", QueueSize(&q));
- printf("%d\n", QueueEmpty(&q));
- printf("%d\n", QueueFront(&q));
- printf("%d\n", QueueBack(&q));
-
- while (!QueueEmpty(&q))
- {
- printf("%d ", QueueFront(&q));
- QueuePop(&q);
- }
- printf("\n");
-
- printf("%d\n", QueueSize(&q));
- printf("%d\n", QueueEmpty(&q));
- //printf("%d\n", QueueFront(&q));
- //printf("%d\n", QueueBack(&q));
-
- QueueDestroy(&q);
- }
-
- int main()
- {
- TestQueue();
- return 0;
- }
队列不写print函数。
队列这里就不写太多了
结束。