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