64,648
社区成员
发帖
与我相关
我的任务
分享
大概看了一下,lz的问题嘛:
就在这个函数上:
linkstack<T>& reverse_stack()const;//返回当前栈的逆序栈;
修改方法:
第一:
linkstack(const linkstack<T> &)
{
//实现你的复制构造函数
}
然后将函数改为:
linkstack<T> reverse_stack()const;//返回当前栈的逆序栈;
{
linkstack<T> _reverse;//这句话乃至这个函数应该怎样写?
node<T> *_temp;
_temp=top;
while(_temp)
{
_reverse.push(_temp->data);
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈temp_stack;
return _reverse;
}
第二:
我帮你改好了:
#include<iostream>
using namespace std;
#ifndef _linkstack_
#define _linkstack_
template<class T>
class linkstack;
template<class T>
class node
{
friend linkstack<T>;
private:
T data;
node *link;
};
template<class T>
class linkstack
{
private:
node<T> *top;
public:
linkstack(){top=0;}
~linkstack();
bool isempty()const{ return top==0;}
T pop() const;
void output() const;
linkstack<T>& push(const T & x);
linkstack<T>& s_delete(T& x);
void r_output() const; //实现倒叙输出;
linkstack<T>& operator=(const linkstack<T>& rightop);
linkstack<T>* reverse_stack()const;//返回当前栈的逆序栈;
};
template<class T>
linkstack<T>::~linkstack()
{
node<T> *next;
while(top)
{
next=top->link;
delete top;
top=next;
}
} // /*////
template<class T>
T linkstack<T>::pop()const
{
if(!isempty())
return top->data;
}
template<class T>
linkstack<T>& linkstack<T>::push(const T&x)
{
node<T> *pointer=new node<T>;
pointer->data=x;
pointer->link=top;
top=pointer;
return *this;
}
template<class T>
linkstack<T>& linkstack<T>::s_delete(T &x)
{
if(!isempty())
{
x=top->data;
node<T> *pointer=top;
top=top->link;
delete pointer;
return *this;
}
}
template<class T>
void linkstack<T>::output() const
{
node<T> *temp;
temp=top;
while(temp)
{
cout<<temp->data<<" ";
temp=temp->link;
}
cout<<endl;
}
template<class T>
void linkstack<T>::r_output() const
{
linkstack<T> _reverse_stack;
node<T> *_temp;
node<T> *_index; //_index是倒序的索引;
_temp=this->top;
while(_temp)
{
_reverse_stack.push(_temp->data);
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈;
_index=_reverse_stack.top;
while(_index)
{
cout<<_index->data<<" ";
_index=_index->link;
}
cout<<endl;
}
template<class T>
linkstack<T>& linkstack<T>::operator=(const linkstack<T>& rightop)
{//返回一个与rightop内容相同的栈;
if(this==&rightop)
return *this;
linkstack<T> temp_stack;
// linkstack<T> _reverse_stack;
node<T> *_temp;
node<T> *_this; //用来清空*this;
node<T> *_index; //_index是倒序的索引;
_temp=rightop.top;
while(_temp)
{
temp_stack.push(_temp->data);
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈temp_stack;
while(top)
{//清空*this的数据;
_this=top->link;
delete top;
top=_this;
}
_index=temp_stack.top;
while(_index)
{
this->push(_index->data);
_index=_index->link;
}
return *this;
}
template<class T>
linkstack<T>* linkstack<T>::reverse_stack()const
{
linkstack<T> *_reverse = new linkstack<T>();//这句话乃至这个函数应该怎样写?
node<T> *_temp ;//, *p_temp;
_temp=top;
while(_temp)
{
_reverse->push(_temp->data);
// p_temp = _temp;
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈temp_stack;
// _reverse->top = p_temp;
return _reverse;
} ////*/
#endif
int main()
{
linkstack<int> src;
for(int i=0;i<10;i++)
src.push(i);
src.output();
linkstack<int>* des=src.reverse_stack();
des->output();
delete des;
return 0;
}
template<class T>
class linkstack
{
private:
node<T> *top;
public:
linkstack(){top=0;}
linkstack(const linkstack<T>& rightop);
~linkstack();
bool isempty()const{ return top==0;}
T pop() const;
void output() const;
linkstack<T>& push(const T & x);
linkstack<T>& s_delete(T& x);
void r_output() const; //实现倒叙输出;
linkstack<T>& operator=(const linkstack<T>& rightop);
linkstack<T> reverse_stack()const;//返回当前栈的逆序栈;
};
template<class T>
linkstack<T>::linkstack(const linkstack<T>& rightop)
{
T x;
this->s_delete(x);
linkstack<T> temp_stack;
// linkstack<T> _reverse_stack;
node<T> *_temp;
node<T> *_this; //用来清空*this;
node<T> *_index; //_index是倒序的索引;
_temp=rightop.top;
while(_temp)
{
temp_stack.push(_temp->data);
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈temp_stack;
while(top)
{//清空*this的数据;
_this=top->link;
delete top;
top=_this;
}
_index=temp_stack.top;
while(_index)
{
this->push(_index->data);
_index=_index->link;
}
...
template<class T>
linkstack<T> linkstack<T>::reverse_stack()const
{
linkstack<T> _reverse;//这句话乃至这个函数应该怎样写?
node<T> *_temp;
_temp=top;
while(_temp)
{
_reverse.push(_temp->data);
_temp=_temp->link;
} //将当前栈里面的值倒序压入临时栈temp_stack;
return _reverse;
} ////*/
}