1 头文件Stack.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 = ;
} void Stack::push(void* dat)
{
Link *newLink = new Link;
newLink->initialize(dat, head);
head = newLink;
} void* Stack::peek()
{
require(head !=, "Stack empty");
return head->data;
} void* Stack::pop()
{
if (head == )
return ;
void* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;
} void Stack::cleanup()
{
require(head == , "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, ); ifstream in(argv[]);
assure(in, argv[]); Stack textLines;
textLines.initialize(); string line;
while (getline(in, line)) //文件以行为单位入栈
textLines.push(new string(line)); string* s;
while ((s = (string*) textLines.pop()) != ) //出栈
{
cout << *s << endl;
delete s;
} textLines.cleanup();
return ;
}

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();
}
} inline void requireArgs(int argc, int args, const std::string& msg = "Must use %d arguments")
{
using namespace std;
if (argc != args + )
{
fprintf(stderr, msg.c_str(), args);
fputs("\n", stderr);
exit();
}
} inline void requireMinArgs(int argc, int minArgs, const std::string& msg ="Must use at least %d arguments")
{
using namespace std;
if(argc < minArgs + )
{
fprintf(stderr, msg.c_str(), minArgs);
fputs("\n", stderr);
exit();
}
} 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();
}
} 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();
}
} #endif // REQUIRE_H

最新文章

  1. Default Parameter Values in Python
  2. Java Js实现导出csv
  3. Unity中Collider和刚体Collider性能对比
  4. http和网页设计
  5. ArcEngine和GDAL读写栅格数据机制对比(二)—— IPixelBlock读写栅格
  6. wordpress内存不足问题“Fatal error:out of memoryin etc...”
  7. IE6下margin出现双边距
  8. .NET4安装总进度一直不动的解决办法
  9. mysql 闪回表工具
  10. x01.AntWorld: An Python AI Game
  11. Java正则表达式语法
  12. Android为TV端助力:UDP协议(接收组播和单播)
  13. ReactiveCocoa - study
  14. eclipse输入中文为繁体字
  15. 020 RDD的理解
  16. hdu 2191 悼念512汶川大地震遇难同胞 【多重背包】(模板题)
  17. Scaleform 中的 3D视角相关研究
  18. 使用Servlet发布WebService
  19. 在windows系统下安装oracle 11g
  20. shell基础 -- 基本正则表达式

热门文章

  1. web App libraries跟referenced libraries的一些问题
  2. css中background背景属性概述
  3. 2019-8-31-C#-对-byte-数组进行模式搜索
  4. 2018-12-27-WPF-从文件创建图片的方法
  5. OSGi教程:Resource API Specification
  6. PLAY2.6-SCALA(十一) 模板常用场景
  7. Leetcode771.Jewels and Stones宝石与石头
  8. react-jd-index
  9. Android Studio 如何引入.jar文件和.so文件?
  10. hdu4325 线段树 成段更新