-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedLists.py
72 lines (52 loc) · 1.27 KB
/
LinkedLists.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
# Singularly Linked List:
# class Node:
# def __init__(self, item=None):
# self.item = item
# self.next = None
# class LinkedList:
# def __init__(self):
# self.head = None
# def LLprint(linked_list):
# temp = []
# node = linked_list.head
# while node:
# temp.append(str(node.item))
# node = node.next
# print(' -> '.join(map(str, temp)))
# Doubly:
class Node:
def __init__(self, item=None):
self.item = item
self.next = None
self.prev = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def LLprint(linked_list):
temp = []
node = linked_list.head
for i in range(4): ## while node: only works if not circular
temp.append(str(node.item))
node = node.next
print(' <-> '.join(map(str, temp)))
if (linked_list.head.prev == linked_list.tail):
print ('This is a circular linked list!')
list1 = LinkedList()
e1 = Node("Mon")
list1.head = e1
e2 = Node("Tue")
e3 = Node("Wed")
list1.tail = e3
# Link first Node to second node
list1.head.next = e2
e2.prev = list1.head
# Link second Node to third node
e2.next = e3
e3.prev = e2
e3.next = e1
e1.prev = e3
e1.item = 1
e2.item = 2
e3.item = 3
LLprint(list1)