| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2333 人关注过本帖
标题:将一个元素加入list和typedef的问题
只看楼主 加入收藏
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:12 
将一个元素加入list和typedef的问题
在写一个添加新元素的程序,要求是将元素添加,然后将光标(个人认为就是curr,现在值)移到新添加数的后面,成功就return1,(例子如下).否则0,这里我不是很理解什么时候是0的情况,如果有大神清楚,请讲解

例子:add 20, returns 1
      ............................
       20   ^  
      ............................
    add 12, returns 1
      ............................
       20   12   ^  
      ............................
    add 33, returns 1
      ............................
       20   12   33   ^  


以下是我目前渣出来的程序,add那里一塌糊涂,并不知道怎么弄
程序代码:
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "listIteratorInt.h"

typedef struct Node {

  // implement struct here .. 
    int 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;

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 it;  // you need to change this...
}

int add(IteratorInt it, int v){
  
  // implement this function
    Node *new;
    new = malloc(sizeof(Node));
  
    new->value = v;
    new->next = NULL;
    it->next = new->next;
    it->curr = new->next;
  
  return 0;  // you need to change this...
}


int hasNext(IteratorInt it){
  
  if(it->curr->next != NULL){
    return 1;
  } else{ return 0;} 
  // implement this function 
  
  // you need to change this...
}

int hasPrevious(IteratorInt it){
  
  if(it->prev != NULL){
    return 1;
  } else{ return 0;}

  // implement this function 
  
    // you need to change this...
}


之后我在测的时候出现以下错误:listIteratorInt.c: In function 'add':
listIteratorInt.c:72:7: error: 'struct IteratorIntRep' has no member named 'next'
     it->next = new->next;
       ^
listIteratorInt.c:73:14: error: assignment from incompatible pointer type [-Werror]
     it->curr = new->next;
              ^
listIteratorInt.c: In function 'hasNext':
listIteratorInt.c:81:14: error: 'struct IteratorIntRep' has no member named 'next'
   if(it->curr->next != NULL){
              ^
listIteratorInt.c: In function 'hasPrevious':
listIteratorInt.c:91:8: error: 'struct IteratorIntRep' has no member named 'prev'
   if(it->prev != NULL){
        ^
想问一下这里要怎么设curr,要怎么改正?
以上是我所有困惑,请高人指点,谢谢!!

[此贴子已经被作者于2016-4-13 13:24编辑过]

搜索更多相关主题的帖子: returns 元素 
2016-04-13 13:13
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
你看不懂英文吗?说得很清楚了,代码把两个结构体搞混了,成员就不配对了。

[此贴子已经被作者于2016-4-13 13:28编辑过]


   唯实惟新 至诚致志
2016-04-13 13:27
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
收藏
得分:0 
回复 2楼 qq1023569223
所以就是把next和curr的typedef位置换一下吗.....表示完全不懂啊
2016-04-13 13:38
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:0 
程序代码:
typedef struct IteratorIntRep {

  // implement struct here .. 
    int nitems;
    struct Node *first;  //这里first,last,curr应该是Node*吧,不然IteratorIntRep*=Node*,强制转换?
    struct Node *last;
    struct Node *curr;
    
} IteratorIntRep;


未佩好剑,转身便已是江湖
2016-04-13 13:54
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
收藏
得分:0 
回复 4楼 alice_usnet
恩, 这里这样好像是对的,谢谢,可是上面那个呢?这两个区别在哪.......
2016-04-13 14:09
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:0 
回复 5楼 cyy06180521
first,last,curr是IteratorIntRep*的话真看不懂,因为first,last,curr都是要指向Node*结点的

未佩好剑,转身便已是江湖
2016-04-13 14:26
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
收藏
得分:0 
回复 6楼 alice_usnet
其实我也真不懂....我是看到其他类似程序第二个是这样的.....
程序代码:
struct IntListNode {
    int  data;  // value of this list item
    struct IntListNode *next;
                // pointer to node containing next element
};

struct IntListRep {
    int  size;  // number of elements in list
    struct IntListNode *first;
                // node containing first value
    struct IntListNode *last;
                // node containing last value
};



那需要把next和prev也写到第二个typedef里吗?谢谢回答!!!
2016-04-13 14:38
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:0 
回复 7楼 cyy06180521
不需要,像4楼那样就行

未佩好剑,转身便已是江湖
2016-04-13 15:06
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
收藏
得分:0 
回复 8楼 alice_usnet
可是它有显示这个:
listIteratorInt.c: In function 'add':
listIteratorInt.c:72:7: error: 'struct IteratorIntRep' has no member named 'next'
     it->next = new->next;
       ^
listIteratorInt.c: In function 'hasPrevious':
listIteratorInt.c:91:8: error: 'struct IteratorIntRep' has no member named 'prev'
   if(it->prev != NULL){
        ^
------------------------------
还有那个,能顺便看一下add要怎么弄吗,无从下手啊.........非常感谢
2016-04-13 15:10
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:0 
回复 9楼 cyy06180521
程序代码:
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 it;  // you need to change this...
}

int add(IteratorInt it, int v){
  
  // implement this function
    Node *new;
    new = malloc(sizeof(Node));
    
    new->value = v;
    new->next = it->curr->next;
    it->curr->next->prev=new;
    it->curr->next=new;
    new->prev=it->curr;
    it->curr=it->curr->next;
  
  return 0;  // you need to change this...
}


int hasNext(IteratorInt it){
  
  if(it->curr->next != NULL){
    return 1;
  } else{ return 0;} 
  // implement this function 
  
  // you need to change this...
}

int hasPrevious(IteratorInt it){
  
  if(it->curr->prev != NULL){
    return 1;
  } else{ return 0;}

  // implement this function 
  
    // you need to change this...
}

未佩好剑,转身便已是江湖
2016-04-13 15:16
快速回复:将一个元素加入list和typedef的问题
数据加载中...
 
   



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

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