簡體   English   中英

麻煩雙鏈表

[英]Trouble with double-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被調用時沒有打印出來,我似乎無法找到問題,一些指針必須搞砸了,但我不確定是哪一個。 任何幫助將非常感激。

似乎insert()函數中存在錯誤。 提示:插入循環雙鏈表應設置或更改4個指針; 你只設3。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM