使用迭代器接口实现链表 [英] Implementing linked list with iterator interface

查看:155
本文介绍了使用迭代器接口实现链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于此。

编写一个函数,用于将多个(已排序)链接列表合并到一个已排序的链接列表中。这个
函数应该通过Iterator接口访问元素(不要直接通过链表访问元素
)。合并过程的参数是
迭代器的数组和数组的大小。返回值应该是具有
基础List实现的另一个Iterator。

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.

步骤:

( 1)使用迭代器接口实现链表。
在列表中定义元素如下:

(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)列表createList();

(a) List createList();

(b)列表插入(列表L,元素e);

(b) List insert(List L, Element e );

(c)无效打印列表(列表L);

(c) Void printList(List L);

(d)迭代器initIterator(List L);

(d) iterator initIterator(List L);

(e)boolean hasMoreElements(iterator I);

(e) boolean hasMoreElements(iterator I);

(f)迭代器moveNext(迭代器I);

(f) iterator moveNext(iterator I);

(2)实现合并功能。

iterator merge(iterator I[],int size)

此函数将合并由属性
marks排序的所有列表中的元素。合并函数应该通过迭代器函数访问列表。

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)实现驱动程序函数。

(3) Implement driver function.

填充输入文件中的列表(作为支持提供)。调用merge
函数并将生成的合并列表中的数据存储到输出文件中。

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.

支持文件:test1.txt,test2.txt,test3.txt ,test4.txt,test5.txt,test6.txt,test7.txt,test8.txt

Support files: test1.txt, test2.txt, test3.txt, test4.txt, test5.txt, test6.txt, test7.txt, test8.txt

可交付成果:dataDef.h,mergeOps.c,mergeOps.h,main .c,output.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?

它还使用数据类型 iterator 那会是什么?

Also it uses a data type of iterator what that would be?

推荐答案

迭代器只是一个通用术语,允许您遍历容器(如数组,列表等)。

An iterator is simply a generalized term for something that allows you to traverse a container (like an array, list, etc).

来自维基百科


在计算机编程中,迭代器是一个对象,它使
程序员能够遍历容器。各种类型的迭代器通常是通过容器的接口提供的
。虽然给定迭代器的接口和
语义是固定的,但迭代器通常是根据容器
实现的基础结构实现的
,并且通常与容器紧密耦合到
启用迭代器的操作语义。请注意,
迭代器执行遍历并且还允许访问
a容器中的数据元素,但不执行迭代(即,不是没有使用该概念或通过使用的概念获得的一些
重要自由
术语)。迭代器的行为类似于数据库
游标。

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

有关迭代器的更多信息

  • Iterator implementation in C
  • Iterator pattern in OOP
  • Do we still need Iterator design pattern?
  • Iterator design pattern in C#

这篇关于使用迭代器接口实现链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