各位用户为了找寻关于python学习数据结构实例代码的资料费劲了很多周折。这里教程网为您整理了关于python学习数据结构实例代码的相关资料,仅供查阅,以下为您介绍关于python学习数据结构实例代码的详细内容
在学习python的过程中,用来练习代码,并且复习数据结构的
? 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59#coding:utf-8
#author:Elvis
class
Stack(
object
):
def
__init__(
self
, size
=
8
):
self
.stack
=
[]
self
.size
=
size
self
.top
=
-
1
def
is_empty(
self
):
if
self
.top
=
=
-
1
:
return
True
else
:
return
False
def
is_full(
self
):
if
self
.top
+
1
=
=
self
.size:
return
True
else
:
return
False
def
push(
self
, data):
if
self
.is_full():
raise
Exception(
'stackOverFlow'
)
else
:
self
.top
+
=
1
self
.stack.append(data)
def
stack_pop(
self
):
if
self
.is_empty():
raise
Exception(
'stackIsEmpty'
)
else
:
self
.top
-
=
1
return
self
.stack.pop()
def
stack_top(
self
):
if
self
.is_empty():
raise
Exception(
'stackIsEmpty'
)
else
:
return
self
.stack[
self
.top]
def
show(
self
):
print
self
.stack
stack
=
Stack()
stack.push(
1
)
stack.push(
2
)
stack.push(
'a'
)
stack.push(
'b'
)
stack.push(
5
)
stack.push(
6
)
stack.stack_pop()
stack.stack_pop()
stack.stack_top()
stack.is_empty()
stack.is_full()
stack.show()
以上所述就是本文给大家分享的全部内容了,希望大家能够喜欢。