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;}

这里写图片描述

原创粉丝点击