Linked lists can be thought about in two ways, as a list of an item and the rest of the list, or as objects which reference the next object in the list. They introduce a new way of creating, viewing, and mutating a list. For example in a traditional list you can use methods such as pop or append mutate the end of the list. For linked lists however, theres no pop function and one can't simply get rid of an item. In a method called "delete" from lecture, we discovered how to do something similar to pop. When comparing linked lists with a python list, the delete method is more like reassigning a list. For example:
>>> lst = [1, 2, 3]
>>> lst
[1, 2, 3]
>>> lst = lst[ 1:]
>>> lst
[2, 3]
Since we named both lists "lst", lst now points to the new list [2, 3]
In a similar way, to delete the first item of a LinkedList, we make its attribute front point to the second item instead of the first item, and make size = size - 1. so similar to the new lst, we don't know where the first item went, but it doesn't matter.
So far, linked lists are interesting but I think they are overcomplicated lists and can't see the benefit of them yet. Hopefully after working with them more my opinion will change.
No comments:
Post a Comment