C语言数据结构学习笔记(7)-链式队列

/*链式队列
输出结果:
入队成功,入队元素为a
队首元素为a
入队成功,入队元素为b
队首元素为a
入队成功,入队元素为c
队首元素为a
入队成功,入队元素为d
队首元素为a
入队成功,入队元素为e
队首元素为a
队列元素个数为5
队列中的元素为: a b c d e
出队成功,出队元素为a
队首元素为b
队列元素个数为4
出队成功,出队元素为b
队首元素为c
队列元素个数为3
出队成功,出队元素为c
队首元素为d
队列元素个数为2
出队成功,出队元素为d
队首元素为e
队列元素个数为1
出队成功,出队元素为e
获取失败,队列为空.
队列元素个数为0
队列中无元素.
入队成功,入队元素为f
队列已清空.
入队成功,入队元素为g
队列中的元素为: g
队列已销毁.
入队失败,队列不存在.
请按任意键继续. . .
*/
# include <stdio.h>
# include <stdlib.h>
# define bool int
# define true 1
# define false 0
# define ElemType char
typedef struct Node{
    ElemType data;
    struct Node * next;
}Node, *pNode;

typedef struct Queue{
    pNode front;//队首前一个结点的指针,不存数据,不移动
    pNode rear;//队尾指针
}Queue, *pQueue;

void InitQueue(pQueue ptrQ);//初始化队列
bool IsEmpty(pQueue ptrQ);//判断队列是否为空
bool InQueue(pQueue ptrQ, ElemType elem);//入队
bool OutQueue(pQueue ptrQ, ElemType * elem);//出队
bool GetQueue(pQueue ptrQ, ElemType * elem);//获取队首元素
int QueueLength(pQueue ptrQ);//获取队列元素个数
void ShowQueue(pQueue ptrQ);//遍历队列
void ClearQueue(pQueue ptrQ);//清空队列
void DestoryQueue(pQueue ptrQ);//销毁队列

int main(void)
{
    Queue Q;
    ElemType elem;
    InitQueue(&Q);
    for(int i = 0; i < 5; i++)
    {
        if(InQueue(&Q, 'a'+i))
            printf("入队成功,入队元素为%c\n", Q.rear->data);
        else
            printf("入队失败,队列不存在.\n");

        if(GetQueue(&Q, &elem))
            printf("队首元素为%c\n", elem);
        else
            printf("获取失败,队列为空.\n");
    }
    printf("队列元素个数为%d\n", QueueLength(&Q));
    ShowQueue(&Q);
    for(int i = 0; i < 5; i++)
    {
        if(OutQueue(&Q, &elem))
            printf("出队成功,出队元素为%c\n", elem);
        else
            printf("出队失败,队列为空.\n");

        if(GetQueue(&Q, &elem))
            printf("队首元素为%c\n", elem);
        else
            printf("获取失败,队列为空.\n");
        printf("队列元素个数为%d\n", QueueLength(&Q));
    }
    ShowQueue(&Q);
    if(InQueue(&Q, 'f'))
        printf("入队成功,入队元素为%c\n", Q.rear->data);
    else
        printf("入队失败,队列不存在.\n");
    ClearQueue(&Q);
    if(InQueue(&Q, 'g'))
        printf("入队成功,入队元素为%c\n", Q.rear->data);
    else
        printf("入队失败,队列不存在.\n");
    ShowQueue(&Q);
    DestoryQueue(&Q);
    if(InQueue(&Q, 'h'))
        printf("入队成功,入队元素为%c\n", Q.rear->data);
    else
        printf("入队失败,队列不存在.\n");
    system("pause");
    return 0;
}
void InitQueue(pQueue ptrQ)//初始化队列
{
    ptrQ->front = (pNode)malloc(sizeof(Node));
    if(NULL == ptrQ->front)
        exit(-1);
    ptrQ->front->next = NULL;
    ptrQ->rear = ptrQ->front;
}
bool IsEmpty(pQueue ptrQ)//判断队列是否为空
{
    if(NULL == ptrQ->front->next)
        return true;
    else
        return false;
}
bool InQueue(pQueue ptrQ, ElemType elem)//入队
{    
    if(NULL == ptrQ->front)
        return false;
    pNode pNew = (pNode)malloc(sizeof(Node));
    if(NULL == pNew)
        exit(-1);
    pNew->data = elem;
    pNew->next = NULL;
    ptrQ->rear->next = pNew;
    ptrQ->rear = pNew;
    return true;
}
bool OutQueue(pQueue ptrQ, ElemType * elem)//出队
{
    if(IsEmpty(ptrQ))
        return false;
    else
    {    
        if(ptrQ->front->next == ptrQ->rear)//最后1个元素出队时需将尾指针初始化
            ptrQ->rear = ptrQ->front;
        pNode temp = ptrQ->front->next;
        *elem = temp->data;
        ptrQ->front->next = temp->next;
        free(temp);
        temp = NULL;
        return true;
    }
}
bool GetQueue(pQueue ptrQ, ElemType * elem)//获取队首元素
{
    if(IsEmpty(ptrQ))
        return false;
    else
    {
        *elem = ptrQ->front->next->data;
        return true;
    }
}
int QueueLength(pQueue ptrQ)//获取队列元素个数
{    
    int count = 0;
    if(IsEmpty(ptrQ))
        return count;
    else
    {    
        pNode temp = ptrQ->front->next;
        while(temp != NULL)
        {
            count++;
            temp = temp->next;
        }
        return count;
    }
}
void ShowQueue(pQueue ptrQ)//遍历队列
{
    if(IsEmpty(ptrQ))
        printf("队列中无元素.\n");
    else
    {    
        printf("队列中的元素为: ");
        pNode temp = ptrQ->front->next;
        while(temp != NULL)
        {
            printf("%c ", temp->data);
            temp = temp->next;
        }
        printf("\n");
    }
}
void ClearQueue(pQueue ptrQ)//清空队列
{
    if(IsEmpty(ptrQ))
        printf("队列已为空.\n");
    else
    {
        pNode temp = ptrQ->front->next;
        while(temp != NULL)
        {
            ptrQ->front->next = temp->next;
            free(temp);
            temp = ptrQ->front->next;
        }
        ptrQ->rear = ptrQ->front;
        printf("队列已清空.\n");
    }
}
void DestoryQueue(pQueue ptrQ)//销毁队列
{
    pNode temp= NULL;
    while(ptrQ->front != NULL)
    {
        temp = ptrQ->front;
        ptrQ->front = ptrQ->front->next;
        free(temp);
    }
    ptrQ->rear = NULL;
    printf("队列已销毁.\n");
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值