我要成为嵌入式高手之3月22日数据结构第五天!!
————————————————————————————
顺序队列
存在问题:假溢出——解决办法:循环队列
空队列、满队列如何判断?
满队列:rear + 1 = front
空队列:rear = front
循环队列
相当于数组
由于有一个指针始终要进行判断队列,所以只有n-1个数据
1、创建
SEQ_QUE *CreateSqeQueue(int max)
{
SEQ_QUE *pseq = malloc(sizeof(SEQ_QUE));
if (NULL == pseq)
{
perror("fail to malloc");
return NULL;
}
pseq->pbase = malloc(max*sizeof(DATA_TYPE));
if (NULL == pseq->pbase)
{
perror("fail to malloc");
return NULL;
}
pseq->front = 0;
pseq->rear = 0;
pseq->size = max;
return pseq;
}
2、判断是否为满队列
/* 满队列 */
int FullOfSeq(SEQ_QUE *pseq)
{
if ((pseq->rear+1) % pseq->size == pseq->front)//尾的下一个是头
{
return 1;
}
return 0;
}
3、判断是否为空队列
/* 空队列 */
int EmptyOfSeq(SEQ_QUE *pseq)
{
if (pseq->front == pseq->rear)//头和尾相同
{
return 1;
}
return 0;
}
4、入队
int JoinInSeq(SEQ_QUE *pseq, DATA_TYPE data)
{
if (FullOfSeq(pseq))
{
return -1;
}
pseq->pbase[pseq->rear] = data;
pseq->rear = (pseq->rear+1) % pseq->size;
return 0;
}
5、遍历
int SearchQueAll(SEQ_QUE *pseq)
{
int i = pseq->front;
while (i != pseq->rear)
{
printf("%d ", pseq->pbase[i]);
i = (i+1) % pseq->size;
}
putchar('\n');
return 0;
}
6、出队
int PopInSeq(SEQ_QUE *pseq, DATA_TYPE *pdata)
{
if (EmptyOfSeq(pseq))
{
return -1;
}
if (pdata != NULL)
{
*pdata = pseq->pbase[pseq->front];
}
pseq->front = (pseq->front+1) % pseq->size;
return 0;
}
7、清空队列
void ClearSeq(SEQ_QUE *pseq)
{
pseq->front = pseq->rear = 0;
}
//头和尾相同且为0,意为清空队列
8、销毁队列
void DestroySeq(SEQ_QUE *pseq)
{
free(pseq->pbase);
free(pseq);
}
总程序:
head.h
#ifndef _HEAD_H
#define _HEAD_H
#include <stdio.h>
#include <stdlib.h>
typedef int DATA_TYPE;
typedef struct que
{
DATA_TYPE *pbase;
int front;
int rear;
int size;
}SEQ_QUE;
extern SEQ_QUE *CreateSqeQueue(int max);
extern int JoinInSeq(SEQ_QUE *pseq, DATA_TYPE data);
extern int SearchQueAll(SEQ_QUE *pseq);
extern int PopInSeq(SEQ_QUE *pseq, DATA_TYPE *pdata);
extern void ClearSeq(SEQ_QUE *pseq);
extern void DestroySeq(SEQ_QUE *pseq);
#endif
queue.c
#include "head.h"
SEQ_QUE *CreateSqeQueue(int max)
{
SEQ_QUE *pseq = malloc(sizeof(SEQ_QUE));
if (NULL == pseq)
{
perror("fail to malloc");
return NULL;
}
pseq->pbase = malloc(max*sizeof(DATA_TYPE));
if (NULL == pseq->pbase)
{
perror("fail to malloc");
return NULL;
}
pseq->front = 0;
pseq->rear = 0;
pseq->size = max;
return pseq;
}
/* 满队列 */
int FullOfSeq(SEQ_QUE *pseq)
{
if ((pseq->rear+1) % pseq->size == pseq->front)//尾的下一个是头
{
return 1;
}
return 0;
}
/* 空队列 */
int EmptyOfSeq(SEQ_QUE *pseq)
{
if (pseq->front == pseq->rear)//头和尾相同
{
return 1;
}
return 0;
}
int JoinInSeq(SEQ_QUE *pseq, DATA_TYPE data)
{
if (FullOfSeq(pseq))
{
return -1;
}
pseq->pbase[pseq->rear] = data;
pseq->rear = (pseq->rear+1) % pseq->size;
return 0;
}
int SearchQueAll(SEQ_QUE *pseq)
{
int i = pseq->front;
while (i != pseq->rear)
{
printf("%d ", pseq->pbase[i]);
i = (i+1) % pseq->size;
}
putchar('\n');
return 0;
}
int PopInSeq(SEQ_QUE *pseq, DATA_TYPE *pdata)
{
if (EmptyOfSeq(pseq))
{
return -1;
}
if (pdata != NULL)
{
*pdata = pseq->pbase[pseq->front];
}
pseq->front = (pseq->front+1) % pseq->size;
return 0;
}
void ClearSeq(SEQ_QUE *pseq)
{
pseq->front = pseq->rear = 0;
}
void DestroySeq(SEQ_QUE *pseq)
{
free(pseq->pbase);
free(pseq);
}
main.c
#include "head.h"
int main(void)
{
DATA_TYPE data;
SEQ_QUE *pseq = CreateSqeQueue(2);
if (NULL == pseq)
{
perror("fail to create!");
return -1;
}
JoinInSeq(pseq, 1);
JoinInSeq(pseq, 2);
JoinInSeq(pseq, 3);
SearchQueAll(pseq);
return 0;
}