溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

隊(duì)列的基本操作

發(fā)布時(shí)間:2020-06-27 16:53:29 來源:網(wǎng)絡(luò) 閱讀:642 作者:科大C2504 欄目:編程語言

structComm.h //定義各種結(jié)構(gòu)的頭文件

#ifndef STRUCTCOMM_H_INCLUDED
#define STRUCTCOMM_H_INCLUDED

#undef NULL
#ifdef __cplusplus
    #define NULL 0
#else
    #define NULL ((void *)0)
#endif

struct list_head
{
    struct list_head *next;
    struct list_head *prev;
};

typedef struct tag_myTree
{
    int data;
    struct tag_myTree* pLeft;
    struct tag_myTree* pRight;
}myTree;

typedef struct tag_myStack
{
    int data;
    myTree *pTree;
    struct list_head stStack;
}myStack;

typedef struct tag_myQue
{
    int data;
    myTree *pTree;
    struct list_head stQue;
}myQue;

#endif // STRUCTCOMM_H_INCLUDED

myQue.h

#ifndef MYQUE_H_INCLUDED
#define MYQUE_H_INCLUDED

#include "structComm.h"

myQue* getNewNode();
void initQue(myQue *pRoot);
void destoryQue(myQue *pRoot);
int getQueLen(myQue *pRoot);
int isQueEmpty(myQue *pRoot);
void enQue(myQue *pRoot, myQue *pNew);
myQue* deQue(myQue *pRoot);

#endif // MYQUE_H_INCLUDED

myQue.c

#include "myQue.h"
#include "myList.h"

myQue* getNewQueNode()
{
    myQue *pTmp = NULL;

    pTmp = (myQue *)malloc(sizeof(myQue));
    if (NULL == pTmp)
    {
        return NULL;
    }

    pTmp->data = 0;
    pTmp->pTree = NULL;
    INIT_LIST_HEAD(&(pTmp->stQue));

    return pTmp;
}

void initQue(myQue *pRoot)
{
    pRoot->data = 0;
    pRoot->pTree = NULL;
    INIT_LIST_HEAD(&(pRoot->stQue));

    return;
}

void destoryQue(myQue *pRoot)
{
    struct list_head *pos = NULL;
    struct list_head *n = NULL;
    myQue *pstQue = NULL;

    list_for_each_safe(pos, n, &(pRoot->stQue))
    {
        list_del(pos);
        pstQue = list_entry(pos, myQue, stQue);
        free(pstQue);
        pos = n;
    }

    return;
}

int getQueLen(myQue *pRoot)
{
    int len = 0;
    struct list_head *pos = NULL;

    list_for_each(pos, &(pRoot->stQue))
    {
        len++;
    }

    return len;
}

int isQueEmpty(myQue *pRoot)
{
    return list_empty(&(pRoot->stQue));
}

void enQue(myQue *pRoot, myQue *pNew)
{
    list_add(&(pNew->stQue), &(pRoot->stQue));
}

myQue* deQue(myQue *pRoot)
{
    myQue *pstQue = NULL;
    struct list_head *pTmp = NULL;

    if (!isQueEmpty(pRoot))
    {
        pTmp = (pRoot->stQue).prev;
        list_del((pRoot->stQue).prev);
        pstQue = list_entry(pTmp, myQue, stQue);
        return pstQue;
    }

    return NULL;
}

main.c

#include <stdio.h>
#include "myQue.h"

myQue g_stQueRoot;      //棧的頭節(jié)點(diǎn),不存儲(chǔ)數(shù)據(jù)

int main()
{
    int i = 0;
    myQue *pNewNode = NULL;

    initQue(&g_stQueRoot);  //初始化之前要保證頭隊(duì)列為空
    printf("is empty:\t%d\n", isQueEmpty(&g_stQueRoot));

    for (i = 0; i < 5; i++)
    {
        pNewNode = getNewNode();
        if (NULL == pNewNode)
        {
            exit(0);
        }

        pNewNode->data = i;
        enQue(&g_stQueRoot, pNewNode);
    }

    printf("is empty:\t%d\n", isQueEmpty(&g_stQueRoot));
    printf("que len:\t%d\n", getQueLen(&g_stQueRoot));

    printf("de:\t");
    for (i = 0; i < 5; i++)
    {
        pNewNode = deQue(&g_stQueRoot);
        printf("%d\t", pNewNode->data);
    }

    pNewNode = deQue(&g_stQueRoot);
    if (NULL == pNewNode)
    {
        printf("\ncan not de\n");
    }

    return 0;
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI