棧:具有先進(jìn)后出的特點(diǎn),且只能在一端進(jìn)行插入與刪除的操作,棧的實(shí)現(xiàn)如下所示
在解放等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),全網(wǎng)營(yíng)銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,解放網(wǎng)站建設(shè)費(fèi)用合理。struct truetype
{
bool get()
{
return true;
}
};
struct falsetype
{
bool get()
{
return false;
}
};
template
struct typetraits
{
typedef falsetype isnpodtype;
};
template <>
struct typetraits
{
typedef truetype ispodtype;
};
template
class stack
{
protected:
T *_a;
size_t _top;
size_t _capacity;
public:
stack()
: _top(0)
, _capacity(3)
{
_a = new T[_capacity];
}
~stack()
{
if (_a)
delete[] _a;
}
void checkcapacity()
{
if (_top == _capacity)
{
_capacity = 2 * _capacity;
T *tem = new T[_capacity];
if ((typetraits
{
memcpy(tem, _a, sizeof(T)*_capacity);
delete[]_a;
_a = tem;
}
else
{
int i = 0;
for (i = 0; i < _top; i++)
{
tem[i] = _a[i];
}
delete[]_a;
_a = tem;
}
}
}
void push(const T&x)
{
checkcapacity();
_a[_top] = x;
_top++;
}
void pop()
{
_top--;
}
bool Empty()
{
return _top == 0;
}
T &Top()
{
if (_top > 0)
{
size_t ret = _top;
_top--;
return _a[ret - 1];
}
}
};
int main()
{
stack
s1.push(1);
s1.push(2);
s1.push(3);
s1.push(4);
while (!s1.Empty())
{
cout << s1.Top() << " ";
}
getchar();
return 0;
}
2.隊(duì)列:具有隊(duì)頭插入,隊(duì)尾刪除的特點(diǎn),具有一個(gè)頭指針和一個(gè)尾指針
template
struct Node
{
T _data;
Node
Node(const T &data=0)
{
_data = data;
_next = NULL;
}
};
template
class Queue
{
protected:
Node
Node
public:
Queue()
:_head(NULL)
, _tail(NULL)
{}
~Queue()
{
Node
if (_head == NULL)
return;
if (_head == _tail)
{
delete _head;
_head = NULL;
_tail = _head;
}
else
{
while (_head)
{
pop();
}
}
}
void push(const T&x)
{
Node
if (_tail == NULL)
{
_tail = newNode;
_head = _tail;
}
else
{
_tail->_next = newNode;
_tail = newNode;
}
}
void pop()
{
Node
if (_head == NULL)
return;
if (_head == _tail)
{
delete _head;
_head = NULL;
_tail = _head;
}
else
{
ret = _head;
_head = _head->_next;
delete ret;
}
}
T &Front()
{
Node
_head = _head->_next;
return ret->_data;
}
T &Back()
{
Node
_tail = _head->_tail;
return _tail->_data;
}
bool Empty()
{
return _head == _tail;
}
void show()
{
while (_head)
{
cout << _head->_data << " ";
_head = _head->_next;
}
}
};
int main()
{
Queue
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.pop();
q.show();
getchar();
return 0;
}
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開(kāi)啟,新人活動(dòng)云服務(wù)器買多久送多久。