zoukankan      html  css  js  c++  java
  • 线性表-顺序队列(循环队列)

      1: //sqqueue.cpp
    
      2: 
    
      3: #include "sqqueue.h"
    
      4: 
    
      5: SqQueue::SqQueue()
    
      6: {
    
      7:   front = 0;
    
      8:   rear = 0;
    
      9: }
    
     10: 
    
     11: SqQueue::~SqQueue()
    
     12: {
    
     13: 
    
     14: }
    
     15: 
    
     16: void  SqQueue::ClearQueue()
    
     17: {
    
     18:   front = 0;
    
     19:   rear = 0;
    
     20: }
    
     21: 
    
     22: bool  SqQueue::IsEmpty()
    
     23: {
    
     24:   return ( rear == front );
    
     25: }
    
     26: 
    
     27: void  SqQueue::EnQueue(int item)
    
     28: {
    
     29:   if( (rear + 1) % MAXSIZE == front )
    
     30:   {
    
     31:     cerr<<"queue is full"<<endl;
    
     32:     exit(1);
    
     33:   }
    
     34:   else
    
     35:   {
    
     36:     data[rear] = item;
    
     37:     rear = (rear + 1) % MAXSIZE;
    
     38:   }
    
     39: }
    
     40: 
    
     41: void  SqQueue::DeQueue()
    
     42: {
    
     43:   if(rear == front)
    
     44:   {
    
     45:     cerr<<"queue is empty."<<endl;
    
     46:     exit(1);
    
     47:   }
    
     48:   else
    
     49:   {
    
     50:     front = (front + 1) % MAXSIZE;
    
     51:   }
    
     52: }
    
     53: 
    
     54: int  SqQueue::GetLength()
    
     55: {
    
     56:   return (rear - front + MAXSIZE) % MAXSIZE;
    
     57: }
    
     58: 
    
     59: void  SqQueue::PrintQueue()
    
     60: {
    
     61:   int temp = front;
    
     62:   while(temp != rear)
    
     63:   {
    
     64:     cout<<data[temp++]<<endl;
    
     65:   }
    
     66: }
      1: //sqqueue.h
    
      2: 
    
      3: #ifndef SQQUEUE_H_H
    
      4: #define SQQUEUE_H_H
    
      5: 
    
      6: #include <iostream>
    
      7: #define MAXSIZE 10
    
      8: 
    
      9: using namespace std;
    
     10: 
    
     11: class SqQueue
    
     12: {
    
     13:   int data[MAXSIZE];
    
     14:   int front;
    
     15:   int rear;
    
     16: public:
    
     17:   SqQueue();
    
     18:   ~SqQueue();
    
     19:   void ClearQueue();
    
     20:   bool IsEmpty();
    
     21:   void EnQueue(int item);
    
     22:   void DeQueue();
    
     23:   int GetLength();
    
     24:   void PrintQueue();
    
     25: };
    
     26: 
    
     27: #endif
      1: //test.cpp
    
      2: #include "sqqueue.h"
    
      3: 
    
      4: #include <iostream>
    
      5: 
    
      6: using namespace std;
    
      7: 
    
      8: int main(int argc, char *argv[])
    
      9: {
    
     10:   SqQueue * queue = new SqQueue;
    
     11:   queue->EnQueue(1);
    
     12:   queue->EnQueue(2);
    
     13:   queue->EnQueue(3);
    
     14:   queue->EnQueue(4);
    
     15:   queue->EnQueue(5);
    
     16:   queue->PrintQueue();
    
     17:   
    
     18:   queue->DeQueue();
    
     19:   queue->PrintQueue();
    
     20:   queue->DeQueue();
    
     21:   queue->PrintQueue();
    
     22:   queue->DeQueue();
    
     23:   queue->PrintQueue();
    
     24:   if(queue->IsEmpty())
    
     25:     cout<<"queue is empty."<<endl;
    
     26:   queue->ClearQueue();
    
     27:   queue->PrintQueue();
    
     28:   if(queue->IsEmpty())
    
     29:     cout<<"queue is empty."<<endl;
    
     30:   cout<<"length is "<<queue->GetLength()<<endl;
    
     31:   
    
     32:   return 0;
    
     33: }
    作者:Lucas Hsueh
    文章部分是自己的学习体会、代码等,还有收集和整理其他技术牛人的文章。
  • 相关阅读:
    最终项目 XMessenger Servers
    Linux下patch的制作和应用
    谈移动互联网入口
    绑定服务后台播放音频的简单示例
    MOQL操作数(Operand) (三)
    浅析Hibernate映射(二)——关系映射(3)
    【Extjs优化二】 Form表单提交通用
    Delphi Dll(1)
    用Groovy思考 第三章 Groovy开发环境
    JUnit单元测试入门(三)--JUnit简单实例
  • 原文地址:https://www.cnblogs.com/lucas-hsueh/p/3711340.html
Copyright © 2011-2022 走看看