Вопрос

So I got this task to make a program which will allow the user to enter a number of integer elements in a double-linked list and I have to delete the ones which can be divided (remainder 0) with the sum of their digits.

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

The problem is, after the deletedividable(list); function call, nothing is printed when the second printlist is called and I can't seem to locate the problem, some of the pointers must be getting screwed up, but I'm not sure which ones. Any help would be much appreciated.

Нет правильного решения

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top