C 資料結構學習 線性結構之鏈棧

2021-10-03 07:25:22 字數 1739 閱讀 6392

上次我們學了順序棧,這次我們接觸一下鏈式堆疊

棧的鏈式儲存結構實際上就是乙個單鏈表,叫做鏈棧。插入和刪除操作只能在鏈棧的棧頂進行

先是自定義鏈棧的結點

class

linkstacknode

set}

public linkstacknode next

set}

public

linkstacknode()

public

linkstacknode

(t data)

}

再自定義鏈棧

class

linkstack

set}

public

linkstack()

public

bool

isempty()

public

void

push

(t data)

newdata.next = top.next;

top.next = newdata;

count++;}

public

tpop()

top.next = currentdata.next;

count--

;return currentdata.value;

}public

tpeek()

return currentdata.value;

}public

void

clear()

}

再驗證一下

//鏈棧

linkstack<

int> a =

newlinkstack

<

int>()

; console.

writeline

("-------------------進行入棧--------------------");

for(

int i =

0; i <

5; i++

) console.

writeline

("當前元素個數為"

+a.count)

; console.

writeline

("-------------------入棧完成--------------------");

console.

writeline

("-------------------進行出棧--------------------");

for(

int i =

0; i <

5; i++

) console.

writeline

("-------------------出棧完成--------------------");

console.

writeline

("當前元素個數為"

+ a.count)

; console.

readkey()

;

看看結果

好的 今天鏈棧學習就到此結束

資料結構學習 棧

這一系列部落格的目的在於複習鞏固資料結構的基礎知識,為考研面試筆試做準備,所以重在原理,實踐不是重點。參考書籍有嚴蔚敏老師的 資料結構 c語言版 c c 資料結構與演算法速學速用大辭典 define stacksize 100 typedef int datatype typedef structs...

(C )資料結構學習一 棧

棧的主要特點是後進先出 last in first out,lifo 即出棧元素只能是位於棧頂的元素,入棧也只能在棧頂。棧是只能在一端 棧頂 進行插入或刪除的線性表。n個元素依次入棧,可在任意時刻出棧,則共有 棧的儲存結構 3.棧的應用3.1括號匹配檢測 3.2遞迴演算法改為非遞迴演算法需要用到棧,...

資料結構學習 2 線性結構之線性表

線性結構的基本特點是除第乙個元素無直接前驅 最後乙個元素無直接後繼之外,其他每個資料元素都有乙個前驅和後繼。像乙個線段一樣,有頭有尾的排排坐 線性表 有n個資料特性相同的元素構成的有限序列稱為線性表。n為線性表的長度,n 0時稱為空表。對於非空的線性表,其特點有 存在唯一的乙個被稱為 第乙個 的資料...