ahua的头发

导航

数据结构::链队列

数据结构::链队列

 

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define MaxSize 10
typedef int dataType;

//定义链队列结点结构
typedef struct Node{
    struct Node *next;
    dataType data;
}LinkNode;
//定义链队列结构
typedef struct {
    LinkNode *front;  //指向头部结点,出队
    LinkNode *rear;  //指向尾部结点,入队
}LQueue;
typedef LQueue *pLQueue;  //定义一个指向链队列的指针

//初始化链队列
pLQueue InitLQueue()
{
    pLQueue plq=(LQueue*)malloc(sizeof(LQueue));
    plq->front=plq->rear=NULL;
    return plq;
}

//入队
void EnLQueue(pLQueue plq,dataType value)
{

    LinkNode *p=(LinkNode*)malloc(sizeof(LinkNode));
    p->data=value;
    p->next=NULL;
    if (plq->rear==NULL)  //处理空队列
    {
        plq->rear=p;
        plq->front=p;
    }
    else{
        plq->rear->next=p;
        plq->rear=p;
    }
}

//自动创建一个链队列(不使用头结点)
pLQueue CreateLQueue()
{
    pLQueue plq=InitLQueue();
    int i=1,num=9;
    while (i<=9)
    {
        cout<<i<<" ";
        EnLQueue(plq,i);
        i++;
    }
    cout<<endl;
    return plq;
}

//出队
void DeLQueue(pLQueue plq)
{
    if (plq->front==NULL)
    {
        cout<<"Empty"<<endl;
        return;
    }
    cout<<plq->front->data<<endl;
    LinkNode *temp=plq->front;
    plq->front=temp->next;
    free(temp);
}

//全部出队
void AllDeLQueue(pLQueue plq)
{
    while (plq->front!=NULL)
    {
        DeLQueue(plq);
    }
    cout<<"Empty"<<endl;
    return;
}

//释放队列
void FreeLQueue(pLQueue plq)
{
    while (plq->front!=NULL)
    {
        LinkNode *temp=plq->front->next;
        plq->front->next=temp->next;
        free(temp);
    }
    free(plq);
}

int main()
{
    pLQueue plq=CreateLQueue();
    AllDeLQueue(plq);
    FreeLQueue(plq);
    system("pause");
}

 

posted on 2020-06-11 16:56  ahua的头发  阅读(169)  评论(0编辑  收藏  举报