使用高级功能

时间:2018-04-20 15:22:28

标签: c linked-list doubly-linked-list abstract-data-type

您正在尝试创建存储整数或字符串元素的通用列表迭代器。我正在尝试测试它调用IteratorG advance(IteratorG it, int n)函数的情况,该函数接收列表it和if { {1}}是一个正整数,它向第一个元素前进(移动)n次。如果n为负,它向列表中的最后一个元素前进n次。然后将元素复制到新创建的列表中n并返回列表。如果无法前进n次,则该函数返回NULL。

在下面的测试用例中,在测试用例3中对此进行了测试。 但是,它正在响应分段故障错误,我尝试使用gdp来诊断问题,我怀疑它来自行lis的提前功能

这是推进功能:

add(lis,&(tem->value));

我正在使用Linux环境,错误是结果的指示。测试它所需的其余函数(在测试代码下的测试用例3中测试)应该正常工作。这是整个程序的代码:

IteratorG advance(IteratorG it, int n){

  int zero;
  zero=0;

  IteratorG lis;
  lis = malloc(sizeof (struct IteratorGRep));
  assert (lis != NULL);
  lis->numofit = 0;
  lis->head = NULL;
  lis->tail = NULL;
  lis->curr = NULL;

  Node *tem;
  if ((tem = malloc(sizeof(Node))) == NULL) { 
    return 0;
  }  

  if(n<0 && distanceFromStart(it)!=0 )

  { 
  for(tem=it->curr;n!=zero;it->curr=it->curr->prev)
  {
    add(lis,&(tem->value));
    zero++;

  }
  return lis;
  }
  if(n>0 && distanceToEnd(it)!=0)
  { 
    for(tem=it->curr;n!=zero;it->curr=it->curr->next)
    {
      add(lis,&(tem->value));
      zero++;

    }
    return lis;
  }
  //To be implemented
  //move forward by n times
  return NULL;
}

这是代码的头文件:

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

typedef struct Node {

  void *value;  // value of thee list item
  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;


/*


  //Your  functions below .... 
 */


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

    IteratorG newit;


  if ((newit = malloc(sizeof (struct IteratorGRep)))==NULL)
  {
    printf("Error...! \n");

  }

  //assert (newit != NULL);
  newit->numofit = 0;
  newit->head = NULL;
  newit->tail = NULL;
  newit->curr = NULL;
  newit->cmpElm=cmpFp;
  newit->newElm=newFp;
  newit->freeElm=freeFp;
  return newit;

    // implemented function here and changed return value 
}



int  add(IteratorG it, void *vp){

  Node *temp;
  if ((temp = malloc(sizeof(Node))) == NULL) { 
    return 0; 
  }
  Node *tempe;
  if ((temp = malloc(sizeof(Node))) == NULL) { 
    return 0; 
  }

  temp->value = it->newElm(vp);
  //temp->next=NULL;

  if(it->curr==NULL)
  {
    //temp->next=it->curr;
    it->head=it->tail=temp;
    it->curr=temp;
  }
  else
  {
   tempe=it->curr;
   tempe->prev=temp;
    temp->next=tempe;
    it->curr=tempe;
    it->curr=temp;
    it->head=temp;


  } 

    //it->tail=it->head=it->curr;
    return 1;
  }  
int  hasNext(IteratorG it){

  if(it->curr->next==NULL)
   {
     return 0;
   }


    // check if theres next element/node
    return 1;
}
int  hasPrevious(IteratorG it){

    if(it->curr->prev!=NULL)
    {
      return 1;
    }
    // check if theres previous element/node 
    return 0;
}
void *next(IteratorG it){

  Node *tempo;

  if(it->curr->next==NULL)
  {
    return NULL;
  }
  tempo=it->curr;
  it->curr=it->curr->next;

  // implemented function here  
  return tempo->value;
}
void *previous(IteratorG it){

    Node *tempor;

  tempor=it->curr;
  if(tempor->prev==NULL)
  {
    return NULL;
  }
  tempor=it->curr->prev;

  it->curr=it->curr->prev;
  //tempor=it->curr;

  // move to next node in list  
  return tempor->value;
}
int  del(IteratorG it){
  if(it->curr->prev!=NULL)
  {
    Node *temp_curr=it->curr;
    Node *temp_prev=it->curr->prev->prev;
    temp_curr->prev=temp_prev;
    temp_prev->next=temp_curr;
    return 1;

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

  it->curr->prev->value=vp;





  return 1;
  }
    // change previous node value with new 
    return 0;
}
IteratorG advance(IteratorG it, int n){

  int zero;
  zero=0;

  IteratorG lis;
  lis = malloc(sizeof (struct IteratorGRep));
  assert (lis != NULL);
  lis->numofit = 0;
  lis->head = NULL;
  lis->tail = NULL;
  lis->curr = NULL;

  Node *tem;
  if ((tem = malloc(sizeof(Node))) == NULL) { 
    return 0;
  }   

  if(n<0 && distanceFromStart(it)!=0 )

  { 
  for(tem=it->curr;n!=zero;it->curr=it->curr->prev)
  {
    add(lis,tem);
    zero++;

  }
  return lis;
  }
  if(n>0 && distanceToEnd(it)!=0)
  {  
    for(tem=it->curr;n!=zero;it->curr=it->curr->next)
    {
      add(lis,&(tem->value));
      zero++;

    }
    return lis;
  }
  //To be implemented
  //move forward by n times
  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;// reverse elements of whole list
}
IteratorG find(IteratorG it, int (*fp) (void *vp) ){
    // To be implemented 
    // Find elements of vp in list after current position and put in new list.return the list.
      return NULL;
 }
int distanceFromStart(IteratorG it){

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


  while(c->prev!=NULL)
  {
    c=c->prev;
    count++;
  }
  return count;
    // count number of elements from start of list to current position 

}
int distanceToEnd(IteratorG it){

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

  while(cu->next!=NULL)
  {
    cu=cu->next;
    count++;
  }
  return count;
    // count number of elements from end of list to current position
}
void reset(IteratorG it){



  while(it->curr->prev!=NULL)
  {

    it->curr=it->curr->prev;

  }
  return;
    // move current position to start of list

}
 void freeIt(IteratorG it){
  assert(it != NULL);
  Node *curr, *prev;
  curr = it->head;
  while (curr != NULL) {
    prev = curr;
    curr = curr->next;
   // free(prev->value);
    free(prev);
  }
  free(it); // free items  

}

其中一个功能尚未实现,并在代码本身中指出。但我想这可能不是问题的根源。

编辑: 下面是测试用例代码。仅在上面的程序中,测试用例代码中没有错误:

#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 个答案:

没有答案