[英]How print the fibonacci nodes of a linked list? For example we print 1st, 2nd, 3rd, 5th, 8th etc. nodes in a linked list
我正在嘗試找出最有效的方法。 我有以下代碼用於打印前十個元素的斐波那契數列(使用記憶)
int main()
{
int arrayWithFibIndices[100] = {0}; //Explanation for this array comes later
int series[10] = {0}; //array to store the fibonacci values for last two iterations
series[0] = 1; //hard coding the first two elements of the fib series
series[1] = 2;
for (int i = 0; i < 10; i++)
{
if (series[i] != 0)
{
printf ("series[%d]=%d.\n", i, series[i]);
}
else
{
series[i] = series[i - 1] + series[i - 2];
printf ("series[%d]=%d.\n", i, series[i]);
}
arrayWithFibIndices[series[i]] = 1;
}
}
我也有以下邏輯來迭代打印鏈表。
void printLL(struct node *temp)
{
int i = 0;
while(temp != NULL)
{
if (arrayWithFibIndices[i] != 0) //reason explained later below
{
printf("data:%d ", temp->data);
temp = temp->next;
}
i++;
}
printf("\n");
}
我想弄清楚這樣做最有效的方法是什么? 我想到的第一件事是創建一個新數組 arrayWithFibIndices[] 並將數組的所有元素初始化為 0。每當我們遇到斐波那契值時,我們將在 arrayWithFibIndices[] 中填充該索引為 1。我們可以稍后在打印鏈表的值之前檢查 arrayWithFibIndices[] 的每個索引。
我想到的第二個想法是創建一個隊列。 我會將所有斐波那契元素排入隊列,在打印鏈接列表時,我將在成功匹配時出列(匹配是隊列元素與鏈接列表的第 i 個元素匹配。
大家還有什么建議嗎?
您在描述中建議的解決方案將起作用。 但是,您不需要通過傳遞來獲取所有 Fibanocci 數字的列表。 您可以只跟蹤以前的數字並用於偏移列表中的當前節點。
這是一個簡單的例子,展示了它是如何工作的。
#include <stdio.h>
#include <malloc.h>
struct node {
int data;
struct node *next;
};
void create_linked_list(struct node **root_ptr, int count) {
struct node **curr_ptr = root_ptr;
struct node *curr = NULL;
for (int i = 0; i < count; i++) {
*curr_ptr = (struct node *)malloc(sizeof(struct node *));
curr = *curr_ptr;
curr->data = i + 1;
curr->next = NULL;
curr_ptr = &(curr->next);
}
}
void print_linked_list(struct node *root) {
for(struct node *curr = root; curr != NULL; curr = curr->next) {
printf("%d\n", curr->data);
}
}
void print_linked_list_fib(struct node *root) {
struct node *curr = root;
// Print the root node twice because of 1,1
// 1
printf("%d\n", curr->data);
int old_val = 1;
// 1
printf("%d\n", curr->data);
int curr_val = 1;
int offset = old_val;
while (true) {
// Keep moving until the next fibonacci node.
for (int i = 0; i < offset; i++) {
if (curr == NULL) return;
curr = curr->next;
}
if (curr == NULL) return;
printf("%d\n", curr->data);
old_val = curr_val;
curr_val = curr_val + offset;
offset = old_val;
}
}
int main() {
struct node *root = NULL;
const int count = 20;
create_linked_list(&root, count);
printf("\n\nList of all nodes\n");
print_linked_list(root);
printf("\n\nList of fibonacci nodes\n");
print_linked_list_fib(root);
return 0;
}
這是樣品 output
List of all nodes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
List of fibonacci nodes
1
1
2
3
5
8
13
編輯:為斐波那契節點編寫打印 function 的另一種方法。 這將前一個實現中的兩個循環扁平化為一個循環。
void print_linked_list_fib_loop(struct node *root) {
struct node *curr = root;
// Print the root node twice because of 1,1
// 1
printf("%d\n", curr->data);
int old_val = 1;
// 1
printf("%d\n", curr->data);
int curr_val = 1;
int i = 0;
curr = curr->next;
for(struct node *curr = root; curr != NULL; curr = curr->next) {
if (i == old_val) {
old_val = curr_val;
curr_val = curr_val + i;
i = 0;
printf("%d\n", curr->data);
}
i++;
}
}
如果您還在設計和構建列表,則在構建期間您可以將斐波那契節點的地址復制到單獨的struct node*
數組中。 為了管理節點的任何重新排序,您將在每個節點中存儲列表 position(並在例如添加或刪除節點時更改它)。 如果經常操作列表,這是開銷,但是打印斐波那契節點變得非常有效:只需逐步遍歷指針數組,根本不需要遍歷鏈表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.