"""
棧 (sstack)
棧的順序儲存結構
"""# 基於列表實現順序棧
# 自定義棧異常
class
stackerror
(exception)
:pass
class
sstack
:def
__init__
(self)
:# 約定列表的最後乙個元素為棧頂元素
self._elems =
# 顯示棧頂元素
deftop
(self):if
not self._elems:
raise stackerror(
"stack is empty"
)return self._elems[-1
]# 判斷棧是否為空
defis_empty
(self)
:return self._elems ==
# 入棧
defpush
(self,elem)
:# 出棧
defpop
(self):if
not self._elems:
raise stackerror(
"stack is empty"
)return self._elems.pop(
)if __name__ ==
"__main__"
: st = sstack(
)#初始化棧
# print(st.top())
print
(st.is_empty())
st.push(10)
st.push(30)
st.push(20)
print
(st.top())
# while not st.is_empty():
# print(st.pop())
# st.pop(
)print
(st.top(
))
棧的順序儲存結構
標頭檔案 函式的宣告 include include include define stacksize 100 typedef int elemtype typedef struct seqstack void initstack seqstack s 初始化棧 int stackempty seq...
棧的順序儲存結構
include include define maxsize 10 typedef struct stack sqstack,sq 棧的初始化,建立乙個空棧 sq inistack intpush sq s,int e void printf sq s while i s top 刪除棧頂元素,並用...
棧的順序儲存結構
棧是一種限定僅在表尾進行插入或刪除操作的線性表。允許插入或刪除的一端為棧頂,另一端為棧底。特點是先進後出 first in last out 棧的插入操作叫做入棧,棧的刪除操作叫做出棧。c語言 入棧 int push stack s elemtype e s top s data s top e r...