(程序员面试题)链队列的基本操作

队列的基本操作跟栈类似,有如下几种:入队列,出队列,判断队列满,判断队列空,遍历队列。

但是跟栈不一样的是:栈的入栈和出栈操作始终是在栈顶,但是队列的入队列是在队列尾部,出队列是在队列的头部。


详情请见testcase:

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

#define QUEUE_MAX 3

typedef struct queue {
	char data[10];
	struct queue *next;
} *qu_ptr;

void print_queue(qu_ptr front) {
	qu_ptr tmp = front;
	while (tmp->next) {
		printf("%s\n", tmp->next->data);
		tmp = tmp->next;
	}
	printf("\n");
}

int is_queue_empty(qu_ptr front, qu_ptr rear) {
	if ((front->next == NULL) && (rear->next == NULL)) {
		printf("queue empty\n");
		return 0;
	}
	return -1;
}

int is_queue_full(qu_ptr front) {
	qu_ptr tmp  = front;
	int cur = 0;
	while (tmp->next) {
		cur++;
		tmp = tmp->next;
	}

	if (cur == QUEUE_MAX) {
		printf("queue full\n");
		return 0;
	}
	return -1;
}

int add_queue(qu_ptr front, qu_ptr rear, qu_ptr add) {
	if (is_queue_full(front) == 0) {
		printf("queue full, can not add\n");
		return -1;
	}

	if (is_queue_empty(front, rear) == 0) {
		rear->next = add;
		front->next = add;
		add->next = NULL; 
		return 0;
	}

	rear->next->next = add;
	rear->next = add;
	add->next = NULL;
	return 0;
}

int del_queue(qu_ptr front, qu_ptr rear) {
	if (is_queue_empty(front, rear) == 0) {
		printf("queue empty, can not del\n");
		return -1;
	}

	if (front->next == rear->next) {
		front->next = NULL;
		rear->next = NULL;
		return 0;
	}

	front->next = front->next->next;
	return 0;
}

int main()
{
	qu_ptr front, rear, cheny, cherry, new, test;
	front = (qu_ptr)malloc(sizeof(struct queue));
	rear = (qu_ptr)malloc(sizeof(struct queue));
	cheny = (qu_ptr)malloc(sizeof(struct queue));
	cherry = (qu_ptr)malloc(sizeof(struct queue));
	new = (qu_ptr)malloc(sizeof(struct queue));
	test = (qu_ptr)malloc(sizeof(struct queue));

	// this is an empty queue
	front->next = NULL;
	rear->next = NULL;

	strcpy(cheny->data, "cheny");
	add_queue(front, rear, cheny);
	print_queue(front);

	strcpy(cherry->data, "cherry");
	add_queue(front, rear, cherry);
	print_queue(front);

	strcpy(new->data, "new");
	add_queue(front, rear, new);
	print_queue(front);

	strcpy(test->data, "test");
	add_queue(front, rear, test);
	print_queue(front);


	del_queue(front, rear);
	print_queue(front);

	del_queue(front, rear);
	print_queue(front);
	del_queue(front, rear);
	print_queue(front);
	del_queue(front, rear);

	return 0;
}

运行结果如下:

queue empty
cheny

cheny
cherry

cheny
cherry
new

queue full
queue full, can not add
cheny
cherry
new

cherry
new

new


queue empty
queue empty, can not del

程序的大致意思是:建立一个空队列,然后一次入队列4个,前3个入队列正常,由于队列空间只有3,所以第4个会入队列失败(这个有打印哦),然后依次出队列4个,前3个出队列正常,第4个出队列失败。


入队列和出队列的函数各有一种特殊情况需要判断:

出队列:出队列是从头部出的,理论上来说跟尾部没有关系,但是当队列中只有一个元素的时候,出队列完毕之后队列会变为空队列,此时头部和尾部都需要指向NULL,所以需要尾部的参与

入队列:入队列是从尾部入的,理论上来说跟头部没有关系,但是当队列中没有元素的时候,入队列完毕会让头部和尾部都指向同一个元素,所以需要头部的参与

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值