forked from rafaelalmeida2909/Python-Simple-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.py
More file actions
84 lines (63 loc) · 1.86 KB
/
Copy pathLinkedList.py
File metadata and controls
84 lines (63 loc) · 1.86 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
# ----------------------------------------------------------------------------
# Created By: Francisco José Cardoso
# Created Date: 13/10/2022
# ---------------------------------------------------------------------------
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
current_node = self.head
while current_node.next:
current_node = current_node.next
current_node.next = new_node
return
def lenght(self):
if self.head == None:
return 0
current_node = self.head
total = 0
while current_node:
total = total + 1
current_node = current_node.next
return total
def to_list(self):
node_data = []
current_node = self.head
while current_node:
node_data.append(current_node.data)
current_node = current_node.next
return node_data
def display(self):
contents = self.head
if contents is None:
print("A lista está vazia")
while contents:
print(contents.data)
contents = contents.next
print("----------")
def reverse_linkedlist(self):
previous_node = None
current_node = self.head
while current_node is not None:
next = current_node.next
current_node.next = previous_node
previous_node = current_node
current_node = next
self.head = previous_node
# Test
my_list = LinkedList()
my_list.display()
my_list.append(3)
my_list.append(5)
my_list.append(6)
my_list.append(9)
my_list.display()
my_list.lenght()