0

そこで、このタスクを実行して、ユーザーが二重リンクリストに整数要素の数を入力できるようにするプログラムを作成しました。数字の合計で除算できる要素(余り0)を削除する必要があります。

#include <stdio.h>
#include <stdlib.h>
#define NEW(t) (t*)malloc(sizeof(t))

typedef int info_t;

typedef struct element {
  info_t info;
  struct element *next;
  struct element *prev;
} node;

typedef node* nodep;
void insert(nodep l, info_t x) {
  nodep t = NEW(node);
  t->info=x;
  t->next=l->next;
  l->next=t;
  t->prev=l;
}
void printList(nodep l) {
  nodep t=l->next;
  while(t!=l)
  {
      printf("->%d", t->info);
      t=t->next;
  }
  printf("\n");
}
void deletedividable(nodep l) {
  nodep t=l->next;
  nodep temp;
  while(t->next!=l)
  {
      int temporary=t->info;
      int sum=0;
      while(temporary>0)
      {
          sum+=(temporary%10);
          temporary/=10;
      }
      if(!(t->info%sum))
      {
          temp=t->next;
          t->next->prev=t->prev;
          t->prev->next=t->next;
          free(t);
          t=temp;
      }
      else
        t=t->next;
   }
}

int main() {
  // declaring a leader node
  nodep list = NEW(node);
  list->next = list;
  list->prev = list;

  printf("Enter elements:\n ");
  int a;
  //if the input isn't a number the loop will exit
  while(scanf("%d", &a)) {
    //elements input function call
    insert(list, a);
  }
  // print list function call
  printList(list);
  // delete elements which are dividable with the sum of their digits

  deletedividable(list);

  printList(list);

  return 0;
}

問題は、deletedividable(list);の後です。関数呼び出し、2番目のプリントリストが呼び出されても何も出力されず、問題を特定できないようです。一部のポインターが失敗している必要がありますが、どれかわかりません。どんな助けでも大歓迎です。

4

1 に答える 1

2

関数にエラーが存在するようinsert()です。ヒント:循環二重リンクリストへの挿入は、4つのポインターを設定または変更する必要があります。3を設定するだけです。

于 2011-05-11T22:26:12.313 に答える