簡體   English   中英

循環鏈表陷入無限循環

[英]Circular linked list going in infinite loop

我應該做一個可以使用循環鏈表進行多項式加法/減法/乘法/求值的程序。

我的乘法代碼陷入無限循環,並且在發生的地方標記了一條注釋(通過printf語句檢測到,已刪除)。

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;
}

使用的結構在這里:

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;

這是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);
}

請給我一個解決此問題的方法,過去三個小時我一直在努力解決該問題。

為什么會進入無限循環? 您可以使用調試器並單步執行代碼來找出答案。 只需在適當的位置放置一個斷點,您就可以自己找到它。 很有可能,鏈表中有一個循環。

您可以使用兩個指針檢查鏈接列表中的循環。 第一個(尾部)指向列表的開頭。 第二個(頭)指向列表的第二個元素。 循環直到head通過最后一個元素(我將那些指向NULL,而不是head)都通過將head和tail都加一。 如果在任何時候尾巴>頭,您都有一個循環。

如果printf("%d",(int) c);會發生什么? 在每次迭代? 我懷疑result-> head指向一個節點,該節點指向鏈表的成員,但不在鏈表本身中。

潛在測試:在列表的每個成員上添加一個int seen ,並在循環給定數量的節點(例如INT_MAX之類的過高)時在每個成員上遞增int seen ,並在循環停止時查看result-> head- >看到> 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);

可能的原因之一:您永遠不會創建p2。 您是否在main功能中缺少這樣的一行:

poly_create(&p2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM