-
Notifications
You must be signed in to change notification settings - Fork 0
/
ll.py
62 lines (54 loc) · 989 Bytes
/
ll.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
class Node(object):
def __init__(self, data, node = None):
self.data = data
self.next = node
class LinkedList(object):
def __init__(self):
self.head = None
self.tail = None
def insert(self, data):
n = Node(data)
n.next = self.head
self.head = n
def insert_front(self,data):
n = Node(data)
if self.head == None:
self.head = n
if self.tail != None:
self.tail.next = n
self.tail = n
def pop(self):
t = None
if self.head:
t = self.head.data
self.head = self.head.next
return t
def is_empty(self):
return self.head == None
def main():
ll = LinkedList()
ll.insert(1)
ll.insert(2)
ll.insert(10)
ll.insert(20)
ll.insert(30)
ll.insert(40)
ll.insert(50)
ll.insert(60)
ll.insert(70)
ll.insert(80)
ll.insert(90)
sr = ll.head
fr = ll.head
while sr:
print("sr", sr.data)
sr = sr.next
if fr:
print("fr", fr.data)
if fr.next:
fr = fr.next.next
else:
fr = None
print("----")
if __name__ == "__main__":
main()