-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW3.py
More file actions
87 lines (73 loc) · 1.93 KB
/
HW3.py
File metadata and controls
87 lines (73 loc) · 1.93 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Node:
def __init__(self, data = None, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self, head=None):
self.head = head
def empty(self):
if self.head:
return False
return True
def printList(self):
node = self.head
while node:
print(node.data, end=" -> ")
node = node.next
print()
def push(self, data):
node = Node(data, next=self.head)
self.head = node
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
else:
node = self.head
while node.next:
node = node.next
node.next = new_node
def find_index(self, value):
node = self.head
index = 0
while node:
if node.data == value:
return index
index += 1
if node.next:
node = node.next
else:
break
return None
def insert(self, index, value):
new_node = Node(value)
cur_node = self.head
if index < 1:
self.push(value)
else:
for i in range (0, index-1):
cur_node = cur_node.next
new_child = cur_node.next
cur_node.next = new_node
new_node.next = new_child
def size(self):
node = self.head
cur_size = 1
while node.next:
node = node.next
cur_size += 1
return cur_size
def value_n_from_end(self, n):
index = self.size() - n
cur_node = self.head
for i in range (0, index):
cur_node = cur_node.next
return cur_node
n3 = Node(9)
n2 = Node(8, next=n3)
n1 = Node(7, next=n2)
l = LinkedList(head=n1)
for i in [1,2,3,4]:
l.push(i)
l.printList()
print(l.value_n_from_end(3).data)