In
week 8, we learned about another data structure called Linked List which was
defined as linear set of nodes. The front of the sequence is known as the head,
whereas the back of the sequence is known as the tail. In this class we will be
viewing linked list as a sequence of nodes, each with a value and a reference
to the next node. We have been highly encouraged to draw diagrams like below
when working through practice examples and such.
In
class, we defined two classes LLNode to represent a single node and LinkedList
to represent the linked list as whole. These were created in class using the
following code.
In
order to perform certain actions such as adding a value in the list thus
increasing the size or deleting the value thus decreasing the size, would
require a programmer to be able to walk the list. Walking a list is usually of
the following general form
cur_node = self.front
while <some condition here>
#do something here…
cur_node = cur_node.nxt
#after you do something
you this allows you to move on the next
Let’s
walk through the following __contains__ function which was discussed in class
this week.
This
function returns a Boolean statement as to whether or not the linked list
contains the desired value. Our while condition is to walk through the entire
linked list, and we further check that if the value of the cur_node matched the
value we passed on, then we return True and exist the loop. Else, we would move
on the next list, and perform similar action until we find out desired value
and return true. If it be the case that we cannot find the value after walking
along the entire linked list, then we simply return False and exist the while
loop.
I
have also read many of my other classmates slog from which Justin's blog post
on Linked List is definitely worth a read.
Until next time everyone.


.jpg)
.jpg)
No comments:
Post a Comment