循环队列基本操作-华为OJ.docx
文本预览下载声明
/****************************************************************************** Copyright (C), 2001-2011, Huawei Tech. Co., Ltd. ****************************************************************************** File Name : Version : Author : Created : 2012/03/12 Last Modified : Description : Function List : History :1.Date : 2012/03/12 Author : Modification: Created file******************************************************************************/#includestdlib.h#defineMAXSIZE 50structstrqueue {int queue[MAXSIZE];int head; /* 队头 */int tail; /* 队尾 */intnum; /* 队元素个数 */};boolinitqueue(structstrqueue *s){if (!s)return 0;for (inti = 0; i MAXSIZE; i++) {s-queue[i] = 0;}s-head = -1;s-tail = -1;s-num = 0;return 1;}boolenqueue(structstrqueue *s, intx) /* 进队列,返回0表示失败,返回1表示成功 */{if (!s)return 0;if (s-num == MAXSIZE)return 0;if (s-head == -1 || s-tail == -1) {s-head = 0;s-tail = 0;s-queue[s-tail] = x; /*赋值*/s-num = 1;return 1;}s-tail = (s-tail + 1) % MAXSIZE;s-queue[s-tail] = x;s-num++;return 1;}booldequeue(structstrqueue *s, int *x) /* 出队列,返回0表示失败,返回1表示成功 */{if (!s || !x)return 0;if (s-num == 0)return 0;*x = s-queue[s-head]; /*赋值*/s-queue[s-head] = 0;s-head = (s-head + 1) % MAXSIZE;/*从对头出队列*/s-num--;if (s-num == 0) {s-head = -1;s-tail = -1;}return 1;}intgethead(structstrqueue *s) /* 获得队列头数值 */{if (!s)return -1;if (s-num == 0)return -1;int head = 0;head = s-queue[s-head];return head;}intgettail(structstrqueue *s) /* 获得队列尾数值 */{if (!s)return 0;int tail = 0;if (s-num == 0)return -1;tail = s-queue[s-tail];return tail;}intgetqueuelenth(structstrqueue *s) /* 获得队列长度 */{if (!s)return 0;intlenth = 0;lenth = s-num;returnlenth;}bool search(structstrqueue *s, intx) /* 在队列中查找x是否存在,如果存在返回1,否则返回0 */{if (!s)return 0;intheadTemp = s-head;inttailTemp = s-tail;if (s-num == MAXSIZE) {for (inti = 0; i MAXSIZE; i++) {if (x == s-queue[i])return 1;}}else {while (headTemp != tailTemp) {if (x == s-queue[headTemp])return 1;headTemp = (
显示全部