forked from stormBMO/homeworks_work_etc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.py
100 lines (82 loc) · 2.38 KB
/
linked_list.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class Node:
def __init__(self, _key, _next):
self.key = _key
self.next = _next
def __del__(self):
del self.next
def insert(self, _value):
cur = self
while cur.next is not None:
cur = cur.next
cur.next = _value
def erase(self, _key):
cur = self
while cur is not None:
if cur.key == _key:
old_data = cur
cur = old_data.next
old_data.next = None
del old_data
return True
cur = cur.next
return False
def print(self, node):
if node is not None:
print(node.key, end=' ')
node.print(node.next)
def reverse_print(self, node):
if node is not None:
node.reverse_print(node.next)
print(node.key, end=' ')
def perform_bubble_sort_step(self, node):
if node.next is not None:
if node.key.upper() > node.next.key.upper():
node.key, node.next.key = node.next.key, node.key
node.perform_bubble_sort_step(node.next)
class SingleLinkedList:
def __init__(self):
self.head = None
self.size = 0
def __del__(self):
del self.head
def insert(self, _key):
if not self.head:
self.head = Node(_key, None)
self.size += 1
else:
Node.insert(self.head, Node(_key, None))
self.size += 1
def erase(self, _key):
if self.size == 0:
return False
if self.head.key == _key:
self.head = self.head.next
self.size -= 1
elif Node.erase(self.head, _key):
self.size -= 1
return True
return False
def print(self):
Node.print(self.head, self.head)
print()
def reverse_print(self):
Node.reverse_print(self.head, self.head)
print()
def sort(self):
if self.size > 1:
for i in range(self.size):
Node.perform_bubble_sort_step(self.head, self.head)
string = input()
cur_word = ""
arr = SingleLinkedList()
for i in range(len(string)):
if 'a' <= string[i] <= 'z' or 'A' <= string[i] <= 'Z':
cur_word += string[i]
else:
arr.insert(cur_word)
cur_word = ""
if cur_word != "":
arr.insert(cur_word)
arr.sort()
arr.print()
arr.reverse_print()