C++数据结构用一个变量或一个临时栈实现栈的复制
来源:互联网 发布:网络热词2016及解释 编辑:程序博客网 时间:2024/06/05 11:25
本人还是个在校大学生,只是想把自己平时打的一点点代码拿出来分享一下。如果代码有误或者还有可以改进的地方,请多多指教!
下面为 arrstack.h
template <class T>class arrStack {public: // 栈的顺序存储 int mSize; // 栈中最多可存放的元素个数 T *st; // 存放栈元素的数组public: int top; // 栈顶位置,应小于mSize public: // 栈的运算的顺序实现 arrStack(int size); //构造函数 arrStack(); //默认构造函数 void clear(); //清除栈函数 bool push(T item); //入栈 bool pop(T & item); //出栈 bool getTop(T & item); //返回栈顶元素 bool isEmpty(); //判断栈是否为空 bool isFull(); //判断栈是否为满 void show(); //显示栈 void stackcopy(arrStack<T> & s); //用临时栈实现栈的复制 void variablecopy(arrStack<T> & s); //用一个变量实现栈的复制};
下面是arrstack.cpp
#include "stdafx.h"#include "arrstack.h"template <class T>arrStack<T>::arrStack(int size) { mSize = size; top = -1; //此处规定top = -1时,栈为空 st = new T[mSize];}template <class T>arrStack<T>::arrStack() { top = -1;}template <class T>void arrStack<T>::clear() { top = -1;}template <class T>bool arrStack<T>::push(T item) { if (isFull()) { cout << "栈满溢出" << endl; return false; } else { st[++top] = item; return true; }}template <class T>bool arrStack<T>::pop(T & item) { if (isEmpty()) { cout << "栈为空,不能出栈操作" << endl; return false; } else { item = st[top--]; return true; }}template <class T>bool arrStack<T>::getTop(T & item) { if (top == -1) { cout << " 栈为空,不能出栈操作" << endl; return false; } else { *item = st[top]; return true; }}template <class T>bool arrStack<T>::isEmpty() { if (top <= -1) { return true; } return false;}template <class T>bool arrStack<T>::isFull() { if (top >= mSize - 1) { return true; } return false;}template <class T>void arrStack<T>::stackcopy(arrStack<T> & s) { arrStack<T> temp(this->mSize); T t; int i = 0; int index = this->top + 1; while (i < index) { this->pop(t); temp.push(t); i++; } i = 0; while (i < index) { temp.pop(t); this->push(t); s.push(t); i++; }}template <class T>void arrStack<T>::show() { if (isEmpty()) { cout << "show(): 空栈!!" << endl; return; } for (int i = top; i > -1; i--) { cout << "NO." << i + 1 << " is: " << st[i] << endl; } cout << endl;}template <class T>void arrStack<T>::variablecopy(arrStack<T> & s) { int data; int temp; int index = this->top; for (int i = 0; i <= index; i++) { for (int j = index - i; j > 0; j--) { if (this->pop(temp)) { s.push(temp); } } this->pop(data); for (int k = index - i; k > 0; k--) { if (s.pop(temp)) { this->push(temp); } } s.push(data); }}
下面为测试代码
#include "stdafx.h"#include "arrstack.cpp"#include <iostream>using namespace std;int main() { arrStack<int> s1(5); arrStack<int> s2(5); for (int i = 1; i < 6; i++) { s1.push(i * 11); } cout << "original s1.show :" << endl; s1.show(); cout << "this is stackcopy:" << endl; s1.stackcopy(s2); cout << "s2.show :" << endl; s2.show(); system("pause"); return 0;}
阅读全文
0 0
- C++数据结构用一个变量或一个临时栈实现栈的复制
- 用一个临时变量实现单链表逆序
- c语言----交换a,b的值(分别用临时变量,加减法,异或实现)
- 面试题:用一个临时变量实现单链表逆序
- 用C/C++实现一个存储整形数据的栈数据结构
- C-一个栈的实现
- 数据结构5 用一个栈实现另一个栈的排序
- 【数据结构】用两个队列实现一个栈
- 用C语言实现在一个链表删除指定的一个或多个元素
- 【规范】C语言中定义变量只能在一个函数或一个块的开始吗?
- 不允许创建临时变量,交换两个数的内容(用异或实现)
- 一个引用相关问题,延长临时变量的生命周期
- 栈的基本操作(附带一个数据结构、C语言版)
- C语言一个栈的实现
- c语言一个栈的实现
- C语言一个栈的实现
- 【C++】一个简单栈的实现
- C语言一个栈的实现
- Linux中SetUID(下)
- 手工编写第一个Java Web程序
- Linux中SetGID
- 2.1标识符与关键字
- 搭建Java Web开发环境——Tomcat
- C++数据结构用一个变量或一个临时栈实现栈的复制
- Qt--QObject
- Linux中Sticky BIT
- Linux中不可改变位权限
- pat乙级真题 1012. 数字分类 (20)
- 网络请求框架总结
- Tomcat的WEB-INF目录详解
- 《Android中级工程师》BroadcastReceive工作流程
- MyEclipse开发Java Web程序