| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1680 人关注过本帖
标题:C中用迭代器做指针 求教
只看楼主 加入收藏
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
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这一方面也不是很理解,求大神讲解啊----来自心力交瘁的我

程序代码:
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#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编辑过]

2016-03-31 14:22
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
这是C++的东西,你当C用。

授人以渔,不授人以鱼。
2016-03-31 16:32
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
收藏
得分:0 
回复 2楼 TonyDeng
是C++和Java的东西,但是就是让用到C里面,就是可能是同一种思路,但不完全是一个东西,233333333333333333333
2016-04-01 12:44
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
收藏
得分:0 
没有人指教一下吗求大神啊
2016-04-02 20:35
快速回复:C中用迭代器做指针 求教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034973 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved