forked from super30admin/PreCourse-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise_2_Solution.py
46 lines (38 loc) · 1018 Bytes
/
Exercise_2_Solution.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
#Stack uing linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
#If stack is empty
def isEmpty(self):
return self.head is None
#Push an element in stack
def push(self, item):
newNode = Node(item)
newNode.next = self.head
self.head = newNode
#pop from stack
def pop(self):
if not self.isEmpty():
poppedItem = self.head.data
self.head = self.head.next
return poppedItem
else:
return None
#Peek in the stack
def peek(self):
if not self.isEmpty():
return self.head.data
else:
return None
#Size of the stack
def size(self):
current = self.head
count = 0
while current:
count += 1
current = current.next
return count