[英]Trouble with double-linked list
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. 所以我得到了这个任务来创建一个允许用户在双链表中输入多个整数元素的程序,我必须删除那些可以用它们的数字之和进行分割(余数为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;
}
The problem is, after the deletedividable(list); 问题是,在cutividable(列表)之后; 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. 函数调用,当第二个printlist被调用时没有打印出来,我似乎无法找到问题,一些指针必须搞砸了,但我不确定是哪一个。 Any help would be much appreciated. 任何帮助将非常感激。
Seems an error exists in your insert()
function. 似乎insert()
函数中存在错误。 A hint: insertion into a circular double-linked list should set or change 4 pointers; 提示:插入循环双链表应设置或更改4个指针; you only set 3. 你只设3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.