麻烦双链表

时间:2011-05-11 21:36:32

标签: c list doubly-linked-list

所以我得到了这个任务来制作一个允许用户在双链表中输入多个整数元素的程序,我必须删除那些可以被分割的数(余数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;
}

问题是,在cutividable(列表)之后;函数调用,当第二个printlist被调用时没有打印出来,我似乎无法找到问题,一些指针必须搞砸了,但我不确定是哪一个。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

insert()函数中似乎存在错误。提示:插入循环双链表应设置或更改4个指针;你只设3个。

相关问题