04 数据结构之队列

循环队列

/* squence_queue.h */
#ifndef _SQUENCE_QUEUE_H_
#define _SQUENCE_QUEUE_H_


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define QUEUE_SIZE 128
#define DEBUG(msg) \
	printf("--%s--, %s", __func__, msg)

typedef int data_t;
typedef struct {
	data_t arr[QUEUE_SIZE];
	int front, rear;
}squence_queue_t;

squence_queue_t *SqQueue_Create(void);
int SqQueue_Free(squence_queue_t *q);
int SqQueue_Clear(squence_queue_t *q);
int SqQueue_Entry(squence_queue_t *q, data_t value);
data_t SqQueue_Departure(squence_queue_t *q);
int SqQueue_Isempty(squence_queue_t *q);
int SqQueue_Isfull(squence_queue_t *q);


#endif
/* squence_queue.c */
#include "squence_queue.h"


/*brife: create a cycle queue
 *para: none
 *ret: heap zone addrees
 * */
squence_queue_t *SqQueue_Create(void)
{
	squence_queue_t *q = NULL;

	q = (squence_queue_t *)malloc(sizeof(squence_queue_t));
	if(q == NULL) {
		DEBUG("malloc failed\n");	
		return NULL;
	}

	memset(q->arr, 0, sizeof(data_t) * QUEUE_SIZE);
	q->front = 0;
	q->rear = 0;

	return q;
}


/*brife: free a cycle in heap space
 *para: cycle queue pointer
 *ret: 0 succsess  other failed
 * */
int SqQueue_Free(squence_queue_t *q)
{
	if(q == NULL) {
		DEBUG("param is invalid!\n");	
		return -1;
	}

	free(q);
	q = NULL;
	
	return 0;
}

/*brife: clear cycle queue content
 * 
 * */
int SqQueue_Clear(squence_queue_t *q)
{
	if(q == NULL) {
		DEBUG("param is invalid!\n");	
		return -1;
	}

	memset(q->arr, 0, sizeof(data_t) * QUEUE_SIZE);
	q->front = q->rear = 0;

	return 0;
}

/*brife: cycle queue entry operator
 *
 * */
int SqQueue_Entry(squence_queue_t *q, data_t value)
{
	if(q == NULL) {
		DEBUG("param is invalid!\n");	
		return -1;
	}

	if((q->rear + 1) % QUEUE_SIZE == q->front) {
		DEBUG("queue is full, entry failed!\n");	
		return -1;
	}

	q->arr[q->rear] = value;
	q->rear = (q->rear + 1) % QUEUE_SIZE;

	return 0;
}


/*brife: cycle queue departure operator
 *
 * */
data_t SqQueue_Departure(squence_queue_t *q)
{
	if(q == NULL) {
		DEBUG("param is invalid!\n");	
		return -1;
	}

	data_t ret;
	ret = q->arr[q->front];
	q->front = (q->front + 1) % QUEUE_SIZE;

	return ret;
}


/*brife: judge cycle queue is empty?
 *ret: 1---empty
 * 	   0---no empty	
 * */
int SqQueue_Isempty(squence_queue_t *q)
{
	if(q == NULL) {
		DEBUG("param is invalid!\n");	
		return -1;
	}

	return (q->front == q->rear ? 1 : 0);
}

/*brife: judge a cycle queue is full?
 *ret: 1---full
 *     0---no full
 * */
int SqQueue_Isfull(squence_queue_t *q)
{
	if(q == NULL) {
		DEBUG("param is invalid!\n");	
		return -1;
	}

	return ((q->rear + 1) % QUEUE_SIZE == q->front ? 1 : 0);
}

链式队列

/* link_queue.h */
#ifndef _LINK_QUEUE_H_
#define _LINK_QUEUE_H_

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define DEBUG(msg) \
	printf("--%s--, --%s--\n", __func__, msg);

typedef int data_t;
typedef struct link_queue {
	data_t data;
	struct link_queue *next;
}queue_t;
typedef struct {
	queue_t *head;
	queue_t *rear;
}linkqueue_t;

linkqueue_t *link_queue_create();
int link_queue_free(linkqueue_t *lq);
int link_queue_entry(linkqueue_t *lq, data_t value);
data_t link_queue_departure(linkqueue_t *lq);
int link_queue_clear(linkqueue_t *lq);
int link_queue_isempty(linkqueue_t *lq);

#endif
/* link_queue.c */
#include "link_queue.h"


linkqueue_t *link_queue_create()
{
	linkqueue_t *p = NULL;

	p = (linkqueue_t *)malloc(sizeof(linkqueue_t));
	if(p == NULL) {
		DEBUG("malloc failed\n");
		return NULL;
	}

	p->head = p->rear = (queue_t *)malloc(sizeof(queue_t));
	if(p->head == NULL) {
		DEBUG("malloc failed\n");
		return NULL;
	}

	p->head->data = 0;
	p->head->next = NULL;


	return p;
}


int link_queue_free(linkqueue_t *lq)
{
	if(lq == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	queue_t *p = lq->head;

	while(lq->head != NULL) {
		p = lq->head;
		lq->head = lq->head->next;
		free(p);
	}

	free(lq);
	lq = NULL;

	return 0;

}


int link_queue_entry(linkqueue_t *lq, data_t value)
{
	if(lq == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	queue_t *p = (queue_t *)malloc(sizeof(queue_t));
	if(p == NULL) {
		DEBUG("new node malloc failed\n");
		return -1;
	}

	p->data = value;
	p->next = NULL;

	lq->rear->next = p;
	lq->rear = lq->rear->next;

	return 0;
	
}

data_t link_queue_departure(linkqueue_t *lq)
{
	if(lq == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	data_t ret = 0;
	queue_t *p;

	ret = lq->head->next->data;
	p = lq->head;
	lq->head = lq->head->next;

	free(p);

	return ret;
}


int link_queue_clear(linkqueue_t *lq)
{
	if(lq == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	queue_t *p;

	while(lq->head->next != NULL) {
		p = lq->head->next;
		lq->head = p->next;
		free(p);
		p = NULL;
	}

	return 0;
}


int link_queue_isempty(linkqueue_t *lq)
{
	if(lq == NULL) {
		DEBUG("param is invalid\n");
		return -1;
	}

	return (lq->head == lq->rear ? 1 : 0);
}

相关推荐

  1. 04 数据结构队列

    2024-03-14 22:08:01       23 阅读
  2. 数据结构队列

    2024-03-14 22:08:01       46 阅读
  3. 数据结构队列

    2024-03-14 22:08:01       19 阅读
  4. 数据结构队列

    2024-03-14 22:08:01       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-14 22:08:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-14 22:08:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-14 22:08:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-14 22:08:01       20 阅读

热门阅读

  1. STM32day2

    STM32day2

    2024-03-14 22:08:01      18 阅读
  2. adb 筛选查看Unity日志

    2024-03-14 22:08:01       20 阅读
  3. 前端面试练习24.3.12

    2024-03-14 22:08:01       17 阅读
  4. Android 二维码相关(二)

    2024-03-14 22:08:01       20 阅读
  5. C 练习实例76-求倒数和

    2024-03-14 22:08:01       18 阅读
  6. C++图书管理案例

    2024-03-14 22:08:01       17 阅读
  7. EKF+PF的MATLAB例程

    2024-03-14 22:08:01       18 阅读