赞
踩
#include<iostream> #include<stdlib.h> using namespace std; #define MAXSIZE 100 typedef int ElemType; typedef long long ll; typedef struct { ElemType* base;//分配需要的存储空间 ll front; ll rear; }queue; bool InitQuque(queue& s) { s.base = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE); if (!s.base)return false; s.front = s.rear = 0; return true; } ll QueueLength(queue&q) { return (q.rear - q.front + MAXSIZE) % MAXSIZE; } bool pop(queue& q) { if (q.front == q.rear)return false;//判断是否为空 q.front = (q.front + 1) % MAXSIZE; return true; } bool insert(queue& s, ElemType val) { if ((s.rear + 1) % MAXSIZE == s.front)return false;//判断是否为满 s.base[s.rear] = val; s.rear = (s.rear + 1) % MAXSIZE; return true; } bool empty(queue& que) { return que.front == que.rear; } ElemType front(queue& que) { return que.base[que.front]; } int main() { queue que; if (!InitQuque(que))cout << "分配内存错误" << endl; for (int i = 0; i < 102; i++) { if (!insert(que, i)) { cout << "超出其范围" << endl; } } while (!empty(que)) { cout << front(que) << " "; pop(que); } cout << endl; for (int i = 0; i < 102; i++) { if (!insert(que, i)) { cout << "超出其范围" << endl; } } while (!empty(que)) { cout << front(que) << " "; pop(que); } pop(que); return 0; }
由于多了一个空隙来作为分辨队列为空还是为满,所以就到98
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。