队列的代码实现(两种方式)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define COLOR(a,b) "\033[" #b "m" a "\033[0m"
#define GREEN(a) COLOR(a,32)
#define RED(a) COLOR(a,31)
typedef struct Queue {
	int* data;
	int head, tail, length;
	int count;
};
Queue* init(int);
void clear(Queue*);
int push(Queue*, int);
int front(Queue*);
int pop(Queue*);
int empty(Queue*);
void output(Queue*);
int expand(Queue*);
int main() {
	srand(time(0));
#define MAX 20
	Queue* q = init(MAX);
	for (int i = 0; i < 100; i++) {
		int op = rand() % 4;
		int val = rand() % 100;
		switch (op) {
		case 0:
		case 1:
		case 2: {
			printf("push %d to the Queue = %d\n", val, push(q, val));
		}break;
		case 3: {
			if (!empty(q)) {
				printf("pop %d from the Queue =", front(q));
				printf("%d\n", pop(q));
			}
		}break;
		}
		output(q), printf("\n");
	}
#undef MAX
	clear(q);
	return 0;
}
Queue* init(int n) {
	Queue* q = (Queue*)malloc(sizeof(Queue));
	q->data = (int*)malloc(sizeof(int) * n);
	q->length = n;
	q->head = q->tail = 0;//默认队尾指针指向的是队尾元素的下一个位置
	q->count = 0;
	return q;
}
void clear(Queue* q) {
	if (q == NULL)return;
	free(q->data);
	free(q);
}
int empty(Queue* q) {
	return q->count == 0;
	//return q->head == q->tail; //尾指针和头指针重合则为空
}
int front(Queue* q) {
	return q->data[q->head];
}
int push(Queue* q, int val) {
	if (q == NULL)return 0;
	if (q->count == q->length) {
		if (!expand(q)) {
			printf(RED("fail to expand!\n"));
			return 0;
		}
		printf(GREEN("success to expand!\n"));
	}
	q->data[q->tail++] = val;
	if (q->tail == q->length)q->tail = 0;
	q->count += 1;
	return 1;
}
int pop(Queue* q) {
	if (q == NULL)return 0;
	if (empty(q))return 0;
	q->head += 1;
	if (q->head == q->length)q->head = 0;
	q->count -= 1;
	return 1;
}
int expand(Queue* q) {
	int extr_size = q->length;
	int* temp = NULL;
	while (extr_size) {
		temp = (int*)malloc(sizeof(int) * (q->length + extr_size));
		if (temp != NULL)break;
		extr_size >>= 1;
	}
	if(temp == NULL)return 0;
	for (int i = q->head, j = 0; j < q->count; j++) {
		temp[j] = q->data[(i + j) % q->length];
	}
	free(q->data);
	q->data = temp;
	q->head = 0, q->tail = q->count;
	q->length += extr_size;
	return 1;
}
void output(Queue* q) {
	printf("Queue(%d) : [", q->count);
	for (int i = q->head, j = 0; j < q->count; j++) {
		j && printf(", ");
		printf("%d", q->data[(i + j) % q->length]);
	}
	printf("]\n");
	return;
}
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
typedef struct Node {
	int data;
	struct Node* next;
}Node;
typedef struct Queue {
	Node head, * tail;
	int length;
}Queue;
Node* get_newNode(int);
Queue* init_queue();
void clear_node(Node*);
void clear_queue(Queue*);
int empty(Queue*);
int pop(Queue*);
int push(Queue*, int);
int front(Queue*);
Node* get_newNode(int val) {
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = val;
	node->next = NULL;
	return node;
}
Queue* init_queue() {
	Queue* q = (Queue*)malloc(sizeof(Queue));
	q->head.next = NULL;
	q->tail = &(q->head);
	q->length = 0;
	return q;
}
void clear_node(Node* node) {
	if (node == NULL)return;
	free(node);
	return;
}
void clear_queue(Queue* q) {
	if (q == NULL)return;
	Node* p = q->head.next, * temp;
	while (p != NULL) {
		temp = p->next;
		clear_node(p);
		p = temp;
	}
	return;
}
int empty(Queue* q) {
	return q->length == 0;
}
int push(Queue* q, int val) {
	if (q == NULL)return 0;
	Node* temp = get_newNode(val);
	q->tail->next = temp;
	q->tail = temp;
	q->length++;
	return 1;
}
int pop(Queue* q) {
	if (q == NULL)return 0;
	if (empty(q))return 0;
	Node* temp = q->head.next;
	if (q->length == 1) {

	}
	q->head.next = temp->next;
	clear_node(temp);
	q->length--;
	if (q->length == 0)q->tail = &(q->head);
	return 1;
}
int front(Queue* q) {
	return q->head.next->data;
}
void output(Queue* q) {
	if (q == NULL)return;
	printf("Queue(%d) : [", q->length);
	for (Node* p = q->head.next; p != NULL; p = p->next) {
		p != q->head.next && printf(", ");
		printf("%d", p->data);
	}
	printf("]\n");
}
int main() {
#define MAX 20;
	srand(time(0));
	Queue* q = init_queue();
	for (int i = 1; i <= 20; i++) {
		int op = rand() % 4;
		int val = rand() % 100;	
		switch (op) {
		case 0:
		case 1:
		case 2: {
			printf("push %d to the queue = %d\n", val, push(q, val));
		}break;
		case 3: {
			if (!empty(q)) {
				printf("pop %d from the queue = ", front(q));
				printf("%d\n", pop(q));
				//printf("pop %d from the queue = %d\n", front(q), pop(q));
			}break;
		}
		}
		output(q);
		printf("\n");
	}
	clear_queue(q);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值