所以我得到了这个任务来制作一个程序,它允许用户在一个双链表中输入一些整数元素,我必须删除那些可以用它们的数字之和除的那些(余数为 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)之后;函数调用,调用第二个打印列表时没有打印任何内容,我似乎无法找到问题所在,某些指针一定是搞砸了,但我不确定哪些指针。任何帮助将非常感激。