-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackLinkedList.py
65 lines (49 loc) · 1.49 KB
/
StackLinkedList.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
55
56
57
58
59
60
61
62
63
64
# Created by Elshad Karimov on 23/05/2020.
# Copyright © 2020 AppMillers. All rights reserved.
class Node:
def __init__(self, value = None):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def __iter__(self):
curNode = self.head
while curNode:
yield curNode
curNode = curNode.next
class Stack:
def __init__(self):
self.LinkedList = LinkedList()
def __str__(self):
values = [str(x.value) for x in self.LinkedList]
return '\n'.join(values)
def isEmpty(self):
if self.LinkedList.head == None:
return True
else:
return False
def push(self, value):
node = Node(value)
node.next = self.LinkedList.head
self.LinkedList.head = node
def pop(self):
if self.isEmpty():
return "There is not any element in the stack"
else:
nodeValue = self.LinkedList.head.value
self.LinkedList.head = self.LinkedList.head.next
return nodeValue
def peek(self):
if self.isEmpty():
return "There is not any element in the stack"
else:
nodeValue = self.LinkedList.head.value
return nodeValue
def delete(self):
self.LinkedList.head = None
customStack = Stack()
customStack.push(1)
customStack.push(2)
customStack.push(3)
print(customStack.peek())