欧美一区2区三区4区公司二百,国产精品婷婷午夜在线观看,自拍偷拍亚洲精品,国产美女诱惑一区二区

數據結構之棧(四)

Queue.c

1.頭文件的聲明

#include "Queue.h"

2.初始化和銷毀函數的定義

void QueueInit(Que* pq)
{
? ? assert(pq);

? ? pq->head = pq->tail = NULL;
? ? pq->size = 0;
}

void QueueDestroy(Que* pq)
{
? ? assert(pq);

? ? QNode* cur = pq->head;
? ? while (cur)
? ? {
? ? ? ? QNode* next = cur->next;
? ? ? ? free(cur);
? ? ? ? cur = next;
? ? }

? ? pq->head = pq->tail = NULL;
? ? pq->size = 0;
}

3.入隊列和出隊列函數的定義

void QueuePush(Que* pq, QDataType x)
{
? ? assert(pq);
? ? QNode* newnode = (QNode*)malloc(sizeof(QNode));
? ? if (newnode == NULL)
? ? {
? ? ? ? perror("malloc fail");
? ? ? ? exit(-1);
? ? }

? ? newnode->data = x;
? ? newnode->next = NULL;

? ? if (pq->tail == NULL)
? ? {
? ? ? ? pq->head = pq->tail = newnode;
? ? }
? ? else
? ? {
? ? ? ? pq->tail->next = newnode;
? ? ? ? pq->tail = newnode;
? ? }

? ? pq->size++;
}

void QueuePop(Que* pq)
{
? ? assert(pq);//判斷隊列指針指向是否為空
? ? assert(!QueueEmpty(pq));//判斷隊列里面的數據是否為空

? ? if (pq->head->next == NULL)
? ? {
? ? ? ? free(pq->head);
? ? ? ? pq->head = pq->tail = NULL;
? ? }
? ? else
? ? {
? ? ? ? QNode* next = pq->head->next;
? ? ? ? free(pq->head);
? ? ? ? pq->head = next;
? ? }

? ? pq->size--;
}

4.查找隊頭、查找隊尾函數的定義

//查找隊頭元素
QDataType QueueFront(Que* pq)
{
? ? assert(pq);
? ? assert(!QueueEmpty(pq));

? ? return pq->head->data;
}

//查找隊尾元素
QDataType QueueBack(Que* pq)
{
? ? assert(pq);
? ? assert(!QueueEmpty(pq));

? ? return pq->tail->data;
}

5.判空以及長度計算函數的定義

//判斷是否為空
bool QueueEmpty(Que* pq)
{
? ? assert(pq);

? ? return pq->head == NULL;
}

//長度計算
int QueueSize(Que* pq)
{
? ? assert(pq);

? ? return pq->size;
}

(3)Test.c

1.頭文件的聲明

#include "Queue.h"

2.測試函數的定義

void QueueTest() {
? ? Que pq;
? ? QueueInit(&pq);
? ? QueuePush(&pq, 1);
? ? QueuePush(&pq, 2);
? ? QueuePush(&pq, 3);
? ? QueuePush(&pq, 4);
? ? QueuePush(&pq, 5);
? ? while (!QueueEmpty(&pq)) {
? ? ? ? printf("%d ", QueueFront(&pq));
? ? ? ? QueuePop(&pq);
? ? }
? ? QueueDestroy(&pq);
}

?

?

文章鏈接: http://www.qzkangyuan.com/26238.html

文章標題:數據結構之棧(四)

文章版權:夢飛科技所發布的內容,部分為原創文章,轉載請注明來源,網絡轉載文章如有侵權請聯系我們!

聲明:本站所有文章,如無特殊說明或標注,均為本站原創發布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發布本站內容到任何網站、書籍等各類媒體平臺。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
建站教程

數據結構之棧(三)

2023-12-22 10:05:17

建站教程投稿分享

c語言之fcfs

2024-1-29 13:08:26

0 條回復 A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優惠劵
今日簽到
有新私信 私信列表
搜索

夢飛科技 - 最新云主機促銷服務器租用優惠

主站蜘蛛池模板: 兴文县| 邓州市| 阳泉市| 长丰县| 千阳县| 山丹县| 苏尼特左旗| 栖霞市| 崇礼县| 忻城县| 枣阳市| 南宫市| 黄龙县| 济源市| 彩票| 芦溪县| 金堂县| 桐城市| 竹溪县| 布拖县| 青河县| 上虞市| 通海县| 军事| 武强县| 拉萨市| 高邑县| 北宁市| 三都| 柳河县| 耿马| 建水县| 菏泽市| 柘城县| 翼城县| 丹凤县| 沭阳县| 青铜峡市| 皋兰县| 建阳市| 前郭尔|