C++ 面向对象编程:从栈到文件处理的全面解析
1. 栈与链表
栈在许多应用中非常有价值,并且可以用链表来实现。栈的操作包括在栈顶添加值( push
)、检查或移除栈顶值( pop
)以及检查栈是否为空。但我们无法对非栈顶的值进行操作。
1.1 栈操作示例
Stack stack;
stack.Push(1);
stack.Push(2);
stack.Push(3);
1.2 类定义
Cell.h
class Cell;
typedef Cell* Link;
class Cell
{
public:
Cell(int iValue, Cell* pNextCell);
int& Value() {return m_iValue;}
const int Value() const {return m_iValue;}
Link& NextLink() {return m_pNextLink;}
const Link NextLink() const {return m_pNextLink;}
private:
int m_iValue;
Link m_pNextLink;
};
Cell.cpp
#include "Cell.