-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackList.py
More file actions
executable file
·49 lines (38 loc) · 1.03 KB
/
StackList.py
File metadata and controls
executable file
·49 lines (38 loc) · 1.03 KB
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
#!/usr/bin/env python
class Stack(object):
class Node(object):
def __init__(self, v, n):
self.value = v
self.next = n
def __init__(self):
self.head = None
self.stacksize = 0
def size(self):
return self.stacksize
def isEmpty(self):
return self.stacksize == 0
def peek(self):
if self.isEmpty():
raise RuntimeError("StackEmptyException")
return self.head.value
def push(self, value):
self.head = self.Node(value, self.head)
self.stacksize += 1
def pop(self):
if self.isEmpty():
raise RuntimeError("StackEmptyException")
value = self.head.value
self.head = self.head.next
self.stacksize -= 1
return value
def printStack(self):
temp = self.head
while temp != None:
print temp.value,
temp = temp.next
s = Stack()
s.push(1)
s.push(2)
s.push(3)
s.pop()
s.printStack()