我們初學者應該互相幫助。
的分配是不容易做到這樣的初學者作爲你和我。:)
給你。
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert(struct node **head, const int a[], size_t n)
{
if (*head != NULL) head = &(*head)->next;
for (size_t i = 0; i < n; i++)
{
struct node *tmp = malloc(sizeof(struct node));
tmp->data = a[i];
tmp->next = *head;
*head = tmp;
head = &(*head)->next;
}
}
void checkModOfPrevSum(struct node *head, long long int sum)
{
if (head != NULL)
{
if (head->next != NULL)
{
checkModOfPrevSum(head->next, sum + head->data);
printf(" - ");
}
printf("%d [%lld] (%s)", head->data, sum, sum % head->data == 0 ? "YES" : "NO");
}
}
int main(void)
{
struct node *head = NULL;
int a[] = { 5, 2, 3, 6, 1, 7, 4 };
const size_t N = sizeof(a)/sizeof(*a);
insert(&head, a, N);
checkModOfPrevSum(head, 0);
return 0;
}
程序輸出是
4 [24] (YES) - 7 [17] (NO) - 1 [16] (YES) - 6 [10] (NO) - 3 [7] (NO) - 2 [5] (NO) - 5 [0] (YES)
當然的列表實現是不完整的。只要你喜歡,你可以進一步發展。
你有沒有嘗試過? :-)如果是這樣,請將其添加到問題中,並嘗試確保它是[mcve]。 – George
歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [在主題](http://stackoverflow.com/help/on-topic)和[如何提問](http://stackoverflow.com/help/how-to-ask)適用於此處。 StackOverflow不是一個設計,編碼,研究或教程服務。 – Prune
[「有人能幫助我嗎?」是不是一個有效的SO問題)[https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question)。這通常表明,你需要的是半個小時的時間與當地的導師,或通過一個教程,而不是堆棧溢出。 – Prune