C语言队列--链队列

2.4 队列--链队列

1 .main.c

//
//  main.c
//  10_LinkQueue
//
//  Created by ls on 2022/5/23.
//  Copyright © 2022 LSC001. All rights reserved.
//


#include "LinkQueue.h"

int main() {
    LQueue myqueue;
    InitQueue(&myqueue);

    int select=1;
    ElemType value=0;
    while(select)
    {
        printf("********************************************\n");
        printf("* [1]  push_back           [2]  pop_head   *\n");
        printf("* [3]  show_Queue          [4]  isEmpty    *\n");
        printf("* [5]  get_top             [6]  length     *\n");
        printf("* [7]  clear               [8]  destroy    *\n");
        printf("********************************************\n");
        printf("请选择:> ");
        scanf("%d",&select);
        if(0 == select)
            break;
        
        switch (select)
        {
            case 1:
                printf("Please input a number(-1 end): ");
                while(scanf("%d",&value),value!= -1)
                {
                    push_back(&myqueue,value);
                }
                show_Queue(&myqueue);
                break;
            case 2:
                pop_head(&myqueue,&value);
                show_Queue(&myqueue);
                break;
            case 3:
                show_Queue(&myqueue);
                break;
            case 4:
                printf("Empty: %ld\n",isEmpty(myqueue));
                break;
            case 5:
                get_top(myqueue);
                break;
            case 6:
                printf("length: %ld\n",length(myqueue));
                break;
            case 7:
                clear0(&myqueue);
                break;
            case 8:
                destroy(&myqueue);
                break;
            default:
                printf("输入错误!");
                break;
        }
    }
    destroy(&myqueue);
    printf("Hello, World!\n");
    return 0;
}

2.LinkQueue.h

//
//  LinkQueue.h
//  10_LinkQueue
//
//  Created by ls on 2022/5/23.
//  Copyright © 2022 LSC001. All rights reserved.
//

#ifndef LinkQueue_h
#define LinkQueue_h


#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

// 1 定义宏
#define ElemType int

// 2 定义节点、单链表
typedef struct LQnode
{
    ElemType data;
    struct LQnode *next;
}LQnode,*PLQnode;

typedef struct LQueue
{
    PLQnode front,rear;
    int size;
}LQueue;

// 3 函数声明

void InitQueue(LQueue *Q);
void push_back(LQueue *Q,ElemType num);     // 1
void pop_head(LQueue *Q,ElemType *num);     // 2
void show_Queue(LQueue *Q);                 // 3
void get_top(LQueue Q);                     // 4
long isEmpty(LQueue Q);                     // 5
long length(LQueue Q);                      // 6
void clear0(LQueue *Q);                     // 7
void destroy(LQueue *Q);                    // 8



#endif /* LinkQueue_h */

3.LinkQueue.c


//
//  LinkQueue.c
//  10_LinkQueue
//
//  Created by ls on 2022/5/23.
//  Copyright © 2022 LSC001. All rights reserved.
//

#include "LinkQueue.h"

void InitQueue(LQueue *Q)
{
    Q->front = Q->rear = (PLQnode)malloc(sizeof(PLQnode));
    assert(Q->front != NULL);
    Q->front->next = NULL;
    Q->size = 0;
}
PLQnode new_(ElemType num)
{
    PLQnode p = (PLQnode)malloc(sizeof(PLQnode));
    p->data = num;
    p->next = NULL;
    return  p;
}
void push_back(LQueue *Q,ElemType num)     // 1
{
    if(!Q->front) return;
    PLQnode p = new_(num);
    Q->rear->next = p;
    Q->rear = p;
    Q->size++;
}
void pop_head(LQueue *Q,ElemType *num)     // 2
{
    if(!Q->front || Q->front == Q->rear) return;
    PLQnode p = Q->front->next;
    *num = p->data;
    Q->front->next = p->next;
    if(p == Q->rear) Q->rear = Q->front;
    free(p);
    p=NULL;
    Q->size--;
}
void show_Queue(LQueue *Q)                 // 3
{
    if(!Q->front || Q->front == Q->rear) return;
    PLQnode p = Q->front->next;
    while (p) {
        printf("%d,",p->data);
        p = p->next;
    }
    printf("\n");
}
void get_top(LQueue Q)                     // 4
{
    if(!Q.front || Q.front == Q.rear) return;
    printf("%d\n",Q.front->next->data);
}
long isEmpty(LQueue Q)                     // 5
{
    if(!Q.front) return -1000000;
    if(Q.front == Q.rear)return 1;
    else return 0;
}
long length(LQueue Q)                      // 6
{
    if(!Q.front) return -1000000;
    return Q.size;
}
void clear0(LQueue *Q)                     // 7
{
    if(!Q->front) return;
    PLQnode p = Q->front->next;
    while(p)
    {
        Q->front->next=p->next;
        free(p);
        p=Q->front->next;
        Q->size--;
    }
}
void destroy(LQueue *Q)                    // 8
{
    clear0(Q);
    free(Q->front);
    Q->front = Q->rear = NULL;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值