设为首页 加入收藏

TOP

顺序栈 Stack(一)
2013-11-20 14:24:23 来源: 作者: 【 】 浏览:364
Tags:顺序   Stack

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

  栈是只能在某一端插入和删除的特殊线性表。它按照后进先出的原则存储数据,先进入的数据被压入栈底(push),最后的数据在栈顶(top),需要读数据的时候从栈顶开始弹出数据(top)最后一个数据被第一个读出来。

  栈中数据用数组储存,通过top(),push(),pop()基本的函数用以实现其功能,此外我还增加了clear()函数用以清除栈中现有的所有元素

  【实验说明】

  我选择的题目:书中calculator的编写与改进

  1.分析栈要实现的功能从而确定栈类的几种基本成员函数——pop(),pop(),top(),clear(),确定栈中以数组实现数据的存储从而确定栈的成员函数——Stack_entry entry[],count(记录栈中数据数量)

  2.编写栈的头文件及实现

  3.选择书中计算器的程序以验证栈的各种基本运算。分析书中算法思路及对栈的运用,提出可以改进的地方——将计算器功能封装到类中,并增加清空栈的功能。

  4.编写计算器类的头文件及类的实现

  5.主函数中通过简单的创建MyClaculator验证程序,并试验各种基本功能。

  【相关代码】

  Stack.h

  [cpp] view plaincopyprint

  #ifndef STACK_H

  #define STACK_H

  enum Error_code{success,underflow,overflow};

  const int stackmax=10;

  typedef double Stack_entry;

  class Stack{

  public:

  Stack();

  bool empty() const;

  Error_code pop();

  Error_code push(const Stack_entry &item);

  Error_code top(Stack_entry &item) ;

  Error_code clear();

  private:

  Stack_entry entry[stackmax];

  int count;

  };

  #endif

  #ifndef STACK_H

  #define STACK_H

  enum Error_code{success,underflow,overflow};

  const int stackmax=10;

  typedef double Stack_entry;

  class Stack{

  public:

  Stack();

  bool empty() const;

  Error_code pop();

  Error_code push(const Stack_entry &item);

  Error_code top(Stack_entry &item) ;

  Error_code clear();

  private:

  Stack_entry entry[stackmax];

  int count;

  };

  #endif

  Stack.cpp

  [cpp] view plaincopyprint

  #include"stack.h"

  //implemention

  //Stack()

  Stack::Stack(){

  count=0;

  }

  //pop()

  Error_code Stack::pop(){

  Error_code outcome=success;

  if(count==0)

  outcome=underflow;

  else

  count--;

  return outcome;

  }

     

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++和MATLAB混合编程 下一篇顺序队列 Queue

评论

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

·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)