??????д??????????????????к??д???????????????????
CStock & CStock::operator =(const CStock & otherStock)
{
CStock tempStock;
Item * copyElement = otherStock.p_Top;
//???otherStock????????????ν???tempStock???????λ??????otherStock????
while(copyElement)
{
tempStock.push(copyElement->data);
copyElement = copyElement->p_next;
}
copyElement = tempStock.p_Top;
//??tempStock????????????ν???????????λ?ú?otherStock?????
while(copyElement)
{
push(copyElement->data);
copyElement = copyElement->p_next;
}
return *this;
}
//???????
void CStock::push(elemType x)
{
Item * pushElement = new Item;
pushElement->data = x;
if(!p_Top)//?????????
{
p_Top = pushElement;
pushElement->p_next = NULL;
}
else //?????????
{
pushElement->p_next = p_Top;
p_Top = pushElement;
}
}
//???????????????????????????x???????????????????????????false;
bool CStock::pop(elemType &x)
{
if(!p_Top)
{
return false;
}
x = p_Top->data;
p_Top = p_Top->p_next;
return true;
}
//??????????
bool CStock::isEmpty() const
{
return p_Top == NULL;
}
//??????????????
void CStock::clear()
{
Item * deleteElement;
while(p_Top)
{
deleteElement = p_Top;
p_Top = p_Top->p_next;
delete deleteElement;
}
}
//???????С
int CStock::size() const
{
int length = 0;
Item *temp = p_Top;
while(temp && ++length)
{
temp = temp->p_next;
}
return length;
}
//?????????
void CStock::print() const
{
int count = 0;
Item * temp = p_Top;
while(temp && ++count)
{
cout << temp->data << " ";
if(count % 5 == 0)
cout << endl;
temp = temp->p_next;
}
}
//????????????????????????????????????????
CStock::~CStock(void)
{
Item * deleteElement;
while(p_Top)
{
deleteElement = p_Top;
p_Top = p_Top->p_next;
delete deleteElement;
}
}