Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9061849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:35:24+00:00 2026-06-16T15:35:24+00:00

I’m trying to write a function that calculates the N-th Fibonacci number using doubly

  • 0

I’m trying to write a function that calculates the N-th Fibonacci number using doubly linked lists, but for some reason when I compile and run the linked list does not stop growing, it keeps adding 1 number over and over with no ending.

This should be a SSCCE:

#include <iostream>
using namespace std;


class node {
    public:
        int value;
        node* previous;
        node* next;
};//node

class number {
    public:
        node* start;
        node* end;
        node* add (int value);
        void show (int K);
        number ();
        void destroy ();

        void copy (number gg1);
        void addition (number gg1, number gg2, int K);

        void fibonacci (int K, int times);

};//number

number::number () {
    start = NULL;
    end = NULL;
}

int power (int K) {
    int L = 1;
    for (int i = (K-1); i > 0; i--) {
        L = L*10;
    }
    return L;
}

int checksize (int value) {
    int counter = 0;
    while (value != 0) {
        value = value / 10;
        counter += 1;
    }
    return counter;
}

void number::show (int K) {
    node* current;
    cout << "\nValue:" << endl;
    if (start == NULL) {
        cout << "\nNothing\n" << endl;
    }
    if (start != NULL) {
        current = start;
        while (current != NULL) {
            if (current->value == 0) {
                for (int i = 0; i < K; i++) {
                    cout << "0";
                }
            cout << "\n";
            }
            else {
                int size = checksize (current->value);
                for (int j = size; j < K; j++) {
                    cout << "0";
                }
            cout << current->value << endl;
            }
            current = current->next;
        }   
    }
    //cout << "\n";
}

int main () {
    number gg1;
    number gg2;
    number gg3;
    const int K = 5;

    gg1.fibonacci (K, 10);
}

node* number::add(int value) {   
        node* currentcode;                   
        if (start == NULL){                    
            currentcode = new node;      
            start = currentcode;               
            end = currentcode;            
            currentcode->next =  NULL;    
            currentcode->previous = NULL;    
            currentcode->value = value;
            return currentcode;
        }
        if (start != NULL) {                    
            currentcode = new node;    
            currentcode->next = NULL;   
            end->next = currentcode;  
            currentcode->previous = end;  
            end = currentcode;          
            currentcode->value = value;
            return currentcode;
        }
         return NULL;
}

void number::addition (number gg1, number gg2, int K) {
    int value1, value2, value3;
    int carry = 0;
    node* current1;
    node* current2;
    current1 = gg1.start;
    current2 = gg2.start;
    while (current1 != NULL || current2 != NULL) {
        if (current1 != NULL && current2 !=NULL) {
            value1 = current1->value;
            value2 = current2->value;
            value3 = value1 + value2 + carry;
            current1 = current1->next;
            current2 = current2->next;
        }
        else if (current1 == NULL && current2 != NULL) {
            value3 = current2->value + carry;
            current2 = current2->next;
        }
        else if (current1 != NULL && current2 == NULL) {
            value3 = current1->value + carry;
            current1 = current1->next;
        }

        checksize(value3);
        if (value3 > power(K)) {
            value3 = value3 - 10*(power(K));
            carry = 1;
        }
        else 
            carry = 0;

        add(value3);

        if ((current1 == NULL && current2 == NULL) && (carry == 1))
            add(1);
    }
}

void number::destroy () {
    node* current;
    node* current2;
    if (start != NULL) {
        current = start;
        current2 = current->next;
        while (current2 != NULL) {
            delete current;
            current = current2;
            current2 = current->next;
        }
        delete current;
    }
}   

void number::fibonacci (int K, int times) {
    number g1;
    number g2;
    number g3;
    destroy ();

    g1.add (1);
    g2.add (1);

    g3.addition (g1, g2, K);
    g2.copy(g1);

    g1.show(K);
    g2.show(K);

        //g1.copy(g3);

        //g1.show(K);
        //g2.show(K);
        //g3.show(K);

        //g3.addition (g1, g2, K);
        //g3.show(K);
        //g2.copy(g1);
        //g1.copy(g3);

    /*for (int i = 0; i < 2; i++) {
        g3.addition (g1, g2, K);
        g3.show(K);
        g2.copy(g1);
        g1.copy(g3);
    }*/

    copy(g3);
}

void number::copy (number gg1) {
    int value;
    destroy ();
    node* current = gg1.start;
    while (current != NULL) {
        value = current->value;
        add(value);
        current = current->next;
    }
}

Whenever I run the Fibonacci function it gives me endless 1’s in the terminal.
The number class is just a basic doubly linked pointer list.

The addition function standalone works just fine, so does the copy. In fact everything was working fine until this. It’s easy to finish the function with a for-loop, but this error prevents me from doing so. Does anyone know what my mistake is? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:35:25+00:00Added an answer on June 16, 2026 at 3:35 pm

    Right now, you have invalid memory access, since calling delete on each node in destroy() does not NULL-out the memory but it only marks the memory free.

    Suggested correction:

    void number::destroy () {
        node* current;
        node* current2;
        if (start != NULL) {
            current = start;
            current2 = current->next;
            while (current2 != NULL) {
                delete current;
                current = current2;
                current2 = current->next;
            }
            delete current;
        }
        start = NULL; // so you can't access the now non-existing list anymore.
        end = NULL;
    }
    

    Remark:

    • Class names should be capital-first by widely adapted convention.
    • You should not pass a class by value in your copy and addition function but const-ref.
    • Better use in this case operator= instead of copy. copy can be copy_from or copy_to, calling a function copy is ambigous, really.
    • Always better to use for loops when you can.
    • Node is not a class but a struct, it is better to call it a struct.

    The new code can also look like this:

    Number& Number::operator=(const Number& n)
    {
      destroy();
      for(Node* current = gg1.start; current; current = current->next)
        add(current->value);
    }
    
    void Number::destroy() 
    {
        Node* temp;
        for(Node* current = start; current; current = current->next, delete temp)
          temp = current;
        start = NULL;
        end = NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.