473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Implementing Iterators in, e.g., Linked Lists

I am taking a second programming course in Java and am currently
attempting to apply what I have learned using Python instead. One
thing that is puzzling me is how to use an iterator.

I am writing a module containing everything I'd need for canonical
linked lists. One particularly useful feature would be to use a for
loop over the whole structure. For this I have learned the benefits of
iterators. I have read a few book entries on Python iterators, as well
as an online article by David Mertz, I believe, and PEP 234, and I may
be lacking some insight but I am still confused about one thing. How
does the iteration know where to begin?

AFAIU, in my LinkedList class, I can either have the LinkedList be its
own iterator by making its __iter__() method return self and defining
a next() method, or I can have a separate iterator called from
LinkedList's __iter__(). My view is that it would be best to have the
LinkedList be its own iterator - is that the case? Or is an external
iterator preferable in this case?

My problem with implementing the former comes with this: in LinkedList
I would have:

....

def __init__(self):
return self

....

def next(self):
if self.__current.next == None:
raise StopIteration
self.__current = self.__current.next
return self.__current.next

....

Now, is this good in the eyes of more experienced programmers? Also,
do I really want to dedicate an instance variable to keep track of
where to begin iteration (if __current is used for other purposes,
iteration could conceivably begin anywhere right?)? Does this suggest
that I should have a separate LinkedListIterator class? And if I do
have that separate class, do I make the LinkedList.__iter__() pass on
to LinkedListIterator's constructor the head node of LinkedList, or
the whole linked list?

Thanks in advance,

Jeremy
Jul 18 '05 #1
1 1731
adeleinandjeremy wrote:
AFAIU, in my LinkedList class, I can either have the LinkedList be its
own iterator by making its __iter__() method return self and defining
a next() method, or I can have a separate iterator called from
LinkedList's __iter__(). My view is that it would be best to have the
LinkedList be its own iterator - is that the case? Or is an external
iterator preferable in this case?


External iterators are always preferable for two reasons.

1. They allow multiple iterators over the same collection.

2. They keep the interface of the base object clean by separating other
functionality.
--
Rainer Deyke - ra*****@eldwood.com - http://eldwood.com
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Adelein and Jeremy | last post by:
I am taking a second programming course in Java and am currently attempting to apply what I have learned using Python instead. One thing that is puzzling me is how to use an iterator. I am...
9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
7
by: sks_cpp | last post by:
Consider the following: list<int> a; // assume a contains some number of integers list<int>::iterator iter = a.find(10); for (iter; iter < a.end(); ++iter) { std::cout << "found one\n"; }...
4
by: jhonyxxx | last post by:
I have the next programa in C++: #include <iostream.h> // C++ I/O routines #include <list.h> // The STL list class #include<stdio.h> #include <string.h> typedef struct { char nombre; int...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
2
by: Thormod Johansen | last post by:
Hi, I have just read the article about iterators at http://www.oreillynet.com/pub/a/network/2005/10/18/what-is-iterator-in-c-plus-plus.html?page=last and I am wondering about what is ment by the...
9
by: nottheartistinquestion | last post by:
As an intellectual exercise, I've implemented an STL-esque List<and List<>::Iterator. Now, I would like a signed distance between two iterators which corresponds to their relative position in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.