栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

队列链式存储基本操作(C语言)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

队列链式存储基本操作(C语言)

#include 
#include 
#include

typedef int ElemType;

typedef  struct LinkNode
{
    ElemType data;
    struct LinkNode *next;
} LinkNode;

typedef struct
{
    LinkNode *front,*rear;//队头指针,队尾指针
} LinkQueue;


void InitQueue(LinkQueue *Q)
{
    Q->front=Q->rear=(LinkNode*)malloc(sizeof(LinkNode));//建立头节点
    Q->front->next=NULL;
}

bool IsEmpty(LinkQueue *Q)
{
    if(Q->rear==Q->front)return true;//队头指针和队尾指针,指向同一片区域
    return false;
}

//入队
void EnQueue(LinkQueue *Q,ElemType e)
{
    LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));
    s->data=e;
    s->next=NULL;
    Q->rear->next=s;
    Q->rear=s;//尾插法
}

bool DeQueue(LinkQueue *Q,ElemType *x)
{
    if(Q->front==Q->rear)return false;//队空

    LinkNode *p=Q->front->next;
    *x=p->data;
    Q->front->next=p->next;
    if(p==Q->rear)Q->rear=Q->front;//队列中只有一个元素
    free(p);//删除p所指向的空间
    return true;
}

void PrintQueue(LinkQueue *Q)
{
    LinkNode *p=Q->front->next;//p指向首结点
    while(p)
    {
        printf("%d---->",p->data);
        p=p->next;
    }
    printf("n");
}


int main()
{

    LinkQueue Q;
    InitQueue(&Q);
    //将1~10插入队列
    for(int i=1; i<=10; i++)
    {
        EnQueue(&Q,i);
    }
    PrintQueue(&Q);
    //将1~5出队
    for(int i=1; i<=5; i++)
    {
        int e;
        DeQueue(&Q,&e);
        printf("当前出队元素为:%dn",e);
    }
    PrintQueue(&Q);


    return 0;
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/889636.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号