线性结构之队列

线性结构之队列

计算机输入输出缓冲区的结构就是队列的应用,队列是另一种限定性线性表,只允许在表的一端进行,而删除在表的另一端进行,这种结构叫做队或队列,把允许插入的一端叫做队尾,把允许删除的一端叫做队头,队列的插入操作叫做入队列,队列的删除操作叫做出队列或者退队列;队列中没有元素时,叫做空队列。队列是“先进先出”表。

这里写图片描述

基本操作如下:

(1)队列初始化

(2)入队操作

(3)出队操作

(4)读队头元素

(5)判队空操作

1. 顺序队列:

#include<stdio.h>
#include<malloc.h>

#define MAXSIZE 100
#define TRUE  1
#define FALSE 0

typedef int dataType;
typedef int boolean;

typedef struct SeQueue {
    dataType data[MAXSIZE];
    int rear,front;
}SeQueue; 

void initQueue(SeQueue **s);                //初始化队列
boolean isEmpty(int rear);      //判断队空 
boolean isFull(int rear);       //判断队满
void inQueue(SeQueue *s, dataType data);    //入队操作
dataType outQueue(SeQueue *s);              //出队操作
dataType getQueue(SeQueue *s);              //得到队头操作

dataType getQueue(SeQueue *s) {
    int index = s->front + 1;
    if(isEmpty(s->rear)) {
        printf("对列为空.\n");
        return;
    }

    return s->data[index]; 
} 
dataType outQueue(SeQueue *s) {
    if(isEmpty(s->rear))     {
        printf("队列为空,无法出队.\n");
        return;
    }
    printf("out : %d\n\n", s->front);
    return s->data[++s->front]; 
}

void inQueue(SeQueue *s, dataType data) {
    if(isFull(s->rear)) {
        printf("队列已满,无法入队\n");
        return;
    } 

    s->data[++s->rear] = data;
}


boolean isFull(int rear) {
    return MAXSIZE <= rear ? TRUE : FALSE;
}
boolean isEmpty(int rear) {
    return rear <= -1 ? TRUE : FALSE;
}
void initQueue(SeQueue **s) {
    if(*s != NULL) {
        printf("已经初始化过.\n");
        return;
    }

    (*s) = (SeQueue *)malloc(sizeof(SeQueue));
    (*s)->front = -1;
    (*s)->rear = -1;
}

void main(void) {
    SeQueue *s = NULL;
    initQueue(&s);
    inQueue(s, 5);
    inQueue(s, 6);
    inQueue(s, 7);
    printf("\n%d\n", outQueue(s));
    printf("\n%d\n", outQueue(s));
    printf("\n%d\n", getQueue(s));
    printf("%d %d", s->front, s->rear);
} 

在顺序队列中存在“假溢出”的情况,出现的原因是由于“队尾出,队头进”的操作所造成的。

2. 循环队列

解决上述问题的方法之一是将数据区的data[0……MAXSIZE-1]看成头尾相接的循环结构,即规定最后一个单元的后继为第一个单元。这样数据区看起来就像是一个环,形象的称为循环队列。

循环表的构造方法可以利用数学上的求模运算,将入队时的队尾指针加1,修改为:

sq->rear = (sq->rear + 1) % MAXSIZE

出队时的队头指针加1,修改为:

sq->front = (sq->front + 1) % MAXSIZE

队满的条件:

(sq->rear + 1)% MAXSIZE == sq->front

代码相关:

#include<stdio.h>
#include<malloc.h> 

#define MAXSIZE 100
#define TRUE  1
#define FALSE 0

typedef int boolean;
typedef int dataType;
typedef struct CSeQueue {
    dataType data[MAXSIZE];
    int front, rear;
}CSeQueue; 


void initSeQueue(CSeQueue **cs);
boolean inSeQueue(CSeQueue *cs, dataType data);
dataType outSeQueue(CSeQueue *cs);
void destorySeQueue(CSeQueue **cs);

void destorySeQueue(CSeQueue **cs) {
    if(*cs == NULL) {
        return;
    }

    free(*cs);
}
dataType outSeQueue(CSeQueue *cs) {
    if(cs->front == cs->rear) {
        printf("队列为空.\n");
        return FALSE;
    }

    cs->front = (cs->front + 1) % MAXSIZE;
    return cs->data[cs->front];

}

boolean inSeQueue(CSeQueue *cs, dataType data) {
    if((cs->rear + 1) % MAXSIZE == cs->front) {
        printf("队列已满.\n");
        return FALSE;
    }
    cs->rear = (cs->rear + 1) % MAXSIZE;
    cs->data[cs->rear] = data;
}

void initSeQueue(CSeQueue **cs) {
    if(*cs != NULL) {
        printf("队列已经初始化过了.\n");
        return;
    }

    (*cs) = (CSeQueue*)malloc(sizeof(CSeQueue));
    (*cs)->front = (*cs)->rear = MAXSIZE-1;  //将 front, rear的值改为第一个单元的直接前驱的下标 
}

void main(void) {
    CSeQueue *cs = NULL;
    initSeQueue(&cs);
    inSeQueue(cs, 5);
    inSeQueue(cs, 6); 
    inSeQueue(cs, 7);
    inSeQueue(cs, 8);
    outSeQueue(cs);
    outSeQueue(cs);
    destorySeQueue(&cs);
} 

代码地址:http://download.csdn.net/detail/dear_mr/9815785

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值