-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.py
More file actions
70 lines (68 loc) · 1.52 KB
/
temp.py
File metadata and controls
70 lines (68 loc) · 1.52 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#qqstackdqsswdshndsfs
class Node:
def __init__(self, data) -> None:
self.data = data
self.next = None
class Stack:
def __init__(self) -> None:
self.top = None
def push(self, data):
node = Node(data)
if self.top is None:
self.top = node
return
node.next = self.top
self.top = node
def pop(self):
if self.top is None:
return "Is empty"
removed = self.top.data
self.top = self.top.next
return removed
def peek(self):
return self.top.data if self.top else None
def display(self):
current = self.top
while current:
print(current.data)
current = current.next
def palindrome(self, str):
for i in str:
self.push(i)
for i in str:
print(self.peek(), i)
if self.peek() == i:
self.pop()
else:
return False
return self.top is None
def reverse(self, str):
temp = Stack()
for i in str:
temp.push(i)
rev = ''
while temp.peek():
rev += temp.pop()
return rev
def copy(self, s):
temp = Stack()
while s.peek():
temp.push(s.pop())
while temp.peek():
self.push(temp.pop())
s = Stack()
s.display()
s.push(2)
s.push(1)
s.push(6)
s.push(4)
print()
print()
s.display()
print()
print()
print()
s2 = Stack()
s2.copy(s)
s2.display()
#test