As I mentioned on the last post, we will
discuss about Linked List Node and Linked List.
Basically, Linked List is a list of
multiple Nodes linked together.
If you’re about to picture what Nodes look
like, it will be like simple diagram below.
17| à 5| à 28| à None
when single Node looks like,
17| à None
As some of you can already picture,
shifting previous Node’s pointing to the next Node is how to link Nodes. If you want to visualize,
17| à
None & 5| à
None get linked together, 17’s arrow
will have to point 5
17| à 5| à None
In python, class Linked List Node initialize
with two values, current and next.
class
LLNode:
'''Node to be used in linked list
nxt: LLNode -- next node
None iff at the end of list
value: object --- data for current node
'''
def __init__(self, value, nxt=None):
''' (LLNode, object, LLNode) -> NoneType
Create LLNode (self) with data value and
successor nxt.
'''
self.value, self.nxt = value, nxt
Now, the Linked List has two attributes:
front, and back.
Front refers to the beginning value and
Back refers to the end value of the list.
You can access middle values by doing
front.nxt.nxt.nxt … and so on.
When you code for Linked List, you’ll do
(I’ll use same example I used above)
LL.front = LLNode(17, LLNode(5,
LLNode(28)))
LL.back = LLNode(28, None)
Say you want t access LLNode(5,
LLNode(28)), all you have to do is put ‘.nxt’ after.
>>> LL.front.nxt == LLNode(5,
LLNode(28))
True
But what would the nxt refer if it’s used
after ‘.back’?
As you guessed, yes it is None.
Why?
Because back refers to the end value of
Linked List and last Node points to None (it must!)
Now, if you want to get the value of 5 or
any other value of LLNode, all you need to do is type ‘.value’ after.
>>> LL.front.value == 17
True
>>> LL.front.nxt.value == 5
True
>>> LL.back.value == 28
True
>>> print(LL.back.nxt.value)
None
If you can create a list but cannot modify
it, list won’t come useful to you.
There are many ways to modify list or
search items in list. I will provide
initialize method of Linked List and try to test yourself how much you
understand the material.
Whoever reads my blog, comment below your
own code that functions as follow:
b)
delete_back: deletes the end
value
c)
append : add new Node at end
d) push: add new Node in front
If you want a CHALLENGE, try to write a
code that
e)
prints all the values in list
backwards.
Hint)
B – make sure you modify size of list as
well. (self.size doesn’t change automatically)
C – same thing as mentioned in B, and make
sure the back end points to None
D – don’t forget to modify size, and try
point Node you want to push to rest of values existed in list
E – Recursion.
Besides, drawing diagram REALLY helps a
lot.
The beginning code for Linked List is here:
class
LinkedList:
'''Collection of LLNodes
front:
LLNode -- front of list
back: LLNode -- back of list'''
def __init__(self):
''' (LinkedList) -> NoneType
Create an empty linked list.
'''
self.front, self.back = None, None
self.size = 0
The answer for all will be posted next week. Also, if you face an issue while coding, search in YouTube or ask Google; they are very helpful!
Great explanation on linked list and LLNode!
ReplyDeleteGreat explanation on linked list and LLNode!
ReplyDelete