C语言——循环队列和链队列的基本运算

// 循环队列
#include <stdio.h> #include "SeqQue.h" // 循环队列的基本运算 /* const int maxsize = 20; typedef struct cycque { int data[maxsize]; int front, rear; }CycQue; */ // 1. 初始化 void InitQueue(CycQue CQ) { CQ.front = 0; CQ.rear = 0; } // 2. 判断队空 int EmptyQueue(CycQue CQ) { if(CQ.rear == CQ.front) return 1; else return 0; } // 3. 入队列 int EnQueue(CycQue CQ, int x) { if((CQ.rear + 1)%maxsize == CQ.front) { printf("队列满\n"); return 0; } else { CQ.rear = (CQ.rear + 1)%maxsize; CQ.data[CQ.rear] = x; return 1; } } // 4. 出队列 int OutQueue(CyQue CQ) { if(EmptyQueue(CQ)) { printf("队列空\n"); return 0; } else { CQ.front = (CQ.front + 1)%maxsize; return 1; } } // 5.取队列首元素 int GetHead(CycQue CQ) { if(EmptyQueue(CQ)) { printf("队列为空\n"); return 0; } else { return CQ.data[(CQ.front + 1)%maxsize]; /* 说明:为了方便操作,规定front指向队列首元素的前一个单元, rear指向实际的队列尾元素单元。 */ } } // 循环队列的基本运算 main() { }

 

链队列

#include <stdio.h>
#include "Lkqueue.h"

/*
// 链队列类型定义
typedef struct LinkQueueNode
{
    int data;
    struct LinkQueueNode *next;
}LkQueNode
typedef struct LkQueue
{
    LkQueNode *front, *rear;
}LkQue;
*/

// 1. 队列的初始化
void InitQueue(LkQue *LQ)
{
    LkQueNode *temp;
    temp = (LkQueNode *)malloc(sizeof(LkQueNode));
    LQ->front = temp;
    LQ->rear = temp;
    (LQ->front)->next = NULL;
}

// 2. 判队列空
int EmptyQueue(LkQue LQ)
{
    if(LQ.rear == LQ.front)
        return 1;
    else
        return 0;
}

// 3. 入队列
void EnQueue(LkQue *LQ, int x)
{
    LkQueNode *temp;
    temp = (LkQueNode *)malloc(sizeof(LkQueNode));
    temp->data = x;
    temp->next = NULL;
    (LQ->rear)->next = temp; // 新节点入队列
    LQ->rear = temp; // 置新的队列尾节点

}

// 4. 出队列
int OutQueue(LkQueue *LQ)
{
    LkQueNode *temp;
    if(EmptyQueue(LQ))
    {
        printf("队列为空\n");
        return 0;
    }
    else
    {
        temp = LQ->front->next; // 队列首元素
        (LQ->front)->next = temp->next;

        if(temp->next == NULL)
            LQ->rear = LQ->front; // 无首节点时,front和rear都指向头节点
        free(temp);
        return 1;
    }
}

 

转载于:https://www.cnblogs.com/lqcdsns/p/7401893.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值