问题
Here's the problem.
Write a function for merging multiple (sorted) linked lists into one sorted linked list. This function should access the elements through Iterator interface (do not access elements through the linked list directly). The arguments to the merge procedure are an array of Iterators and the size of the array. The return value should be another Iterator with an underlying List implementation.
Steps:
(1) Implement linked list with iterator interface. Define the Element in the list as below:
typedef struct
{
int idno;
char name[25];
float marks;
} Element;
(a) List createList();
(b) List insert(List L, Element e );
(c) Void printList(List L);
(d) iterator initIterator(List L);
(e) boolean hasMoreElements(iterator I);
(f) iterator moveNext(iterator I);
(2) Implement Merge function.
iterator merge(iterator I[],int size)
This function will merge the elements in all the lists ordered by the attribute “marks”. Merge function should access the list through the iterator functions.
(3) Implement driver function.
Populate the lists from the input files (provided as support). Call the merge function and store the data in resultant merged list to an output file.
Support files: test1.txt, test2.txt, test3.txt, test4.txt, test5.txt, test6.txt, test7.txt, test8.txt
Deliverables: dataDef.h, mergeOps.c, mergeOps.h, main.c, output.txt
Now i dont want the solution for this, but i want to know what an iterator interface is. I have never heard of it before.
And how do i go about implementing linked list with iterator interface.What does it mean?
Also it uses a data type of iterator what that would be?
回答1:
An iterator is simply a generalized term for something that allows you to traverse a container (like an array, list, etc).
From wikipedia,
In computer programming, an iterator is an object that enables a programmer to traverse a container. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterator are fixed, iterators are often implemented in terms of the structures underlying a container implementation and are often tightly coupled to the container to enable the operational semantics of the iterator. Note that an iterator performs traversal and also gives access to data elements in a container, but does not perform iteration (i.e., not without some significant liberty taken with that concept or with trivial use of the terminology). An iterator is behaviorally similar to a database cursor.
As your assignment talks about creating an iterator without access the elements directly, you can have a look at Iterator Design Pattern
More information on Iterator
- Iterator implementation in C
- Iterator pattern in OOP
- Do we still need Iterator design pattern?
- Iterator design pattern in C#
来源:https://stackoverflow.com/questions/7369969/implementing-linked-list-with-iterator-interface