linked list - progress update
This commit is contained in:
parent
a5a80b77f0
commit
0f841ee61f
|
@ -662,10 +662,9 @@ if you can identify the runtime complexity of different algorithms. It's a super
|
|||
- [x] front() - get value of front item
|
||||
- [x] back() - get value of end item
|
||||
- [x] insert(index, value) - insert value at index, so current item at that index is pointed to by new item at index
|
||||
- [ ] erase(index) - removes node at given index
|
||||
- [ ] value_n_from_end(n) - returns the value of the node at nth position from the end of the list
|
||||
- [x] erase(index) - removes node at given index
|
||||
- [x] reverse() - reverses the list
|
||||
- [ ] remove_value(value) - removes the first item in the list with this value
|
||||
- [x] remove_value(value) - removes the first item in the list with this value
|
||||
- [ ] Doubly-linked List
|
||||
- [Description (video)](https://www.coursera.org/lecture/data-structures/doubly-linked-lists-jpGKD)
|
||||
- No need to implement
|
||||
|
|
|
@ -97,14 +97,11 @@ class LinkedList:
|
|||
if index == 0:
|
||||
self.push_front(value)
|
||||
return
|
||||
if self.size() == 1 and index == 1:
|
||||
self.push_back(value)
|
||||
return
|
||||
else:
|
||||
cur_index = 0
|
||||
node = self.__head
|
||||
prev = node
|
||||
while cur_index != index and node.get_next() is not None:
|
||||
while cur_index != index and node is not None:
|
||||
cur_index += 1
|
||||
prev = node
|
||||
node = node.get_next()
|
||||
|
@ -145,6 +142,35 @@ class LinkedList:
|
|||
node = node.get_next()
|
||||
self.__head = reversed_list.__head
|
||||
|
||||
def remove_at(self, index):
|
||||
if index > self.size() or index < 0:
|
||||
raise IndexError(index)
|
||||
node = self.__head
|
||||
prev = node
|
||||
cur_index = 0
|
||||
while cur_index != index:
|
||||
if node.get_next() is not None:
|
||||
prev = node
|
||||
node = node.get_next()
|
||||
cur_index += 1
|
||||
else:
|
||||
break
|
||||
if cur_index == index:
|
||||
prev.set_next(node.get_next())
|
||||
else:
|
||||
raise IndexError(index)
|
||||
|
||||
def remove_value(self, value):
|
||||
node = self.__head
|
||||
prev = node
|
||||
while node.get_next() is not None:
|
||||
if node.get_value() == value:
|
||||
prev.set_next(node.get_next())
|
||||
break
|
||||
else:
|
||||
prev = node
|
||||
node = node.get_next()
|
||||
|
||||
def print(self):
|
||||
str = ""
|
||||
node = self.__head
|
||||
|
|
|
@ -88,6 +88,30 @@ class LinkedListTestCase(unittest.TestCase):
|
|||
printed = linked_list.print()
|
||||
self.assertEqual(output, printed)
|
||||
|
||||
def test_remove_at(self):
|
||||
linked_list = LinkedList()
|
||||
linked_list.push_back(0)
|
||||
linked_list.push_back(1)
|
||||
linked_list.push_back(2)
|
||||
linked_list.push_back(3)
|
||||
|
||||
linked_list.remove_at(2)
|
||||
output = "0 1 3"
|
||||
printed = linked_list.print()
|
||||
self.assertEqual(output, printed)
|
||||
|
||||
def test_remove_value(self):
|
||||
linked_list = LinkedList()
|
||||
linked_list.push_back(0)
|
||||
linked_list.push_back(1)
|
||||
linked_list.push_back(2)
|
||||
linked_list.push_back(10)
|
||||
linked_list.push_back(3)
|
||||
|
||||
linked_list.remove_value(10)
|
||||
output = "0 1 2 3"
|
||||
printed = linked_list.print()
|
||||
self.assertEqual(output, printed)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue