数据结构-队列

一、队列

  • 队列是一种特殊的线性表,特殊之处在于它只允许在表的前端进行删除操作,而在表的后端进行插入操作,和栈一样,队列是一种操作受限制的线性表。
  • 进行插入操作的端称为队尾,进行删除操作的端称为队头。
  • 这种先进先出的线性序列,称为“队列”。它的操作受限,只能在两端操作:一端进,一端出。
  • 进的一端称为队尾,出的一端称为队头。队列可以用顺序存储,也可以用链式存储

1.1、顺序队列

-队列的顺序存储采用一段连续的空间存储数据元素,并用两个整形变量记录对头和队尾元素的下标

typedef int datatype;
#define N 128
typedef struct {
	datatype data[N];
	int front;
	int rear;
}sequeue;

1.2、链表队列

  • 链式队列是一种使用链表实现的队列数据结构。与数组队列相比,链式队列不需要预先指定固定大小,可以动态地添加和删除元素。
typedef int datatype;
typedef struct node {
	datatype data;
	struct node *next;
}listnode , *linklist;
typedef struct {
	linklist front;
	linklist rear;
}linkqueue;

二、顺序队列实现

2.1、顺序队列创建

sequeue * queue_create() {
	sequeue *sq;
	if ((sq = (sequeue *)malloc(sizeof(sequeue))) == NULL) {
		printf("malloc failed\n");
		return NULL;
	}
	memset(sq->data, 0, sizeof(sq->data));
	sq->front = sq->rear = 0;
	return sq;
}

2.2、顺序队列入列

int enqueue(sequeue *sq, datatype x) {
	if (sq == NULL) {
		printf("sq is NULL\n");
		return -1;
	}
	if ((sq->rear + 1) % N == sq->front) {
		printf("sequeue is full\n");
		return -1;
	}
	sq->data[sq->rear] = x;
	sq->rear = (sq->rear + 1) % N;
	return  0;
}

2.3、顺序队列出队

datatype dequeue(sequeue *sq) {
	datatype ret;
	ret = sq->data[sq->front];
	sq->front = (sq->front + 1) % N;
	return ret;
}

2.4、顺序队列是否为空

int queue_empty(sequeue *sq) {
	if (sq == NULL) {
		printf("sq is NULL\n");
		return -1;
	}
	return (sq->front == sq->rear ? 1 : 0);
}

2.5、顺序队列是否满

int queue_full(sequeue *sq) {
	if (sq == NULL) {
		printf("sq is NULL\n");
		return -1;
	}
	if ((sq->rear + 1) % N == sq->front) {
		return 1;
	}
	else {
		return 0;
	}
}

2.6、顺序队列清空

int queue_clear(sequeue *sq) {
	if (sq == NULL) {
		printf("sq is NULL\n");
		return -1;
	}
	sq->front = sq->rear = 0;
	return 0;
}

2.7、顺序队列释放

sequeue * queue_free(sequeue *sq) {
	if (sq == NULL) {
		printf("sq is NULL\n");
		return NULL;
	}
	free(sq);
	sq = NULL;
	return NULL;
}

三、链式队列实现

3.1、链式队列创建

linkqueue * queue_create() {
	linkqueue *lq;
	if ((lq = (linkqueue *)malloc(sizeof(linkqueue))) == NULL) {
		printf("malloc linkqueue failed\n");
		return NULL;
	}
	lq->front = lq->rear = (linklist)malloc(sizeof(listnode));
	if (lq->front == NULL) {
		printf("malloc node failed\n");
		return NULL;
	}
	lq->front->data = 0;
	lq->front->next = NULL;
	return lq;
}

3.2、链式队列入队

int enqueue(linkqueue *lq, datatype x) {
	linklist p;
	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}
	if ((p = (linklist)malloc(sizeof(listnode))) == NULL) {
		printf("malloc node failed\n");
		return -1;
	}
	p->data = x;
	p->next = NULL;
	lq->rear->next = p;
	lq->rear = p;
	return 0;
}

3.3、链式队列出队

datatype dequeue(linkqueue *lq) {
	linklist p;
	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}
	p = lq->front;
	lq->front = p->next;
	free(p);
	p = NULL;
	return (lq->front->data);
}

3.4、链式队列是否为空

int queue_empty(linkqueue *lq) {
	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

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

3.5、链式队列清空

int queue_clear(linkqueue *lq) {
	linklist p;
	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}
	while (lq->front->next) {
		p = lq->front;
		lq->front = p->next;
		printf("clear free:%d\n", p->data);
		free(p);
		p = NULL;
	}
	return 0;
}

3.6、链式队列释放

linkqueue * queue_free(linkqueue *lq) {
	linklist p;
	if (lq == NULL) {
		printf("lq is NULL\n");
		return NULL;
	}
	while (lq->front) {
		p = lq->front;
		lq->front = p->next;
		printf("free:%d\n", p->data);
		free(p);
	}
	free(lq);
	lq = NULL;
	return NULL;
}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值