下推栈实现(c++编程思想 p136)
2015-06-08 15:20
513 查看
1 头文件Stack.h
2 实现文件Stack.cpp
3 测试文件main.cpp
4 运行分析
附 辅助测试工具require.h
#ifndef STACK_H #define STACK_H struct Stack { struct Link { void* data; Link* next; void initialize(void* dat, Link* nxt); }* head; void initialize(); void push(void* dat); void* peek(); void* pop(); void cleanup(); }; #endif // STACK_H
2 实现文件Stack.cpp
#include "Stack.h" #include "../require.h" using namespace std; void Stack::Link::initialize(void* dat, Link* nxt) { data = dat; next = nxt; } void Stack::initialize() { head = 0; } void Stack::push(void* dat) { Link *newLink = new Link; newLink->initialize(dat, head); head = newLink; } void* Stack::peek() { require(head !=0, "Stack empty"); return head->data; } void* Stack::pop() { if (head == 0) return 0; void* result = head->data; Link* oldHead = head; head = head->next; delete oldHead; return result; } void Stack::cleanup() { require(head == 0, "Stack not empty"); }
3 测试文件main.cpp
#include <iostream> #include <fstream> #include <string> #include "Stack.h" #include "../require.h" using namespace std; int main(int argc, char* argv[]) { requireArgs(argc, 1); ifstream in(argv[1]); assure(in, argv[1]); Stack textLines; textLines.initialize(); string line; while (getline(in, line)) //文件以行为单位入栈 textLines.push(new string(line)); string* s; while ((s = (string*) textLines.pop()) != 0) //出栈 { cout << *s << endl; delete s; } textLines.cleanup(); return 0; }
4 运行分析
附 辅助测试工具require.h
#ifndef REQUIRE_H #define REQUIRE_H #include <cstdio> #include <cstdlib> #include <fstream> #include <string> inline void require(bool requirement, const std::string& msg = "Requirement failed") { using namespace std; if (!requirement) { fputs(msg.c_str(), stderr); fputs("\n", stderr); exit(1); } } inline void requireArgs(int argc, int args, const std::string& msg = "Must use %d arguments") { using namespace std; if (argc != args + 1) { fprintf(stderr, msg.c_str(), args); fputs("\n", stderr); exit(1); } } inline void requireMinArgs(int argc, int minArgs, const std::string& msg ="Must use at least %d arguments") { using namespace std; if(argc < minArgs + 1) { fprintf(stderr, msg.c_str(), minArgs); fputs("\n", stderr); exit(1); } } inline void assure(std::ifstream& in, const std::string& filename = "") { using namespace std; if(!in) { fprintf(stderr, "Could not open file %s\n", filename.c_str()); exit(1); } } inline void assure(std::ofstream& out, const std::string& filename = "") { using namespace std; if(!out) { fprintf(stderr, "Could not open file %s\n", filename.c_str()); exit(1); } } #endif // REQUIRE_H
相关文章推荐
- 【Minimum Window】cpp
- 关于C#中调用C++编写的DLL(非托管的DLL)的实现和问题记录
- C++ ofstream和ifstream详细用法
- 用c++写的数字阅读软件!
- VC++ 里面的_tmain 和 main的区别
- C++ this 指针 浅析
- C++9.5 vector/list/queue容器的区别与选用
- 一个同学的c++的故事(推测c++的由来)(一)
- 面向对象程序设计-C++ Stream & Template & Exception【第十五次上课笔记】
- NYOJ 53 无聊的小明
- C++位操作符
- C++ Primer 学习笔记_2_快速入门(续)
- C++ Primer 学习笔记_1_快速入门
- C++ Primer 学习笔记_5_变量和基本类型(续2)
- MySQL的C++封装
- C++ Primer 学习笔记_4_变量和基本类型(续1)
- C++ Primer 学习笔记_3_变量和基本类型
- c/c++ define用法
- 代码,用c++实现线性链表
- 代码,用c++实现线性链表