• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • 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: 4109
72
Hibernate Fedora Chrome
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic