开发者

Circular linked list going in infinite loop

开发者 https://www.devze.com 2023-04-11 10:11 出处:网络
I am supposed to do a program which can do polynomial addition/subtraction/multiplication/evaluation using circular linked list.

I am supposed to do a program which can do polynomial addition/subtraction/multiplication/evaluation using circular linked list.

My multiplication code is going in infinite loop, and I have marked a comment where it is happening (detected with printf statements, removed).

list* poly_mul(list *p1, list *p2) {
  term tmp;
  list *result = malloc(sizeof(list));
  memcpy(result, p1, sizeof(list));
  node *b = p2->head;
  node *r = result->head;
  do {
    do {
      tmp.exp = r->data.exp + b->data.exp;
      tmp.coeff = r->data.coeff * b->data.coeff;
      unsigned int add_term = 1;
      node *c = result->head;
      do {
        if(c->data.exp == tmp.exp) {
          c->data.coeff += tmp.coeff;
          add_term = 0;
          break;
        }
        c = c->next;
        //Here it goes in infinite loop
      } while(c != result->head);
      if(add_term)
        node_add(result, &tmp);
      b = b->next;
    } while(b != p2->head);
    r = r->next;
  } while(r != result->head);
  return result;
}

The structures used are here:

typedef struct {
  int exp;
  int coeff;
} term;

typedef struct node {
  term data;
  struct node *next;
} node;

typedef struct {
  node *head;
  node *tail;
  unsigned int count;
} list;

And this is the code in main:

void main() {
  list p1, p2, *p3;
  p1.count = p2.count = 0;
  poly_create(&p1);
  p3 = poly_mul(&p1, &p2);
  poly_print(p3);
}

void poly_create(list *l) {
  int i, n;
  printf("\nEnter number of terms in the polynomial: ");
  scanf("%d", &n);
  for(i = 1; i <= n; i++) {
    printf("\nEnter details for term %d: ", i);
    term_append(l);
}

void node_add(list *l, term *t) {
  node *tmp = malloc(sizeof(node));
  memcpy(&tmp->data, t, sizeof(term));
  if(l->count == 0) {
    l->head = tmp;
    l->tail = tmp;
    tmp->next = tmp;
  }
  else {
    l->tail->next = tmp;
    tmp->next = l->head;
    l->tail = tmp;    
  }
  l->count++;
}

void term_append(list *l) {
  term t;
 enter:
  printf("\nEnter term as <coefficient>,<exponent>: ");
  scanf("%d,%d", &t.coeff, &t.exp);
  if(!t.coeff) {
    printf("\nCoefficient is zero, reenter term");
    goto enter;
  }
  if(l->count >= 1) {
    node *i = l->head;
    do {
      if(i->data.exp == t.exp) {
        printf("\nExponent %d was already entered, reenter term", t.exp);
        goto enter;
      }
      i = i->next;
    } while(i != l->head);
    node_add(l, &t);
  }
  else
    node_add(l, &t);
}

Please get me a soluti开发者_开发百科on for this problem, I've been trying to solve this for the past three hours.


Why is it going into an infinite loop? You can find out by using a debugger and stepping through the code. Just put a breakpoint at the appropriate place and you should be able to find it yourself. In all likelihood, you have a loop in your linked list.

You can check for loops in your linked list with two pointers. The first one (tail) point to the start of your list. The second (head) points to the second element of your list. Loop till head is past the last element (I have those pointed to NULL, not head) by incrementing both head and tail by one. If at any point tail > head, you have a loop.


What happens if you printf("%d",(int) c); at each iteration? I suspect that result->head is pointing to a node which is pointing to a member of the linked list, but is not in the linked list itself.

Potential test: Add a int seen to each member of the list and increment it on each member as you loop for a given number of nodes (something excessively high such as INT_MAX) and, when the loop stops, see if result->head->seen > 0:

typedef struct node {
  term data;
  struct node *next;
  // to be removed later
  int seen;
} node;

// place this before you get the infinite loop
unsigned int i = 1;
c->seen = 0;
do
{
    c = c->next;
    c->seen = i;
// replace INT_MAX with some number which is greater than the maximum list length
} while(++i <= INT_MAX);
// this should be roughly equal to i (might be off by 1).
// I'll bet it isn't though!
printf("result->head->seen = %d", result->head->seen);


One possible cause: you're never creating p2. Are you missing a line like this in your main function:

poly_create(&p2);

?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号