链队列

链队列是队列的链式表示和实现。主要操作包括:初始化、清空、销毁、判断是否为空、插入、删除、队列长度、获取队头元素。在实现的过程中,在队列的结构中添加了count,用来记录队列中元素的个数,这样无论在判定是否为空、求队列长度以及其他操作的实现中都可以简化。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -1

typedef struct QNode
{
    int data;
    struct QNode *next;
}QNode;

typedef struct
{
    QNode *front;//队头指针
    QNode *rear;//队尾指针
    int count;//队列元素个数
}LinkQueue;

//队列初始化
int InitQueue(LinkQueue *Q)
{
    Q->front = Q->rear = (QNode *)malloc(sizeof(QNode));
    if(!Q->front)
        exit(OVERFLOW);
    Q->rear->next =NULL;
    Q->count = 0;
    return OK;
}

//销毁队列
int DestroyQueue(LinkQueue *Q)
{
    while(Q->front)
    {
        Q->rear = Q->front->next;
        free(Q->front);
        Q->front = Q->rear;
    }
    return OK;
}

//清空队列
int ClearQueue(LinkQueue *Q)
{
    while(Q->front->next->next)
    {
        Q->front->next = Q->front->next->next;
        free(Q->front);
    }
    free(Q->front->next);
    Q->front->next = NULL;
    Q->rear = Q->front;
    Q->count = 0;
    return OK;
}

//判断队列是否为空
int QueueEmpty(LinkQueue *Q)
 {
     if (Q->count == 0)
         printf("Queue is empty\n");
     else
         printf("Queue is not empty\n");
     return 0;
 }

//队列长度
 int QueueLength(LinkQueue *Q)
 {
     int n;
     n = Q->count;
     return n;
 }

//获取队头元素
int GetHead (LinkQueue *Q)
{
    int n;
    QNode *P;
    if (QueueLength(Q)==0)
    {
        printf("Queue don't have Head\n");
    }
    else
    {
        P = Q->front->next;
        n = P->data;
    }
    return n;
}

//插入
int InsertQueue(LinkQueue *Q, int n)
{
    QNode *P;
    P=(QNode *)malloc(sizeof(QNode));
    if(!P)
        exit(OVERFLOW);
    P->data = n;
    P->next = NULL;
    Q->rear->next = P;
    Q->rear = P;
    Q->count ++;
    return OK;
}

//删除
int DeleteQueue(LinkQueue *Q)
{
    QNode *P;
    int n;
    if(Q->front == Q->rear)
        return ERROR;
    P = Q->front->next;
    n = P->data;
    Q->front->next = P->next;
    if(Q->rear == P)
        Q->rear = Q->front;
    free(P);
    Q->count --;
    return 0;
}


int main()
{
    LinkQueue *Q;
    int i,k;
    Q=(LinkQueue*)malloc(sizeof(LinkQueue));
    InitQueue(Q);
    int quit=1;
    while(quit==1)
    {
        printf("1.Insert 2.Delete 3.GetHead 4.QueueLength 5.Empty 6.Clear 7.Destroy\n");
        scanf("%d",&k);
        getchar();
        switch(k)
        {
            case 1:
                printf("Insert number \n");
                scanf("%d",&i);
                InsertQueue(Q,i);
                break;
            case 2:
                printf("Delete number is ");
                DeleteQueue(Q);
                printf("\n");
                break;
            case 3:
                printf("Queue head is %d\n",GetHead(Q));
                break;
            case 4:
                printf("Queue length is %d\n",QueueLength(Q));
                break;
            case 5:
                QueueEmpty(Q);
            case 6:
                ClearQueue(Q);
                QueueEmpty(Q);
                break;
            case 7:
                DestroyQueue(Q);
                break;
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值