-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.py
54 lines (40 loc) · 1.24 KB
/
linkedlist.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
class Node :
def __init__(self, data) :
self.data = data
self.next = None
class Linkedlist :
def __init__(self) :
dummy = Node("dummy")
self.head = dummy
self.tail = dummy
self.current = None
self.before = None
self.num_of_data = 0
def append(self, data) :
new_node = Node(data)
self.tail.next = new_node
self.tail = new_node
self.tail = new_node
self.num_of_data += 1
def delete(self) :
pop_data = self.current.data
if self.current is self.tail :
self.tail = self.before
self.before.next = self.current.next
self.current = self.before
self.num_of_data -= 1
return pop_data
def first(self) :
if self.num_of_data == 0 :
return None
self.before = self.head
self.current = self.head.next
return self.current.data
def next(self) :
if self.current.next == None :
return None
self.before = self.current
self.current = self.current.next
return self.current.data
def size(self) :
return self.num_of_data