编译错误struct没有名为next和prev的成员

时间:2018-04-16 17:29:10

标签: c nodes function-pointers doubly-linked-list abstract-data-type

嘿我正在尝试实现一个通用列表迭代器,它可以存储任何类型的元素。它有其他文件来处理正整数类型和字符串类型。但是,我收到一个错误,指出结构IteratorGRep没有名为'的成员接下来'和'prev'。节点next和prev都已经声明了,这应该工作正常但不确定错误在哪里。我正在使用linux环境,错误是编译时的指示。这里是代码:

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "iteratorG.h"

typedef struct Node {

  char   *value;  // value of thee list item (string)
  struct Node *prev;
  // pointer previous node in list
  struct Node *next;
  // pointer to next node in list

  // implemented struct here .. 
} Node;

typedef struct IteratorGRep {

  int  numofit;      // count of items in list
  Node *head;      // first node in list
  Node *curr;       // current node in list
  Node *tail;       // last node in list

  ElmCompareFp  cmpElm;
  ElmNewFp  newElm;
  ElmFreeFp freeElm;

  // implemented struct here .. 

} IteratorGRep;


/*


  // functions below .... 
 */


IteratorG newIterator(ElmCompareFp cmpFp, ElmNewFp newFp, ElmFreeFp freeFp){

    struct IteratorGRep *it;

  it = malloc(sizeof (struct IteratorGRep));
  assert (it != NULL);
  it->numofit = 0;
  it->head = NULL;
  it->tail = NULL;
  it->curr = NULL;
  it->cmpElm=cmpFp;
  it->newElm=newFp;
  it->freeElm=freeFp;
  return it;

    // created list 
}

int  add(IteratorG it, void *vp){

  Node *temp=NULL;
  temp=it->next;
  it->next=vp;
  vp->prev=it;
  vp->next=temp;
  temp->prev=vp;

    // Inserts element pointed by 'vp' into list iterator 'it' 
    return 1;
}
int  hasNext(IteratorG it){

  if(it->next!=NULL)
   {
     return 1;
   }

    // check for any next elements  
    return 0;
}
int  hasPrevious(IteratorG it){

    if(it->prev!=NULL)
    {
      return 1;
    }
    // check for any previous elements 
    return 0;
}
void *next(IteratorG it){
  if(it->next!=NULL)
  {
    it=it->next;
    Node *curre=it->curr;
    return curre;

  }
    // move to next element
    return NULL;
}
void *previous(IteratorG it){

    if(it->prev!=NULL)
    {
      it=it->prev;
      Node *curre=it->curr;
      return curre;

    }

    // moves to previous element
    return NULL;
}
int  del(IteratorG it){
  if(it->prev!=NULL)
  {
    Node *temp_curr=it->curr;
    Node *temp_prev=it->prev->prev;
    temp_curr->prev=temp_prev;
    temp_prev->next=temp_curr;
    return 1;

  }
     // removes previous element from list 
  if(it->prev==NULL)
  {
    return 0;
  }
}
int  set(IteratorG it, void *vp){
  if(it->prev!=NULL)
  {

  Node *store_curr=it->curr;
  Node *store_prev=it->prev->prev;

  store_curr->prev=vp;
  vp->next=store_curr;
  store_prev->next=vp;
  vp->prev=store_prev;
  return 1;
  }
    // Replaces previous element with the element (*vp) 
    return 0;
}
IteratorG advance(IteratorG it, int n){


    // Advance by n times and return list with n times of elements
    // To  implement function here and change return value 
    return NULL;
}
void reverse(IteratorG it){
  Node *curr = it->head;
  Node *temp = NULL;
  while(curr != NULL) {
    temp = curr->next;
    curr->next = curr->prev;
    curr->prev = temp;
    curr = temp;
  }
  temp = it->head;
  it->head = it->tail;
  it->tail = temp;    

    // reverses order of list  
}
IteratorG find(IteratorG it, int (*fp) (void *vp) ){

    // finds elements after current position,append to new list
    // To implement function here and change return value 

     return NULL;
}

int distanceFromStart(IteratorG it){

  Node *c=it->curr;
  int count=0;

  if (c->prev==NULL){
    return 0;  
  }

  while(c->prev!=NULL)
  {
    c=c->prev;
    count++;
    return count;
  }


     // counts number of nodes from current position to start of list 

}
int distanceToEnd(IteratorG it){

  Node *cu=it->curr;
  int count=0;

  if (cu->next==NULL){
    return 0;  
  }
  while(cu->next!=NULL)
  {
    cu=cu->next;
    count++;
    return count;
  }
    // counts number of nodes from current position to end of list  

}
void reset(IteratorG it){

  it->curr=it->head;

    // reset to start of list  
    return;
}
void freeIt(IteratorG it){
  assert(it != NULL);
  Node *curr, *prev;
  curr = it->first;
  while (curr != NULL) {
    prev = curr;
    curr = curr->next;
    free(prev->value);
    free(prev);
  }
  free(it); 

    // remove nodes in it and free memory  

}

这是代码的头文件:

#ifndef LISTITERATORG_H
#define LISTITERATORG_H

#include <stdio.h>

typedef struct IteratorGRep *IteratorG;

typedef int   (*ElmCompareFp)(void const *e1, void const *e2);
typedef void *(*ElmNewFp)(void const *e1);
typedef void  (*ElmFreeFp)(void *e1);


IteratorG newIterator(ElmCompareFp cmpFp, ElmNewFp newFp, ElmFreeFp freeFp);
int  add(IteratorG it, void *vp);
int  hasNext(IteratorG it);
int  hasPrevious(IteratorG it);
void *next(IteratorG it);
void *previous(IteratorG it);
int  del(IteratorG it);
int  set(IteratorG it, void *vp);
IteratorG advance(IteratorG it, int n);
void reverse(IteratorG it);
IteratorG find(IteratorG it, int (*fp) (void *vp) );
int distanceFromStart(IteratorG it);
int distanceToEnd(IteratorG it);
void reset(IteratorG it);
void freeIt(IteratorG it);

#endif

其中一个功能尚未实现,并在代码本身中显示。但我猜这可能不是问题的根源。我认为这里没有正确使用节点。

0 个答案:

没有答案