代码拉取完成,页面将自动刷新
#include <iostream>
using namespace std;
class CStack
{
private:
char *s; //栈的内容保存在s中
int tp; //栈顶指示器,栈空为-1
int size;
public:
CStack(int initSize = 5);
CStack(const CStack &scopy);
CStack operator=(const CStack &scopy);
~CStack();
bool isEmpty();
bool isFull();
void push(char c);
char pop();
char top();
};
CStack CStack::operator=(const CStack &scopy)
{
if (this == &scopy) return *this;
if(s!=NULL) free(s);
size = scopy.size;
tp = scopy.tp;
s = NULL;
if (size!=0)
{
s = new char[size];
for (int i=0; i<=tp; i++)
s[i] = scopy.s[i];
}
return *this;
}
bool CStack::isEmpty()
{
return (tp==-1);
}
bool CStack::isFull()
{
return (tp==(size-1));
}
void CStack::push(char c)
{
if (isFull())
{
char *p = new char[2*size];
for (int i=0; i<size; i++)
p[i] = s[i];
delete []s;
s = p;
size = 2*size;
}
tp++;
s[tp] = c;
}
char CStack::pop()
{
int oldtp = tp;
tp--;
return s[oldtp];
}
char CStack::top()
{
return s[tp];
}
CStack::CStack(int initSize)
{
tp = -1;
size = initSize;
s = new char[size];
}
CStack::CStack(const CStack &scopy)
{
tp = scopy.tp;
size = scopy.size;
if (size!=0)
{
s = new char [size];
for (int i=0; i<=tp; i++)
s[i] = scopy.s[i];
}
}
CStack::~CStack()
{
if (s!=NULL)
{
delete []s;
s = NULL;
}
}
int main()
{
CStack s;
for(int i=0; i<7; i++) s.push('a'+i);
CStack sCopy;
sCopy = s;
while(!s.isEmpty()) cout << s.pop();
cout << endl;
for(int i=0; i<7; i++) s.push('1'+i);
while(!sCopy.isEmpty()) cout<<sCopy.pop();
cout<<endl;
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。