第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

如果不知道具體需要建多大的站,構(gòu)造函數(shù)怎么寫

如果不知道具體需要建多大的站,構(gòu)造函數(shù)怎么寫

正在回答

3 回答

根據(jù)老師寫了一個整個的程序,這個是申請物理空間的,因為物理空間很大一般不會不足,符合你的問題,所以可以按需要取多少大小。

不是很規(guī)范,有所不足互相學習哈

#include<iostream>
using?namespace?std;
#define?elemtype?char
struct?node?{
elemtype?data;
node?*last;
node?*next;
//新定義的結(jié)點的默認構(gòu)造函數(shù)?
node(const?elemtype?data?=?0,node?*last?=?NULL,node?*next?=?NULL)?{
this->data?=?data;
this->last?=?last;
this->next?=?next;
}
~node()?{
data?=?0;
last?=?next?=?NULL;
}
};
class?stack{
private:
node?*base;?//棧底?
node?*top;??//棧頂?
int?size;???//棧的實際大小?
public:
stack()?{
//申請一個哨兵結(jié)點作為棧底,哨兵的data沒有意義?
//如果申請物理空間失敗,拋出異常?
if?(!(base?=?new?node()))?{
cout?<<?"內(nèi)存不足!";
//throw?...
}
top?=?base;
size?=?0;
}
~stack()?{
node?*p=base,*q=NULL;
while(p)?{
q?=?p;
p?=?p->next;
delete?q;
}
}
bool?isfull()?{
//嘗試申請一個node,如果可以申請說明未滿?
node?*p=new?node;
if?(!p)?{
return?true;
}
else?{
delete?p;
return?false;
}
}
bool?isempty()?{
return?!size;
//也可以寫成?return?top?==?base;
}
void?push(const?elemtype?&c)?{
if?(isfull())?{
cout?<<?"滿了";
//throw?..?
}
node?*p?=?new?node(c);
top->next?=?p;
p->last?=?top;
top?=?p;
++size;
}
elemtype?pop()?{
if?(isempty())?{
cout?<<?"空了";
//theow?..
}
elemtype?x?=?top->data;
node?*p?=?top;
top?=?p->last;
top->next?=?NULL;
delete?p;
--size;
return?x;
}
//遍歷函數(shù),默認從棧底到棧頂?
void?traverse(bool?isfrombottom?=?true)?{
node?*p;
if?(isfrombottom)?{
p?=?base->next;
while?(p)?{
cout?<<?p->data;
p?=?p->next;
}
cout<<endl;
}
else?{
p?=?top;
while?(p?!=?base)?{
cout?<<?p->data;
p?=?p->last;
}
cout<<endl;
}
}?
int?lenth()?{
return?size;
}
void?clearStack()?{
node?*p?=?base->next,*q;
while?(p)?{
q?=?p;
p?=?p->next;
delete?q;
}
top?=?base;
size?=?0;
base->next?=?NULL;
}
};
int?main(void)?{
stack?*s?=?new?stack();
if?(s->isempty())
cout<<"棧為空"<<endl;
cout?<<?s->lenth()<<endl;
s->push('h');
s->push('e');
s->push('l');
s->push('l');
s->push('o');
cout?<<?s->lenth()<<endl;
s->traverse();
s->traverse(false);
s->clearStack();
cout?<<?s->lenth()<<endl;
cout?<<endl;
s->push('a');
s->traverse();
s->pop();
s->traverse();
//測試是不是能二次入棧?
s->push('a');
s->traverse();
s->pop();
s->traverse();
//因為內(nèi)存物理空間蠻大的,所以一般棧滿的情況不會出現(xiàn)?在此不做測試?
//if?(s->isfull())
//cout<<"棧為滿"<<endl;
s->clearStack();
//最后這句話送給大家
s->push('H');
s->push('a');
s->push('p');
s->push('p');
s->push('y');
s->push('?');
s->push('C');
s->push('h');
s->push('r');
s->push('i');
s->push('s');
s->push('t');
s->push('m');
s->push('a');
s->push('s');
s->push('!');
s->traverse();
delete?s;
return?0;
}


0 回復(fù) 有任何疑惑可以回復(fù)我~

用鏈棧,通過new或malloc動態(tài)申請內(nèi)存。但結(jié)點之間不再是數(shù)組下標之間的關(guān)系,它們是通過指針串起來的。和鏈表相似。

0 回復(fù) 有任何疑惑可以回復(fù)我~

用new寫

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

如果不知道具體需要建多大的站,構(gòu)造函數(shù)怎么寫

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號