Linked lists are made up of nodes, each node in the list refers to the next node, shown in the diagram below.
- linked lists can be empty, which is represented by an empty node (or in python: None).
- can be list of nodes referring to the next node.
- each list has a a front/head and a back/tail, where back/tail has the nest node = None.
How are Linked Lists useful?
- linked lists are useful for making abstract data structures such as lists, queues, and stacks.
- the advantage of using L.L is that items can be easily inserted or removed without interrupting the other data in the list.
Linked Lists in python.
- inorder to write the code for the linked list, first make a diagram of how the linked list work. (this is the basic idea for writing any code, MAKE A DIAGRAM!). in this case we will use the one shown above.
- first we need to store a node and a reference to next node. thus write a class with the __init__ consisting of node, and next. set the "next", by default, to None. so if there is no reference to another node, it will be, by default, None.
- next we need to write code for front/head and back/tail (advantage of this is that we can easily find front and back and insert/remove object at that place). Implement another class and leave the parameters of __init__ empty. but inside the body set self.front and self.back to None.
- the way appending and prepending to a linked list node is made much much simpler.
- To write any complicated code, make a diagram, understand what is happening in the diagram and you'll instantly know of how to write the code. (Writing the code itself may take a while).
- when writing for/while loops (or looping through anything), make a tree of what value am i looping through and what would happen to that value if something is applied to it.

No comments:
Post a Comment