• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ordered List Question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,

I have to implement an Ordered List using a Linked List, using the following class definitions:

class linkedlist {
int indexOf(Object o) {}
void add(Object o) {}
void addFirst(Object o) {}
void addLast(Object o) {}
void addAtIndex(int index, Object o) {}
Object get(int index) {}
Iterator getIterator() {}
}

class orderedlist {
Comparator comparator;
OrderedList(Comparator c) {
this.comparator = c;
}
int indexOf(Object o);
Object get(int index);
void add(Object o);
Iterator getIterator();
}

I am not asking for you to provide me with the solution, however I was wondering if anyone had any idea what I am supposed to use the getIterator() method for? I am implementing this once using composition, and a second time using inheritance. I am guessing that I have to use an Iterator when adding an item to the List - I will iterate through the list and use a Comparator to compare each item, and then perform the ordered insertion. What is actually supposed to happen in the getIterator() method? Why must there be this method in both OrderedList and LinkedList?

Any help would be appreciated, thanks...
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getIterator would typically be used to return an Iterator that the user can use to walk the list in order and see all of the elements. Usage of it might look like


The key is that the iterator needs to return the items in order.

Jeff
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you have defined your own ADT's (LinkedList etc..), do you have Iterator/Enumeration defined also? Then when getting a such, you should pass an implementation of that (either Iterator/Enumeration) with regards to the underlying datastructure you have. So that the users of that are able to traverse the list.
 
I'm full of tinier men! And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic