数据结构队的使用

9 篇文章 0 订阅
1 篇文章 0 订阅
/* 本程序实现队列的基本功能 
    创建队、 进队、 出队、 判满、 判空、销毁 */
/* main.c */
#include<stdio.h>
#include"squeue.h"

int main(void)
{
    squeue SQ = CreateQueue();

    EnQueue(SQ,11);
    EnQueue(SQ,22);
    EnQueue(SQ,33);

    while(FALSE == EmptyQueue(SQ))
    {
        printf("%d\n",DeQueue(SQ));
    }


    return OK;
}
/* queue.c */
#include <stdio.h>
#include <stdlib.h>
#include "squeue.h"
// 创建squeue CreateQueue(void){ squeue sq = malloc(sizeof(Sqnode)); if ( NULL == sq ) { printf("malloc error\n"); return NULL; } sq->front = sq->rear = 0; return sq;}// 判空int EmptyQueue(squeue SQ){ return (SQ->rear == SQ->front ? TRUE : FALSE);}//
 判满int FullQueue(squeue SQ){ int rear = SQ->rear; int front = SQ->front; if( ( rear + 1 ) % MAX == front ) // 空一个位置出来 { return TRUE; } else { return FALSE; }}// 入队data_t EnQueue(squeue SQ,data_t val){ if ( TRUE == FullQueue(SQ) ) { printf("queue is full\n");
 return ERROR; } SQ->rear = SQ->rear % MAX; // 构成循环队列 SQ->a[SQ->rear] = val; SQ->rear++; return OK;}// 出队data_t DeQueue(squeue SQ){ if ( TRUE == EmptyQueue(SQ) ) { printf("queue is empty\n"); return ERROR; } SQ->front = SQ->front % MAX; // 构成循环队列 data_t val
 = SQ->a[SQ->front]; SQ->front++; return val;}// 清空void ClearQueue(squeue SQ){ SQ->front = SQ->rear = 0;}// 销毁void Destroy(squeue SQ){ free(SQ);}

/* queue.h */
#ifndef _SQUEUE_H
#define _SQUEUE_H

#define MAX 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR -1

typedef int data_t;

struct node{
    data_t a[MAX];
    int front;
    int rear;
};
typedef struct node Sqnode;
typedef struct node * squeue;


squeue CreateQueue(void);             // 创建空队
void ClearQueue(squeue SQ);           // 清空
int EmptyQueue(squeue SQ);            // 判断队是否空
int FullQueue(squeue SQ);             // 判断队是否满
data_t EnQueue(squeue SQ,data_t val); // 进队
data_t DeQueue(squeue SQ);            // 出队
void Destroy(squeue SQ);              // 销毁
#endif




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值