Linked Lists Question 4: Iterative Linked List The Link class is supposed to pro
ID: 3676372 • Letter: L
Question
Linked Lists
Question 4: Iterative Linked List
The Link class is supposed to provide a linked-list type, as shown in lecture. In contrast to the lecture version, do not use recursion to implement it. In doctests, we've used the ints_list method to supply very long lists that will cause a recursive function to hit too large a stack depth.
Implement the basic methods __len__ and __getitem__ and _get_link. The method _get_link is intended for internal use. Once you implement it, __getitem__ should be very short, and it is useful for later methods as well.
Explanation / Answer
def __len__(self):
temp = Link # Initialise temp
count = 0 # Initialise count
# Loop while end of linked list is not reached
while (temp):
count += 1
temp = temp.next
return count
def __getitem__(self, i):
temp = self.head
while(temp):
if(temp.data == i):
element = temp.data
print temp.data,
temp = temp.next
I have not any idea about the how to get the node link in a given list but i will try to implement that function also.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.