c 的迭代器和c语言的指针的区别,C中用迭代器做指针 求教

这篇博客讨论了C语言中使用迭代器进行元素插入的问题。作者指出ListIterator没有当前元素的概念,并需要编写一个程序,根据元素v在列表中的位置,决定使用next()还是previous()方法。如果v在当前元素之前,插入后返回next(),之后则返回previous()。若列表为空,v将成为唯一元素。此外,新元素插入后,不会影响后续next()的调用,但previous()会返回新元素。文章中提供了部分未完成的代码实现,包括IteratorInt结构体定义和相关操作函数,如add()、hasNext()、hasPrevious()等,作者请求帮助理解迭代器插入的位置判断及返回值含义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C中用迭代器做指针 求教

不是很懂迭代器的作用,书上说List Iterator是没有现在元素的,然后要写一个程序加入元素v,要求是如果v在之前插入,就返回next(),如果是之后,就返回previous();(如果list中没有元素,v就是唯一元素);在隐式游标之前插入新的元素:下一次调用将不受影响,随后的调用将返回新的元素。返回1如果成功,0否则。如下,不知道我有没有表达出正确的意思.......:

Inserts the specified value v into the list iterator it. The element is inserted immediately before the element that would be returned by next(), if any, and after the element that would be returned by previous(), if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element. Returns 1 if successful, 0 otherwise.

问题就是我不懂到底这个新的元素插在哪个位置,怎么辨别是用next()还是previous(),后面的返回1还是0这一方面也不是很理解,求大神讲解啊----来自心力交瘁的我

21c92f7342edc52acb5684b0b88bdcec.png程序代码:#include

#include

#include

#include "listIteratorInt.h"

typedef struct Node {

//implement struct here .. char *value;

struct Node *next;

struct Node *prev;

} Node;

typedef struct IteratorIntRep {

//implement struct here .. int nitems;

struct IteratorIntRep *first;

struct IteratorIntRep *last;

struct IteratorIntRep *curr;

} IteratorIntRep;

/*Your local functions here, if any....*/

IteratorInt IteratorIntNew(){

//implement this function struct IteratorIntRep *it;

it = malloc(sizeof (struct IteratorIntRep));

assert (it != NULL);

it->nitems = 0;

it->first = NULL;

it->last = NULL;

it->curr = NULL;

return NULL; //you need to change this...}

void reset(IteratorInt it){

//implement this function//重置到列表it的开始

}

int add(IteratorInt it, int v){

//implement this function//加入元素v,要求是如果v在之前插入,就返回next(),如果是之后,就返回previous();(如果list中没有元素,v就是唯一元素);在隐式游标之前插入新的元素:下一次调用将不受影响,随后的调用将返回新的元素。返回1如果成功,0否则。

assert (it != NULL);

return 0; //you need to change this...}

int hasNext(IteratorInt it){

//implement this function//如果有下一元素就返回1

return 0; //you need to change this...

}

int hasPrevious(IteratorInt it){

//implement this function//如果有上一元素就返回1

return 0; //you need to change this...}

int *next(IteratorInt it){

//implement this function//返回给定的列表中的下一个值的指针,并在光标位置前进

return NULL; //you need to change this...}

int *previous(IteratorInt it){

//implement this function//返回给定的列表中的上一个值的指针,并在光标位置前进

return NULL; //you need to change this...

}

int delete(IteratorInt it){

//implement this function//从列表中删除迭代器的最后一个值,是由下一个或前或findprevious或FindNext或返回。这个命令只能调用一次每次到下一个或前或findprevious或FindNext。它可以调用只有当add或reset尚未被命令在最后一个命令之后给next或 next或previous或findNext或findPrevious。返回1,如果成功,否则0(例如,无效的命令)

return 0; //you need to change this...}

int set(IteratorInt it, int v){

//implement this function//替换最后一个元素返回通过next或 next或previous或findNext或findPrevious.它可以调用只有当remove或add或reset尚未被命令在最后一个命令之后给next或next或previous或findNext或findPrevious。返回1,如果成功,否则0(例如,无效的命令)

return 0;

}

int *findNext(IteratorInt it, int v){

//implement this function//返回在v在it中下一个指针的值,并在光标位置前进.返回NULL如果没有下一个值.

return 0;

}

int *findPrevious(IteratorInt it, int v){

//implement this function//返回在v在it中上一个指针的值,并在向后移动光标位置.返回NULL如果没有上一个值.

return 0;

}

这是程序原型,前面的typedef也是我写的,不知有没有错误,求讲解.......谢谢!!

[此贴子已经被作者于2016-3-31 15:07编辑过]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值