数据结构课件C++版=讲解.ppt
文本预览下载声明
* mayan 第三章 栈和队列 --循环队列 队列初始化:front = rear = 0; 队空条件:front == rear; 队满条件:(rear+1) % maxSize == front; 注意,在循环队列中,最多只能存放maxSize-1个元素。 * mayan 第三章 栈和队列 --循环队列的类定义 template class T class SeqQueue { //队列类定义 protected: int rear, front; //队尾与队头指针 T *elements; //队列存放数组 int maxSize; //队列最大容量 * mayan 第三章 栈和队列 --循环队列的类定义 public: SeqQueue(int sz = 10); //构造函数 ~SeqQueue() { delete[ ] elements; } //析构函数 bool EnQueue(const T x); //新元素进队列 bool DeQueue(); //退出队头元素 bool getFront(); //取队头元素值 void makeEmpty() { front = rear = 0; }//队列做空 * mayan 第三章 栈和队列 --循环队列的类定义 bool IsEmpty() const { return front == rear; } //判断队空 bool IsFull() const //判断队满 { return ((rear+1)% maxSize == front); } int getSize() const //队中元素个数 { return (rear-front+maxSize) % maxSize; } void display() const;//输出队中元素 }; * mayan 第三章 栈和队列 --循环队列的函数实现 //构造函数 template class T SeqQueueT::SeqQueue(int sz) : front(0), rear(0), maxSize(sz) { elements = new T[maxSize]; } * mayan 第三章 栈和队列 --循环队列的函数实现 //若队列不满, 则将x插入到该队列队尾, 否则返回 template class T bool SeqQueueT::EnQueue(const T x) { if (IsFull() == true) return false; elements[rear] = x; //先存入 rear = (rear+1) % maxSize; //尾指针加一 return true; } * mayan 第三章 栈和队列 --循环队列的函数实现 //若队列不空则函数退队头元素并返回其值 template class T bool SeqQueueT::DeQueue() { int x; if (IsEmpty() == true) return false; x = elements[front]; //先取队头 front = (front+1) % maxSize; //再队头指针加一 return true; } * mayan 第三章 栈和队列 --循环队列的函数实现 //若队列不空则函数返回该队列队头元素的值 template class T bool SeqQueueT::getFront() const { int x; if (IsEmpty() == true) return false; //队列空 x = elements[front]; //返回队头元素 return true; } * mayan 第三章 栈和队列 --循环队列的函数实现 //输出队列中的元素 template class T void SeqQueueT::display() const { int i; if (IsEmpty() == true) return ; //队列空 cout 队列中的元素为: endl; for(i =
显示全部