c语言开发之循环队列的实现:c语言开发之循环队列的又该怎么实现呢?有什么方法呢?希望下面的文章对大家有所帮助。
运行环境:vs2015
#include #include #include //-------定义----- typedef int elemtype; typedef int status; //------定义常量的值 #define ok 1 #define error 0 #define true 1 #define false 0 #define overflow 0 //----------结构体----------- typedef struct { elemtype *elem; //存储空间基址 int front; //队头位标 int rear; //队尾位标,指示队尾元素的下一个位置 int maxsize; //存储容量 }sqqueue; //---------------接口声明-------- status initqueue_sq(sqqueue &q, int size); //构造一个新队列 status destoryqueue_sq(sqqueue &q); //销毁队列q,q不再存在 void clearqueue_sq(sqqueue &q); //将q置为空队列 status queueempty_sq(sqqueue q); //判断队列是否为空 int queuelength_sq(sqqueue q); //返回队列q中的元素个数,即队列的长度 status gethead_sq(sqqueue q, elemtype e); //若队列不空,则用e返回q的队列头元素 status enqueue_sq(sqqueue &q, elemtype e);//在队尾之后插入元素e,为新的队尾元素 status dequeue_sq(sqqueue &q, elemtype &e); //出队 //----------初始化循环队列------ status initqueue_sq(sqqueue &q, int size) { q.elem = (elemtype *)malloc(size * sizeof(elemtype)); //分配空间 if (q.elem == null) return overflow; //分配空间失败 q.front = q.rear = 0; //置队列为空队列 q.maxsize = size; //赋值,设定存储空间长度 return ok; } //----------销毁循环队列--------- status destoryqueue_sq(sqqueue &q) { q.front = q.rear = 0; //置队为空 q.elem = null; //把基址设为null q.maxsize = 0; free(q.elem); return ok; } //------清空循环队列------ void clearqueue_sq(sqqueue &q) { q.front = q.rear = 0; } //------判断循环队列是否为空------ status queueempty_sq(sqqueue q) { if (q.front == q.rear) return true; else return false; } //------计算队列长度----- int queuelength_sq(sqqueue q) { if (q.rear >= q.front) { //考虑两个都是0的情况 return (q.rear-q.front); }else { return (q.rear - q.front + q.maxsize + 1); //q.rear-q.front+q.maxsize+1 } } //----若队列不空,则用e返回q的队列头元素--- status gethead_sq(sqqueue q, elemtype e) { if (queueempty_sq(q) == false) { //注意细节! e = q.elem[q.front]; return e; } else { return error; } } //---在队尾之后插入元素e,为新的队尾元素--- status enqueue_sq(sqqueue &q, elemtype e) { if (q.front == (q.rear + 1) % (q.maxsize)) { //队满条件 少用一个元素空间 return overflow; } else { q.elem[q.rear] = e; q.rear = (q.rear + 1) % q.maxsize; //q.rear循环+1 return e; } } //----出队--- status dequeue_sq(sqqueue &q, elemtype &e) { if (q.front == q.rear) //队空 return error; e = q.elem[q.front]; q.front = (q.front + 1) % q.maxsize; //队头循环+1 return e; } void main() { sqqueue q; elemtype e = 0; int length1; //构造新队列 initqueue_sq(q, 8); //入队 for (int i = 0; i < 12; i++) { enqueue_sq(q, i+1); } length1 = queuelength_sq(q); //遍历循环队列 for (int i = 0; i < length1; i++) { printf("%dn", q.elem[i]); } //出队操作 printf("执行出队操作,出队3个元素:n"); for (int i = 0; i < 3; i++) { dequeue_sq(q, e); } length1 = queuelength_sq(q); for (int i = q.front; i < q.rear; i++) { printf("%dn", q.elem[i]); } printf("执行入队操作,入队2个元素:n"); for (int i = 0; i < 2; i++) { enqueue_sq(q, i + 1); } for (int i = q.front; i < q.maxsize; i++) { printf("%dn", q.elem[i]); } for (int i = 0; i < q.rear; i++) { printf("%dn", q.elem[i]); } printf("返回队头元素为:%dn", gethead_sq(q, e)); }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/607973.html