跟我学数据结构:(4) 栈 (Stack)

来源:互联网 发布:js记录按钮点击次数 编辑:程序博客网 时间:2024/05/29 12:28

 

跟我学数据结构: (4)栈
摘要:
本文通过实际代码,演示了用C++实现数据实现的固定容量的栈
读者对象
   C++有一定基础的同学。
 
栈虽然简单,但在数据结构中非常重要。在计算机算法世界里,很多场合中都是利用栈巧妙解决问题的。
 
设计说明
本文的代码有三部分组成。
1) Stack.h, Stack.cpp
我们用c++封装了栈的实现。
2) Main.cpp
主函数构造了一个Stack_T 对象的实例stack。程序运行后,在界面上要求用户输入名字。系统把用户输入的字符放进栈中。然后在依次出栈,完成了字符串的顺序的反转。
3) Makefile
之前的示例我们都只有一个文件,直接通过 g++ 进行编译。本例特地将一个文件可以实现的代码根据逻辑划分成多个,编写makefile脚本,用gnu make来编译程序。 这样,你可以了解用makefile来管理多个文件组成的项目。
 
代码和运行结果
本文件的代码已提交至svn中。
 
svn  co http://cpp-csdn.googlecode.com/svn/trunk/data-structure/stack/
 
stack.h
Code:
  1. //////////////////////////////////////////////////////////////////////////   
  2. // file: stack.h   
  3. // auth: jiangtao <jiangtao@tao-studio.net>   
  4. // date: 2009-6-3   
  5. //   
  6. //////////////////////////////////////////////////////////////////////////   
  7.   
  8. #ifndef stack_h__   
  9. #define stack_h__   
  10.   
  11. // The stack consists of Element_T   
  12. typedef int Element_T;   
  13.   
  14. class Stack_T   
  15. {   
  16. public:   
  17.     // Initialize a new stack so that it is empty.   
  18.     Stack_T (void);   
  19.   
  20.     // Copy constructor   
  21.     Stack_T (const Stack_T& s);   
  22.   
  23.     // Assignment operator   
  24.     Stack_T& operator = (const Stack_T& s);   
  25.   
  26.     //Perform actions when stack is destroyed.   
  27.     ~Stack_T (void);   
  28.   
  29.     // Place a new item on top of the stack   
  30.     // Does not check if the stack is full.   
  31.     void push (Element_T newItem);   
  32.   
  33.     // Remove and return the top stack item   
  34.     // Dose not check if stack is empty.   
  35.     Element_T pop (void);   
  36.   
  37.   
  38.     // Return top stack item without removing it   
  39.     // Does not check if stack is empty.   
  40.     Element_T top (void);   
  41.   
  42.     // Returns 1 if the stack is empty,   
  43.     // otherwise returns 0.   
  44.     int isEmpty (void);   
  45.   
  46.     // Returns 1 if stack full, else returns 0   
  47.     int isFull ( void);   
  48.   
  49. private:   
  50.     // bottom and maximun size of the stack   
  51.     enum    
  52.     {   
  53.        BOTTOM = 0,   
  54.        SIZE  = 100,   
  55.     };   
  56.   
  57.     // Keeps track of the current top of stack.   
  58.     int stackTop_;   
  59.   
  60.     // Holds the stack's contents.   
  61.     Element_T stack_[SIZE];   
  62.   
  63. };   
  64. #endif // stack_h__   
stack.cpp
Code:
  1. //////////////////////////////////////////////////////////////////////////   
  2. // file: stack.cpp   
  3. // auth: jiangtao <jiangtao@tao-studio.net>   
  4. // date: 2009-6-3   
  5. //   
  6. //////////////////////////////////////////////////////////////////////////   
  7.   
  8. #include "stack.h"   
  9.   
  10. Stack_T::Stack_T(void)   
  11. : stackTop_ (Stack_T::BOTTOM)   
  12. {   
  13.     //no-op   
  14. }   
  15.   
  16. Stack_T::Stack_T(const Stack_T& s)   
  17. : stackTop_ (s.stackTop_)   
  18. {   
  19.     for (int i = 0; i < s.stackTop_; i++)   
  20.     {   
  21.         this->stack_[i] = s.stack_[i];   
  22.     }   
  23. }   
  24.   
  25. Stack_T&  Stack_T::operator = (const Stack_T& s)   
  26. {   
  27.     if (this != &s)   
  28.     {   
  29.         this->stackTop_ = s.stackTop_;   
  30.         for (int i = 0; i < s.stackTop_; i++)   
  31.         {   
  32.             this->stack_[i] = s.stack_[i];   
  33.         }   
  34.     }   
  35.     return *this;   
  36. }   
  37.   
  38.   
  39. Stack_T::~Stack_T(void)    
  40. {   
  41.     //    
  42. }   
  43.   
  44. void Stack_T::push(Element_T newItem)   
  45. {   
  46.     this->stack_[this->stackTop_++] = newItem;   
  47. }   
  48.   
  49. Element_T Stack_T::pop(void)   
  50. {   
  51.     return this->stack_[-- this->stackTop_];   
  52. }   
  53.   
  54. Element_T Stack_T::top(void)   
  55. {   
  56.     return this->stack_[this->stackTop_ - 1];   
  57. }   
  58.   
  59. int Stack_T::isEmpty(void)   
  60. {   
  61.     return this->stackTop_ == Stack_T::BOTTOM;   
  62. }   
  63.   
  64. int Stack_T::isFull(void)   
  65. {   
  66.     return this->stackTop_ >= Stack_T::SIZE;   
  67. }   
  68.   
