99
1010class Node :
1111
12- def __init__ (self , data , next = None ):
12+ def __init__ (self , data , next_node = None ):
1313 self .data = data
14- self ._next = next
14+ self .next_node = next_node
1515
1616
1717class LinkedStack :
1818 """A stack based upon singly-linked list.
1919 """
2020
2121 def __init__ (self ):
22- self ._top = None
22+ self ._head = None
2323 self ._size = 0
2424
2525 def __repr__ (self ):
26- current = self ._top
26+ current = self ._head
2727 nums = []
2828 while current :
2929 nums .append (current .data )
30- current = current ._next
31- ret = ',' .join ([str (num ) for num in nums ])
30+ current = current .next_node
31+ ret = ',' .join ([repr (num ) for num in nums ])
3232 return f'[{ ret } ]'
3333
3434 def is_empty (self ):
@@ -38,87 +38,40 @@ def size(self):
3838 return self ._size
3939
4040 def push (self , item ):
41- new_top = Node (item )
42- new_top . _next = self ._top
43- self ._top = new_top
41+ new_head = Node (item )
42+ new_head . next_node = self ._head
43+ self ._head = new_head
4444 self ._size += 1
4545
4646 def pop (self ):
4747 if not self ._size :
4848 print ('Stack Overflow' )
4949 else :
50- item = self ._top .data
51- self ._top = self ._top . _next
50+ item = self ._head .data
51+ self ._head = self ._head . next_node
5252 return item
5353
5454 def top (self ):
55+ """
56+ 返回栈顶元素
57+ :return:
58+ """
5559 if not self ._size :
5660 print ('Stack Overflow.' )
5761 else :
58- return self ._top .data
59-
60-
61- s = LinkedStack ()
62- print (s .is_empty ())
63- s .push (4 )
64- print (s )
65- s .push ('dog' )
66- print (s )
67- # print(s.peek())
68- s .push (True )
69- print (s )
70- print (s .size ())
71- print (s .is_empty ())
72- print (s )
73-
74- # class LinkedStack:
75- # """
76- # 基于单链表的栈实现
77- # """
78- #
79- # def __init__(self):
80- # self._top = None
81- # self._num = 0
82- #
83- # def is_empty(self):
84- # return self._num == 0
85- #
86- # def size(self):
87- # return self._num
88- #
89- # # def __repr__(self):
90- # # current = self._top
91- # # nums = []
92- # # while current:
93- # # nums.append(current._data)
94- # # current = current._next
95- # # return ' '.join(f'{num}' for num in nums)
96- #
97- # def push(self, value):
98- # new_top = Node(value)
99- # new_top._next = self._top
100- # self._top = new_top
101- # self._num += 1
102- #
103- # def pop(self):
104- # if not self._num:
105- # print('The length of stack is 0.')
106- # else:
107- # item = self._top._data
108- # self._top = self._top.next
109- # return item
110- #
111- #
112- # if __name__ == '__main__':
113- # s = LinkedStack()
114- # print(s.is_empty())
115- # s.push(4)
116- # print(s)
117- # s.push('dog')
118- # print(s)
119- # # print(s.peek())
120- # s.push(True)
121- # print(s)
122- # print(s.size())
123- # print(s.is_empty())
124- # print(s)
62+ return self ._head .data
63+
64+
65+ if __name__ == '__main__' :
66+ s = LinkedStack ()
67+ print (s .is_empty ())
68+ s .push (4 )
69+ print (s )
70+ s .push ('dog' )
71+ print (s )
72+ print (s .top ())
73+ s .push (True )
74+ print (s )
75+ print (s .size ())
76+ print (s .is_empty ())
77+ print (s .pop ())
0 commit comments