-
Notifications
You must be signed in to change notification settings - Fork 0
/
栈的链表实现.py
54 lines (35 loc) · 876 Bytes
/
栈的链表实现.py
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
# -*- coding:utf-8 -*-
#Õ»µÄÁ´±íʵÏÖ
class Node(object):
def __init__(self,p=0):
self.val=None
self.next=p
class stack(Node):
def __init__(self):
self.head=0
def create_stack(self):
self.head=Node()
self.head.next=0
def is_empty(self):
return (self.head.next==0)
def make_empty(self):
while (not self.is_empty()):
self.pop()
def push(self,tar):
tempcell=Node()
tempcell.val=tar
tempcell.next=self.head.next
self.head.next=tempcell
def pop(self):
if (self.is_empty()):
return "empty stack"
else:
firstcell=self.head.next
self.head.next=firstcell.next
return firstcell.val
s=stack()
s.create_stack()
for i in range(5):
s.push(i)
for i in range(5):
print s.pop()