设为首页 加入收藏

TOP

顺序队列 Queue(一)
2013-11-20 14:24:18 来源: 作者: 【 】 浏览:376
Tags:顺序 队列   Queue

  顺序队列各种基本运算算法的实现

  顺序队列是较为普遍的一种队列实现方式,采用环状数组来存放队列元素,并用两个变量分别指向队列的前端(front)和尾端(rear),往队列中加进或取出元素时分别改变这两个变量的计数(count)。

  队列中用环状数组存储数据(合理利用空间、减少操作),通过基本的append()将元素加入队列,serve()将元素移出队列,先进入的先移出,retieve得到最先加入队列的元素。此外在继承的Extended_queue()中我增加了empty()和serve_and_retrieve()的功能。

  【实验说明】

  我选择的题目:课本中Programming Projects 3.3 P1

  问题描述:Write a function that will read one line of input from the terminal. The input is supposed to consist of two parts separated by a colon ':'. As its results, your function should produce a single character as follows:

  N No colon on the line.

  L The left part(before the colon) is longer than the right.

  R The right part(after the colon) is longer than the left.

  D The left and right parts have the same length but are different.

  S The left and right are exactly the same.

  Examples:

  Use either a queue or an extended queue to keep track of the left part of the line while reading the right part.

  1.分析队列要实现的基本功能以及继承的类要拓展的功能从而确定基本成员函数——append(),serve(),retireve(),拓展队列中:empty(),serve_and_retrieve(),确定队列中以环形数组存储数据从而确定成员函数——Queue_entry entry[],count(记录队列中数据数量)

  2.编写队列的头文件及实现

  3.分析题目中结束程序并输出几种字母的条件,简略画出功能实现的流程图,编写程序。(具体思路见源代码注释)

  4.简单测试程序的几种情况,分析需要改进的地方

  【相关代码】

  queue.h

  [cpp] view plaincopyprint

  #ifndef QUEUE_H

  #define QUEUE_H

  const int maxqueue=10;

  enum Error_code {success,overflow,underflow};

  typedef char Queue_entry ;

  class Queue{

  public:

  Queue();

  bool empty() const;

  Error_code append(const Queue_entry &item);

  Error_code serve();

  Error_code retrieve(Queue_entry &item)const;

  protected:

  int count;

  int front,rear;

  Queue_entry entry[maxqueue];

  };

  class Extended_queue:public Queue{

  public:

  bool full()const;

  int size()const;

  void clear();

     

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇顺序栈 Stack 下一篇通过二叉树和一个数找到路径

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)