📄 clinkstack.cpp
字号:
//clinkstack.cpp
//链栈类的实现
#include <iostream>
using namespace std;
#include "CLinkStack.h"
#include "CNode.h"
CLinkStack::CLinkStack()
{
top=NULL;
}
CLinkStack::CLinkStack(const CLinkStack &s) //
{
CNode *temp1,*temp2,*temp3;
temp1=temp2=NULL;
if(s.top==NULL)
{
top=NULL;
}
else
{
temp1=s.top;
temp2=new CNode;
if(temp2==NULL)
{
return;
}
temp2->setData(temp1->getData());
top=temp3=temp2;
temp1=temp1->getNext();
while(temp1)
{
temp2=new CNode;
if(temp2==NULL)
{
return;
}
temp2->setData(temp1->getData());
temp3->setNext(temp2);
temp3=temp3->getNext();
temp1=temp1->getNext();
}
}
}
CLinkStack::~CLinkStack()
{
makeEmpty();
}
bool CLinkStack::push(CNode &n) //
{
CNode *temp;
temp=new CNode;
if(temp==NULL)
{
return false;
}
temp->setData(n.getData());
temp->setNext(top);
top=temp;
return true;
}
bool CLinkStack::pop(CNode &n) //
{
CNode *temp;
if(top==NULL)
{
n.setData('\0');
return false;
}
temp=top;
top=top->getNext();
n.setData(temp->getData());
delete temp;
return true;
}
bool CLinkStack::isEmpty(void) //
{
if(top==NULL)
{
return true;
}
return false;
}
void CLinkStack::makeEmpty(void) //
{
CNode *temp;
while(top)
{
temp=top;
top=top->getNext();
delete temp;
}
}
void CLinkStack::print(void) //
{
CNode *temp;
int i,j;
if(top==NULL)
{
cout<<"(empty)"<<endl;
return;
}
for(i=0,temp=top;temp!=NULL;temp=temp->getNext())
{
i++;
}
for(;i!=0;i--)
{
for(j=0,temp=top;j<i-1;j++)
{
temp=temp->getNext();
}
cout<<temp->getData();
}
cout<<endl;
}
bool CLinkStack::operator==(const CLinkStack &s) //
{
CNode *temp1,*temp2;
if(top==NULL&&s.top==NULL)
{
return true;
}
temp1=top;
temp2=s.top;
for(;temp1!=NULL;temp1=temp1->getNext(),temp2=temp2->getNext())
{
if(temp1->getData()!=temp2->getData())
{
return false;
}
}
return true;
}
void CLinkStack::operator=(const CLinkStack &s)
{
CNode *temp1,*temp2,*temp3;
makeEmpty();
temp1=temp2=NULL;
if(s.top==NULL)
{
top=NULL;
}
else
{
temp1=s.top;
temp2=new CNode;
if(temp2==NULL)
{
return;
}
temp2->setData(temp1->getData());
top=temp3=temp2;
temp1=temp1->getNext();
while(temp1)
{
temp2=new CNode;
if(temp2==NULL)
{
return;
}
temp2->setData(temp1->getData());
temp3->setNext(temp2);
temp3=temp3->getNext();
temp1=temp1->getNext();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -