将一个元素加入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编辑过]