Browse Source

adding dlist

shuvit 3 years ago
parent
commit
7c55880556
1 changed files with 129 additions and 0 deletions
  1. 129
    0
      DList.py

+ 129
- 0
DList.py View File

@@ -0,0 +1,129 @@
1
+""" A node is a container which encapsulates any type of data.
2
+    The node object also has a reference to the NODE preceding and following
3
+    said object."""
4
+class Node(object):
5
+ 
6
+    def __init__(self, data, prev, next):
7
+        self.data = data
8
+        self.prev = prev
9
+        self.next = next
10
+        
11
+    def getNext(self):
12
+        if self.next is not None:
13
+            return self.next
14
+        print('There\'s nothing next, trying prev')
15
+        if self.prev is not None:
16
+            return self.prev
17
+        print('There\s nothing before either, just this one')
18
+        return self
19
+        
20
+    def getPrev(self):
21
+        if self.prev is not None:
22
+            return self.prev
23
+        print('There\'s nothing before, trying next')
24
+        if self.next is not None:
25
+            return self.next
26
+        print('There\'s nothing next either, just this one')
27
+        return self
28
+        
29
+    def getData(self):
30
+        return self.data
31
+ 
32
+""" A doubly-linked list implementation 
33
+    NOTE: the getters and setters return a reference to a Node object,
34
+    inside which is the data you're probably looking for. If you want to
35
+    access data, remember; after you call a get or find, use .data"""
36
+class DoubleList(object):
37
+ 
38
+    head = None
39
+    tail = None
40
+    length = 0;
41
+ 
42
+    """ Add a node to the list """
43
+    def append(self, data):
44
+        new_node = Node(data, None, None)
45
+        if self.head is None:
46
+            self.head = self.tail = new_node
47
+        else:
48
+            new_node.prev = self.tail
49
+            new_node.next = None
50
+            self.tail.next = new_node
51
+            self.tail = new_node
52
+        self.length = self.length + 1
53
+ 
54
+    """ Find a node in the list, then remove it """
55
+    def remove(self, node):
56
+        current_node = self.head
57
+ 
58
+        while current_node is not None:
59
+            if current_node == node:
60
+                # if it's not the first element
61
+                if current_node.prev is not None:
62
+                    current_node.prev.next = current_node.next
63
+                    current_node.next.prev = current_node.prev
64
+                # if its the last element
65
+                elif current_node.next is None:
66
+                    self.tail = current_node.prev
67
+                    self.tail.prev = current_node.prev.prev
68
+                    self.tail.next = None
69
+                else:
70
+                    # otherwise we have no prev (it's None), head is the next one, and prev becomes None
71
+                    self.head = current_node.next
72
+                    current_node.next.prev = None
73
+ 
74
+            current_node = current_node.next
75
+        self.length = self.length - 1
76
+    
77
+    def show(self):
78
+        print("Show list data:")
79
+        current_node = self.head
80
+        while current_node is not None:
81
+            print(current_node.data)
82
+            current_node = current_node.next
83
+        print("*"*50)
84
+        
85
+    """ Find a node in the list by value comparison """
86
+    def findNode(self, node_value):
87
+        current_node = self.head
88
+        
89
+        while current_node is not None:
90
+            if current_node.data == node_value:
91
+                return current_node
92
+            current_node = current_node.next
93
+    
94
+    """ Get a node by index position """
95
+    def get(self, index):
96
+        if index > self.length:
97
+            return none
98
+        elif index == 1:
99
+            return self.head
100
+        elif index == self.length:
101
+            return self.tail
102
+        else:
103
+            current_node = self.head
104
+            pos = 1
105
+            while pos < index:
106
+                current_node = current_node.next
107
+                pos = pos + 1
108
+            return current_node
109
+    
110
+    """ Checks if a node is in this list """
111
+    def contains(self, node):
112
+        current_node = self.head
113
+        while current_node is not None:
114
+            if current_node == node:
115
+                return true
116
+            current_node = current_node.next
117
+        return false
118
+        
119
+    """ Get the length of this list """
120
+    def getLength(self):
121
+        return self.length
122
+        
123
+    """ Get the head of this list """
124
+    def getHead(self):  # get head (;
125
+        return self.head
126
+        
127
+    """ Get the tail of this list """
128
+    def getTail(self):
129
+        return self.tail

Loading…
Cancel
Save