鏈式儲存的棧的實現

2021-09-06 11:39:04 字數 3713 閱讀 4867

2013-08-18 15:59:58

順序儲存的棧的缺點:

儲存空間有限,當棧中元素個數超出定義的棧的maxsize時,再進行插入,就會出現訪問越界。當然,可以通過realloc重新分配記憶體以擴大容量,不過這樣不但會增加複雜度,而且要求儲存空間是連續的,當記憶體中沒有這麼大的連續空間時,就會發生記憶體溢位;

為了解決這個問題,鏈式儲存的棧不失為好的辦法,下面是用鍊錶實現的棧。

用鏈式儲存是實現棧時,注意幾點:

將煉表頭結點作為棧頂,而且也不需要附加的頭結點;

棧的結構只有乙個鍊錶指標;

棧的初始化,就是將煉表頭結點置為null;

棧的銷毀,就是鍊錶的銷毀;

棧的push、pop,就是在鍊錶的頭部進行插入、刪除的操作,在pop時,注意對空棧的處理,下面的**中用assert(null != s.top);檢查是否為空;

求棧的長度的操作,就是求鍊錶長度;

獲取棧頂元素,就是獲取煉表頭結點元素,注意對空棧的處理,下面的**中用assert(null != s.top);檢查是否為空。

1 #include 2 #include 3

using

namespace

std;

45 typedef int

datatype;

67 typedef struct

node

8lnode,*plnode;

1213 typedef struct

stack

14stack;

1718

//初始化棧

19void initstack(stack &s)

2023

24//

初始化棧

25void destorystack(stack &s)

2635}36

37//

出棧38

void pushstack(stack &s,datatype data)

3946

47//

出棧48 datatype popstack(stack &s)

4960

61//

獲取棧頂元素

62 datatype gettopofstack(stack &s)

6367

68//

獲取棧中元素個數

69 size_t getlengthofstack(stack &s)

7078

79return

lengthofstack;80}

8182

void displaystack(stack &s)

8390 cout<9293

//棧測試

94void

teststack()

95128

129case

(pop):

130141

142case

(getlength):

143148

149case

(gettop):

150155

default

:156

break

;157

}158 cout<<"

please enter the command ,end with ctrl+z :

"<159}

160161 destorystack(s); //

銷毀棧162

}163

164int

main()

165

測試結果:

1 please enter the command ,end with ctrl+z :22

3 the length of stack is : 0

4 please enter the command ,end with ctrl+z :50

6please enter z data to push :71

8 push 1

9the stack before push :

1011

the stack after push :121

13 please enter the command ,end with ctrl+z :140

15please enter z data to push :162

17 push 2

18the stack before push :191

20the stack after push :212

122 please enter the command ,end with ctrl+z :230

24please enter z data to push :253

26 push 3

27the stack before push :282

129the stack after push :303

2131 please enter the command ,end with ctrl+z :320

33please enter z data to push :344

35 push 4

36the stack before push :373

2138the stack after push :394

32140 please enter the command ,end with ctrl+z :411

42the stack before pop :434

32144 pop 4

45the stack after pop :463

2147 please enter the command ,end with ctrl+z :481

49the stack before pop :503

2151 pop 3

52the stack after pop :532

154 please enter the command ,end with ctrl+z :551

56the stack before pop :572

158 pop 2

59the stack after pop :601

61 please enter the command ,end with ctrl+z :622

63 the length of stack is : 1

64 please enter the command ,end with ctrl+z :652

66 the length of stack is : 1

67 please enter the command ,end with ctrl+z :681

69the stack before pop :701

71 pop 1

72the stack after pop :

7374 please enter the command ,end with ctrl+z :751

76the stack before pop :

7778 assertion failed: null !=s.top, file e:\visual studio 2010_projects\link_stack_

79 2013_08_17\link_stack_2013_08_17\link_stack.cpp, line 50

80 請按任意鍵繼續. . .

棧的順序儲存實現及鏈式儲存實現

include include define size 100 typedef int elemtype typedef struct stackstack void init stack s 初始化 intpush stack s,elemtype e 入棧 s s s top e 從s 1 開始...

棧的鏈式儲存

線性表的順序儲存來模擬棧時,在尾部新增或者刪除元素,不會涉及到陣列的元素大量移動 用線性表的鏈式儲存來模擬棧的線性儲存,在頭部新增或刪除,不用從頭到尾遍歷 linkstack.h ifndef my linkstack h define my linkstack h typedef void lin...

鏈式儲存mysql 鏈棧 棧的鏈式儲存結構

前面講完了棧的順序儲存結構,我們現在來看看棧的鏈式儲存結構,簡稱為鏈棧。鏈棧是沒有附加頭結點的運算受限的單鏈表。棧頂指標就是鍊錶的頭指標。棧是用棧頂來做插入和刪除操作,那麼對於鏈棧的棧頂放在鍊錶的頭部還是尾部呢?單鏈表有頭指標,而棧頂指標也是必須的,那幹嗎不讓它倆合二為一呢,所以比較好的辦法是把棧頂...