链队列数据结构代码(C语言)

利用链队列存储一组数据元素,并利用简单的交互实现队列的入队和出队操作

//ADT 队列(Queue) 链式存储结构 LinkQueue
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 50
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int ElemType;
typedef int Status;

//定义链队列的结点
typedef struct QueueNode
{
    ElemType data;
    struct QueueNode *next;
} QueueNode;
typedef struct QueueNode *LinkQueuePtr;

//定义链队列结构
typedef struct LinkQueue
{
    LinkQueuePtr front;
    LinkQueuePtr rear;
    ElemType Length;
} LinkQueue;

//链队列的初始化
Status InitQueue(LinkQueue *Q)
{
    Q->front = Q->rear = (LinkQueuePtr)malloc(sizeof(QueueNode)); //创建头结点并用链队列的首尾指针指向
    Q->front->next = NULL;                                        //初始化头结点的指针域
    Q->front->data = 0;
    Q->Length = 0;
    return OK;
}

//判断链队列是否初始化成功
Status QueueEmpty(LinkQueue Q)
{
    if (Q.front == Q.rear)
        return TRUE;
    else
        return FALSE;
}

//清空链队列
Status ClearQueue(LinkQueue *Q)
{
    while (Q->front)
    {
        Q->rear = Q->front->next;
        free(Q->front);
        Q->front = Q->rear;
    }
    Q->Length = 0; //在遍历删除结点的最后,队列的首尾指针指向已经为NULL,所以不用再初始化指针
    return OK;
}

//查询链队列数据元素的个数
Status QueueLength(LinkQueue Q)
{
    return Q.Length;
}

//查询队列队头数据元素的值
Status GetElem(LinkQueue Q, ElemType *e)
{
    if (Q.front == Q.rear)
        return ERROR;
    *e = Q.front->next->data;
    return OK;
}

//链队列的插入(入队)
Status QueueInsert(LinkQueue *Q, ElemType e)
{
    LinkQueuePtr p = NULL;
    p = (LinkQueuePtr)malloc(sizeof(QueueNode));
    if (p == NULL)
    {
        printf("队列结点创建失败\n");
        exit(0);
    }
    p->data = e;
    p->next = NULL;
    Q->rear->next = p;
    Q->rear = p;
    Q->Length++;
    return OK;
}

//链队列的删除(出队)
Status QueueDelete(LinkQueue *Q, ElemType *e)
{
    LinkQueuePtr p = NULL;
    if (Q->front == Q->rear)
        return ERROR;
    p = Q->front->next;
    *e = p->data;
    Q->front->next = p->next;
    if (Q->rear == p)
        Q->rear = Q->front; //如果出队的是最后一个结点,将的首尾指针指向相等,表示队列空了。
    free(p);
    Q->Length--;
    return OK;
}

//链队列数据元素的输入
Status CreateQueue(LinkQueue *Q)
{
    LinkQueuePtr s = Q->front;
    printf("请输入一组数据元素的值:");
    while (TRUE)
    {
        int a = 0;
        LinkQueuePtr p = NULL;
        scanf("%d", &a);
        char c = getchar();
        p = (LinkQueuePtr)malloc(sizeof(QueueNode));
        if (p == NULL)
        {
            printf("队列结点创建失败\n");
            exit(0);
        }
        p->data = a;
        p->next = NULL;
        s->next = p;
        s = p;
        Q->rear = p;
        Q->Length++;
        if (c == '\n')
            break;
    }
    return OK;
}

//链队列主函数
int main()
{
    LinkQueue Q;
    ElemType e = 0;
    Status i = 0;
    printf("链队列初始化中......\n");
    i = InitQueue(&Q);
    i = QueueEmpty(Q);
    if (i == 0)
        printf("链队列初始化失败\n");
    else
        printf("链队列初始化成功\n");
    i = CreateQueue(&Q);
    i = QueueLength(Q);
    printf("数据元素存储成功,链队列中的数据元素个数为%d\n", i);
    while (TRUE)
    {
        int a = 0;
        printf("请选择将要进行的操作:\n1:查询数据元素的个数\n2:查询队首数据元素的值\n3:链队列队尾的插入\n4:链队列队首的删除\n5:清空链队列并结束程序\n");
        scanf("%d", &a);
        switch (a)
        {
        case 1:
        {
            int b = 0;
            b = QueueLength(Q);
            printf("链队列中的数据元素个数为%d\n", b);
            break;
        }
        case 2:
        {
            int b = 0;
            b = GetElem(Q, &e);
            if (b == 0)
                printf("查询失败\n");
            printf("链队列队头数据元素的值为%d\n", e);
            break;
        }
        case 3:
        {
            int b = 0;
            printf("请输入要入队的值:");
            scanf("%d", &e);
            b = QueueInsert(&Q, e);
            if (b == 0)
                printf("入队失败\n");
            else
                printf("入队成功\n");
            break;
        }
        case 4:
        {
            int b = 0;
            b = QueueDelete(&Q, &e);
            if (b == 0)
                printf("出队失败\n");
            else
                printf("出队成功,出队数据元素的值为%d\n", e);
            break;
        }
        case 5:
        {
            int b = 0;
            b = ClearQueue(&Q);
            if (b == 0)
                printf("链队列清空失败\n");
            else
                printf("链队列清空成功\n");
            exit(0);
        }
        default:
            break;
        }
    }
    return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值