main.cpp
Code:
  1. //////////////////////////////////////////////////////////////////////////   
  2. // file: main   
  3. // auth: jiangtao <jiangtao@tao-studio.net>   
  4. // date: 2009-6-3   
  5. //   
  6. //////////////////////////////////////////////////////////////////////////   
  7. #include "stack.h"   
  8. #include <iostream>   
  9. using namespace std;   
  10.   
  11. int main (int argc, char* argv[])   
  12. {   
  13.     cout << argv[0] << " start here." << endl;   
  14.     const int MAX_NAME_LEN = 64;   
  15.     char name[MAX_NAME_LEN];   
  16.   
  17.     Stack_T stack;   
  18.   
  19.     cout << " Please enter you name. :";   
  20.     cin.getline(name,MAX_NAME_LEN);   
  21.   
  22.     for (int i = 0; name[i] != '/0' && !stack.isFull(); i++)   
  23.     {   
  24.         stack.push(Element_T (name[i]));   
  25.     }   
  26.   
  27.     cout << "/nyour name backwards is ..:";   
  28.     while (!stack.isEmpty())   
  29.     {   
  30.         cout << char (stack.pop());   
  31.     }   
  32.     cout << endl;   
  33.     return 0;   
  34.   
  35. }   
 
makefile
 
Code:
  1. CC=g++   
  2. CFLAGS=-O2 -W    
  3. LIBS=   
  4.   
  5. stack: main.cpp stack.cpp   
  6.     $(CC) $(CFLAGS) -c main.cpp stack.cpp   
  7.     $(CC) main.o stack.o $(LIBS) -o stack   
  8. clean:   
  9.     rm *.o   
 
Code:
  1. 输出结果   
  2.   
  3. ./stack start here.   
  4.  Please enter you name. : jiangtao   
  5. your name backwards is ..:oatgnaij   

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 公司核名下来了不想注册了怎么办 家人受到小贷公司催款威胁怎么办 商标抽签资料提交上去有问题怎么办 花椒直播助手苹果版下载不了怎么办 在香港酒店住把床单弄上血了怎么办 综英美我能怎么办我也很绝望百度云 护照的名字中间有个空格怎么办 开车不小心压死黄鼠狼了怎么办 三户联保贷款一方不还怎么办 因为隔断中介违约…我该怎么办 上海居住证没下来换住址了怎么办 工商注册后大股东不注资怎么办 公司不给去办理变更股东信息怎么办 滴滴车主注册没有自己的车型怎么办 代办用虚假地址注册的公司怎么办? 写字楼注册公司租户不租了怎么办 租户没把公司迁出我该怎么办 租户不肯把户口迁出了业主怎么办 同片区个体户营业场所搬迁怎么办 个体领发票的本丢了怎么办 领房产证发票和合同丢了怎么办 税务登记证5年没有办怎么办 二证合一后税务登记证怎么办? 微信漂流瓶打招呼对方收不到怎么办 添加不上徽信好友的微信帐号怎么办 手机号码不用了微信密码忘了怎么办 别人用我的手机号码注册微信怎么办 电脑此网站的安全证书有问题怎么办 起诉离婚开庭时被告不到场怎么办 商标35类被别人注册了怎么办 商标被注销后被别人注册怎么办 电商35类商标被抢注怎么办 血小板太低怎么办可以吃水果吗? 微信好友删除了只记得昵称怎么办 优酷会员1080p很卡怎么办 电脑最下面的任务栏不显示怎么办 ea账号保留的问题忘了怎么办 微博超级话题账号被屏蔽怎么办 梦幻西游手机将军令号码换了怎么办 文档的内容被锁定无法修改怎么办 用户没有权限访问u盘时该怎么办?