学习C++——实现抽象数据类型——栈

来源:互联网 发布:中国消防网站域名 编辑:程序博客网 时间:2024/06/07 01:19

一、本例实现了一个比较简单的栈,用数组实现的栈空间。

  • 创建栈空间
  • 将数据项添加到栈顶
  • 从栈顶弹出数据项
  • 查看栈是否填满
  • 查看栈是否为空

源代码

#ifndef STACK_H_#define STACK_H_typedef unsigned long Item;class Stack{private:enum { MAX = 10 };Item items[MAX];int top;public:Stack();~Stack();bool isempty() const;bool isfull() const;bool push(const Item & item);bool pop(Item & item);};#endif
#include "stack.h"/*Stack();~Stack();bool isempty() const;bool isfull() const;bool push(const Item & item);bool pop(Item & item);*/Stack::Stack(){top = 0;}bool Stack::isempty() const{if (top == 0)return true;elsereturn false;}bool Stack::isfull() const{if (top == MAX)return true;elsereturn false;}bool Stack::push(const Item & item){if(top < MAX){//top++;items[top++] = item;return true;}elsereturn false;}bool Stack::pop(Item & item){if(top > 0){item = items[--top];//top--;return true;}elsereturn false;}Stack::~Stack(){}

#include "stack.h"#include <iostream>#include <cctype>int main(){using namespace std;Stack st;char ch;unsigned long po;cout << "Enter A/a to add a new data," << "P/p to pop a data to po, or Q/q to quit.\n";while(cin >> ch && toupper(ch) != 'Q'){while(cin.get() != '\n')continue;if(!isalpha(ch)){cout << "\a";continue;}switch(ch){case 'A':case 'a':cout << "Add a new data to the stack:";cin >> po;if(st.isfull())cout << "The stack is full.\n";elsest.push(po);break;case 'P':case 'p':if(st.isempty())   cout << "The stack is empty,please push the data.\n";   else   {   st.pop(po);   cout << "Po = " << po << endl;   }break;}cout << "\nPlease enter A/a to add new data,enter P/p to pop a data.\n";}cout << "Bye.\n";cin.get();return 0;}

二、本例用动态数组实现栈

源代码

#ifndef STACK_H_#define STACK_H_typedef unsigned long Item;class Stack{private://enum { MAX = 10 };//Item items[MAX];int MAX;Item * pitem;int top;public:Stack();~Stack();bool isempty() const;bool isfull() const;bool push(const Item & item);bool pop(Item & item);};#endif

#include "stack.h"#include <iostream>using namespace std;/*Stack();~Stack();bool isempty() const;bool isfull() const;bool push(const Item & item);bool pop(Item & item);*/Stack::Stack(){top = 0;cout << "Please enter the MAX:";//输入栈的高度,也就是可以存数据的数目cin >> MAX;pitem = new Item[MAX];//动态分配内存空间,也就是分配栈空间}bool Stack::isempty() const{if (top == 0)return true;elsereturn false;}bool Stack::isfull() const{if (top == MAX)return true;elsereturn false;}bool Stack::push(const Item & item){if(top < MAX){//top++;pitem[top++] = item;return true;}elsereturn false;}bool Stack::pop(Item & item){if(top > 0){item = pitem[--top];//top--;return true;}elsereturn false;}Stack::~Stack(){delete [] pitem;//释放动态分配的内存}

#include "stack.h"#include <iostream>#include <cctype>int main(){using namespace std;Stack st;char ch;unsigned long po;cout << "Enter A/a to add a new data," << "P/p to pop a data to po, or Q/q to quit.\n";while(cin >> ch && toupper(ch) != 'Q'){while(cin.get() != '\n')continue;if(!isalpha(ch)){cout << "\a";continue;}switch(ch){case 'A':case 'a':cout << "Add a new data to the stack:";cin >> po;if(st.isfull())cout << "The stack is full.\n";elsest.push(po);break;case 'P':case 'p':if(st.isempty())   cout << "The stack is empty,please push the data.\n";   else   {   st.pop(po);   cout << "Po = " << po << endl;   }break;}cout << "\nPlease enter A/a to add new data,enter P/p to pop a data.\n";}cout << "Bye.\n";cin.get();return 0;}


0 0
原创粉丝点击