Tauruser
Enjoy Every Day
posts - 34, comments - 95, trackbacks - 0, articles - 5
C++博客
::
首頁
::
新隨筆
::
聯系
::
聚合
::
管理
算法與數據結構實驗(二)
Posted on 2006-03-22 12:18
Tauruser
閱讀(719)
評論(2)
編輯
收藏
引用
所屬分類:
算法與數據結構
數組空間組織與鏈表空間組織的堆棧實現
為了增強實現的堆棧通用性,用堆棧實現進行模板化。代碼如下:
/**/
////////////
//stack.h
///////////////
/
////////////////////////////////////////////////
#ifndef?stack_h_
#define
?stack_h_
#include?
<
iostream
>
using
?
namespace
?std;
template?
<
class
?T
>
?
class
?stack
{
public
:
????
virtual
?
void
?push(
const
?T?
&
x)
=
0
;
????
virtual
?
void
?pop()
=
0
;
????
virtual
?T?Top()?
const
?
=
?
0
;
????
virtual
?
bool
?IsEmpty()?
const
?
=
0
;
????
virtual
?
bool
?IsFull()?
const
=
0
;
}
;
#endif
/**/
////////////
segstack.cpp
///////////////
/////////
數組組織代碼
////////////////////////
#include?
"
stack.h
"
template?
<
class
?T
>
?
class
?SegStack:?
public
?stack
<
T
>
{
public
:
????SegStack(
int
?mSize);
????
~
SegStack();
????
bool
?IsEmpty()?
const
;
????
bool
?IsFull()?
const
;
????
void
?push(
const
?T?
&
x);
????
void
?pop();
????T?Top()?
const
;
????
//
friend?ostream&?operator?<<?(ostream&?out,const?SegStack<T>&?seg);
????template?
<
?
class
?T
>
?friend?ostream
&
?
operator
?
<<
?(ostream
&
?
out
,
const
?SegStack
<
T
>&
?seg);?
????
void
?output(ostream
&
?
out
)?
const
;
private
:
????T?
*
s;
????
int
?maxSize;
????
int
?top;
}
;
template?
<
class
?T
>
?SegStack
<
T
>
::SegStack(
int
?mSize):top(
-
1
)
{
????maxSize
=
mSize;
????s?
=
?
new
?T[maxSize];
}
template?
<
class
?T
>
?SegStack
<
T
>
::
~
SegStack()
{
????delete?[]s;
}
template?
<
class
?T
>
?
bool
?SegStack
<
T
>
::IsFull()?
const
{????????
????
return
?(top
==
(maxSize
-
1
));
}
template?
<
class
?T
>
?
bool
?SegStack
<
T
>
::IsEmpty()?
const
{
????
return
?(top
==-
1
);
}
template?
<
class
?T
>
?
void
?SegStack
<
T
>
::push(
const
?T?
&
x)
{
????
if
(IsFull())
????
{
????????cout
<<
"
The?stack?is?full
"
<<
endl;
????}
else
????
{
????????s[
++
top]
=
x;
????}
}
template?
<
class
?T
>
?
void
?SegStack
<
T
>
::pop()
{
????
if
(IsEmpty())
????
{
????????cout
<<
"
The?stack?is?empty
"
<<
endl;
????}
else
????
????
{
????????top
--
;
????}
}
template?
<
class
?T
>
?T?SegStack
<
T
>
::Top()?
const
{
????
return
?s[top];
}
template?
<
class
?T
>
?
void
?SegStack
<
T
>
::output(ostream
&
?
out
)?
const
{
????
out
<<
"
The?stack?list?is:
"
;
????
for
(
int
?i(
0
);i
<=
top;i
++
)
????????
out
<<
"
?
"
<<
s[i];
????
//
out<<endl;
}
template?
<
class
?T
>
?ostream
&
?
operator
?
<<
?(ostream
&
?
out
,
const
?SegStack
<
T
>&
?seg)
{
????
out
<<
"
The?stack?list?is:
"
;
????
for
(
int
?i(
0
);i
<=
seg.top;i
++
)
????????
out
<<
"
?
"
<<
seg.s[i];
????
//
out<<endl;
????
//
seg.output(out);
????
return
?
out
;
}
/**/
///////////////
linkstack.cpp
////////////
////////////
//鏈表實現
/////////////////////
//
#include?
"
stack.h
"
template?
<
class
?T1
>
?
struct
?Element
{
????T1?content;
????Element
*
?next;
}
;
template?
<
class
?T1
>
?
class
?LinkStack:?
public
?stack
<
T1
>
{
public
:
????LinkStack();
????
~
LinkStack();
????
bool
?IsEmpty()?
const
;
????
bool
?IsFull()?
const
;
????
void
?push(
const
?T1?
&
x);
????
void
?pop();
????T1?Top()?
const
;
????template?
<
class
?T
>
?friend?ostream
&
?
operator
<<
(ostream
&
?
out
,?
const
?LinkStack
<
T1
>&
?linkstack);
????
void
?output(ostream
&
?
out
)?
const
;
private
:
????Element
<
T1
>*
?top;
}
;
template?
<
class
?T1
>
?
bool
?LinkStack
<
T1
>
::IsEmpty()?
const
{
????
if
(top
==
NULL)
????????
return
?
true
;
????
else
????????
return
?
false
;
}
template?
<
class
?T1
>
?
bool
?LinkStack
<
T1
>
::IsFull()?
const
{
????
return
?
false
;
}
template?
<
class
?T1
>
?LinkStack
<
T1
>
::LinkStack():top(NULL)
{
}
template?
<
class
?T1
>
?LinkStack
<
T1
>
::
~
LinkStack()
{
????
while
(top
!=
NULL)
????
{
????????Element
<
T1
>*
?temp;
????????temp
=
top;
????????top
=
top
->
next;
????????delete?temp;
????}
}
template?
<
class
?T1
>
?
void
?LinkStack
<
T1
>
::push(
const
?T1?
&
x)
{
????Element
<
T1
>*
?temp
=
new
?Element
<
T1
>
;
????temp
->
content
=
x;
????temp
->
next
=
top;
????top
=
temp;
}
template?
<
class
?T1
>
?
void
?LinkStack
<
T1
>
::pop()
{
????
if
(top
!=
NULL)
????
{
????????Element
<
T1
>*
?temp;
????????temp
=
top;
????????top
=
top
->
next;
????????delete?temp;
????}
????
}
template?
<
class
?T1
>
?T1?LinkStack
<
T1
>
::Top()?
const
{
????
return
?top
->
content;
????
}
template?
<
class
?T1
>
?ostream
&
?
operator
<<
(ostream
&
?
out
,?
const
?LinkStack
<
T1
>&
?linkstack)
{
????Element
<
T1
>*
?temp;
????temp
=
linkstack.top;
????
out
<<
"
The?stack?list?is:
"
;
????
while
(temp
!=
NULL)
????
{
????????
out
<<
temp
->
content
<<
'
?
'
;
????????temp
=
temp
->
next;
????}
????
return
?
out
;
}
template?
<
class
?T1
>
?
void
?LinkStack
<
T1
>
::output(ostream
&
?
out
)?
const
{
????Element
<
T1
>*
?temp;
????temp
=
top;
????
out
<<
"
The?stack?list?is:
"
;
????
while
(temp
!=
NULL)
????
{
????????
out
<<
temp
->
content
<<
'
?
'
;
????????temp
=
temp
->
next;
????}
}
沒有寫注釋,有空再補上吧。
Feedback
#
re: 算法與數據結構實驗(二)
回復
更多評論
2006-03-22 17:33 by
任我行
template < class T > class stack
{
public :
virtual void push( const T & x) = 0 ;
virtual void pop() = 0 ; // 這樣子void 類型有些不妥吧。
#
re: 算法與數據結構實驗(二)
回復
更多評論
2006-03-22 18:15 by
Tauruser
嗯,你認為應該如何呢?
我知道有些stack結構直接用pop(),彈出并返回棧頂,但這個模板類已經有一個Top()函數可以做到這個,將pop()返回值設為void有什么不妥呢?
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
雙鏈表模版類的實現
C風格字符串與標準庫string類型性能對比
算法與數據結構實驗(二)
為什么在VS2005重載輸出運算符那么難?
我的算法與數據結構學習(三)
Josephus問題
算法與數據結構實驗(一)
我的算法與數據結構學習(二)
我的算法與數據結構學習(一)
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Powered by:
C++博客
Copyright © Tauruser
日歷
<
2025年7月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
公告
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
(7)
給我留言
查看公開留言
查看私人留言
隨筆分類
(16)
MFC(2)
計算機圖形學(2)
密碼學
數學模型
數值計算(2)
算法與數據結構(9)
信息論(1)
隨筆檔案
(34)
2007年3月 (1)
2007年2月 (2)
2007年1月 (1)
2006年10月 (1)
2006年6月 (3)
2006年5月 (5)
2006年4月 (6)
2006年3月 (14)
2006年2月 (1)
文章分類
(5)
密碼學
數學模型
數值計算
算法與數據結構(4)
網絡(1)
信息論
文章檔案
(5)
2006年4月 (1)
2006年3月 (4)
相冊
文章貼圖
收藏夾
(3)
我的收藏(3)
協議
流媒體
中國協議網
友情鏈接
Orlaa
最新隨筆
1.?CListCtrl For Beginners(轉載)
2.?結構體對齊的具體含義(#pragma pack) (轉載)
3.?MFC ComboBox 使用方法(轉載)
4.?05年寫的直線裁剪算法
5.?C# Coding時的注釋格式(zz)
6.?查找字符串的哈希方法(zz)
7.?木馬客戶端與服務端通訊如何隱藏不被發現
8.?Gauss消去法直接求解方程組(附例程)
9.?使用AsycnSocket類進行簡單雙機通訊
10.?Romberg求積(例程)
搜索
積分與排名
積分 - 106515
排名 - 238
最新評論
1.?re: 雙鏈表模版類的實現
問下:關于查找(search)那部分,我有點小問題要問,就是你直接就靠默認的比較操作符來比較,而你所用的是模版,鏈表支持各種類型,那么是字符串類型的鏈表或自定義類型的呢,你該怎么辦
--周曉榮
2.?re: OnSize()加入處理函數后,DEBUG報告出錯
評論內容較長,點擊標題查看
--NULL
3.?re: C風格字符串與標準庫string類型性能對比
評論內容較長,點擊標題查看
--Hzj_jie
4.?re: 05年寫的直線裁剪算法
你這個算法也太復雜了吧
--啊啊啊啊啊啊
5.?re: C++ Primer Fourth Edition (download file)
i want english edition
--zhccc
閱讀排行榜
1.?MFC ComboBox 使用方法(轉載)(18750)
2.?結構體對齊的具體含義(#pragma pack) (轉載)(11466)
3.?Tab Control控件使用的例子(zz)(8364)
4.?MAC地址有合法不合法之分嗎?(7612)
5.?VC++2005 比 VC++ 6.0 退步了?(7544)
評論排行榜
1.?C風格字符串與標準庫string類型性能對比(20)
2.?VC++2005 比 VC++ 6.0 退步了?(12)
3.?OnSize()加入處理函數后,DEBUG報告出錯(8)
4.?為什么在VS2005重載輸出運算符那么難?(7)
5.?Gauss消去法直接求解方程組(附例程)(7)
久久国产免费观看精品
|
中文字幕亚洲综合久久2
|
伊人久久精品影院
|
久久久国产乱子伦精品作者
|
日本久久久精品中文字幕
|
国内精品久久久久久中文字幕
|
一本色道久久88综合日韩精品
|
久久久av波多野一区二区
|
日韩久久久久中文字幕人妻
|
久久精品国产亚洲av水果派
|
狠狠人妻久久久久久综合
|
久久久午夜精品
|
久久福利青草精品资源站免费
|
精品久久综合1区2区3区激情
|
精品人妻伦九区久久AAA片69
|
久久精品国产亚洲精品
|
91精品国产综合久久婷婷
|
久久久亚洲AV波多野结衣
|
一日本道伊人久久综合影
|
国产综合久久久久
|
亚洲午夜久久久久久噜噜噜
|
韩国三级中文字幕hd久久精品
|
色综合久久无码中文字幕
|
精品综合久久久久久88小说
|
99久久国产热无码精品免费
|
亚洲色欲久久久综合网
|
亚洲综合久久久
|
精品久久久久成人码免费动漫
|
久久成人国产精品一区二区
|
日韩一区二区久久久久久
|
国产91色综合久久免费分享
|
国产成人综合久久综合
|
久久不射电影网
|
国产成人综合久久综合
|
久久久久免费精品国产
|
亚洲国产成人久久精品影视
|
久久99精品久久久久久
|
欧美777精品久久久久网
|
日韩一区二区久久久久久
|
亚洲嫩草影院久久精品
|
国产亚洲色婷婷久久99精品91
|