(程序员面试题)链队列的基本操作
2014-03-30 16:49
204 查看
队列的基本操作跟栈类似,有如下几种:入队列,出队列,判断队列满,判断队列空,遍历队列。
但是跟栈不一样的是:栈的入栈和出栈操作始终是在栈顶,但是队列的入队列是在队列尾部,出队列是在队列的头部。
详情请见testcase:
运行结果如下:
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,所以需要尾部的参与
入队列:入队列是从尾部入的,理论上来说跟头部没有关系,但是当队列中没有元素的时候,入队列完毕会让头部和尾部都指向同一个元素,所以需要头部的参与
但是跟栈不一样的是:栈的入栈和出栈操作始终是在栈顶,但是队列的入队列是在队列尾部,出队列是在队列的头部。
详情请见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,所以需要尾部的参与
入队列:入队列是从尾部入的,理论上来说跟头部没有关系,但是当队列中没有元素的时候,入队列完毕会让头部和尾部都指向同一个元素,所以需要头部的参与
相关文章推荐
- GC(垃圾处理机制)面试题
- 面试准备之常见上机题目搜罗(一)
- 【面向程序设计常见面试题】纯虚函数和抽象类(已阅)
- 【面向对象程序设计常见面试题】多继承与虚函数之间的关系(已阅)
- (程序员面试题)链栈的基本操作
- 【面向对象程序设计常见面试题】虚析构函数(已阅)
- 黑马程序员——Java学习笔记之⑨——“交通灯管理系统”
- 【面向对象程序设计常见面试题】虚函数的定义的几点说明(已阅)
- 黑马程序员——Java学习笔记之⑧——“Java新技术”
- 黑马程序员——Java学习笔记之⑤——“IO输入与输出”
- 黑马程序员——Java学习笔记之④——“JavaAPI”
- 黑马程序员——Java学习笔记之③——“多线程”
- 黑马程序员——Java学习笔记之②——“面向对象”
- 老程序员十年生涯黯然总结
- 新长城 郁闷的一道面试题(把C盘文件复制到D盘)
- 即使别人是码农 你却不该是-写的挻实在的,有一定的保存价值
- (程序员面试题)链表的基本操作
- 来自苹果、谷歌、微软等知名公司六道有趣智力面试题,试试吧!
- 黑马程序员---java 概述
- Android面试之内存溢出解决