C++ binary max heap Read Access Violation - c++

So I'm making a binary max heap for my c++ class and I keep encountering this exception saying that I'm returning a nullptr in my class methods. I'm having trouble finding where it could be at in my code so any help would be greatly appreciated. The exception that keeps getting thrown is as follows:
Unhandled exception thrown: read access violation.
**std::_String_alloc<std::_String_base_types<char,std::allocator<char> > ::_Get_data**(...) returned nullptr. occurred
Here's my header file for the tree:
#ifndef HEAP_H
#define HEAP_H
#include <iostream>
#include "THeapNode.h"
class TreeHeap {
private:
THeapNode *root; // the top node of the heap
int next_loc; // the next valid location to place a node
void bubble_up(THeapNode *node); //performs the bubble up operation on the given node
void bubble_down(THeapNode *node); //performs the bubble down operation on the given node
void clear_heap(THeapNode *node); // removes all elements from the heap
public:
TreeHeap(); // the constructor
~TreeHeap(); // the destructor
THeapNode* find_node(const int position); // finds the node in the position given and returns a pointer to it
void insert(const string &item); // creates a node with the string given as the value and places it in the next location
bool Delete(); // removes the root of the heap and returns false if the tree is empty
};
#endif
Here's my Node's .h file:
#ifndef NODE_H
#define NODE_H
#include<string>
using std::string;
struct THeapNode {
string data; // stores a data string
THeapNode *parent; // a pointer to the parent node
THeapNode *rightChild; // a pointer to the right child node
THeapNode *leftChild; // a pointer to the left child node
THeapNode( const string &str );
};
#endif
And here's my .cpp file:
#include "stdafx.h"
#include <cmath>
#include "TreeHeap.h"
#include "THeapNode.h"
TreeHeap::TreeHeap() {
root = NULL;
next_loc = 1;
};
TreeHeap::~TreeHeap() {
THeapNode *current = root;
THeapNode *deleteNode = NULL;
int d = floor(log2(next_loc - 1));
int j = next_loc;
for ( int i = 1; i <= j-1; i++ ) {
while (1) {
int d = floor(log2(next_loc - i));
int power = std::pow(2, d - 1);
if (next_loc == 1) {
deleteNode = current;
j -= 1;
}
else if (next_loc < (std::pow(2, d - 1) * 3)) {
current = current->leftChild;
}
else {
current = current->rightChild;
}
// Update location and depth to reflect traversal
next_loc = std::pow(2, d - 1) + next_loc % power;
d = d - 1;
}
clear_heap(deleteNode);
}
}
void TreeHeap::clear_heap(THeapNode *node) {
if (node == NULL) {
return;
}
node->data = "";
node->leftChild = NULL;
node->rightChild = NULL;
node->parent = NULL;
}
void TreeHeap::insert( const string &value ) {
THeapNode newNode = THeapNode(value);
int loc = next_loc;
if (loc == 1) {
*root = newNode;
}
THeapNode *current = root;
while (1) {
int d = floor(log2(loc));
int power = std::pow(2, d - 1);
if (loc < (power * 3)) {
if (current->leftChild = nullptr) {
*current->leftChild = newNode;
newNode.parent = current;
next_loc += 1;
break;
}
else {
current = current->leftChild;
}
}
else {
if (current->rightChild = nullptr) {
*current->rightChild = newNode;
newNode.parent = current;
next_loc = +1;
break;
}
else {
current = current->rightChild;
}
}
// Update location and depth to reflect traversal
loc = std::pow(2, d - 1) + loc % power;
d = d - 1;
}
std::cout << current->data << "\n";
system("PAUSE");
}
void TreeHeap::bubble_up( THeapNode *node ) {
if (node == NULL) {
return;
}
THeapNode *parent = node->parent;
while ( parent->data < node->data ) {
parent = node->parent;
string temp = parent->data;
parent->data = node->data;
node->data = temp;
}
}
void TreeHeap::bubble_down( THeapNode *node ) {
if (node == NULL) {
return;
}
while( node->data < node->rightChild->data || node->data < node->leftChild->data ){
if (node->rightChild->data > node->leftChild->data) {
THeapNode *right = node->rightChild;
string temp = right->data;
right->data = node->data;
node->data = temp;
}
else if (node->rightChild->data < node->leftChild->data) {
THeapNode *left = node->leftChild;
string temp = left->data;
left->data = node->data;
node->data = temp;
}
}
}
THeapNode* TreeHeap::find_node( const int position ){
int loc = position;
int d = floor(log2(position));
int power = std::pow(2, d - 1);
THeapNode *returnValue = root;
while (returnValue != NULL && 1 < position && position < (next_loc - 1)) {
if (loc == 1) {
return returnValue;
}
else if (loc < ( std::pow( 2, d-1 ) * 3)) {
returnValue = returnValue->leftChild;
}
else {
returnValue = returnValue->rightChild;
}
// Update location and depth to reflect traversal
loc = std::pow(2, d - 1) + loc % power;
d = d - 1;
}
std::cout << returnValue->data<<"\n";
return returnValue;
}
bool TreeHeap::Delete() {
if (next_loc = 1) {
return false;
}
int d = floor(log2(next_loc - 1));
THeapNode *current = root;
THeapNode *usedNode = NULL;
int loc = next_loc - 1;
while ( 1 ) {
int d = floor(log2(loc));
int power = std::pow(2, d - 1);
if (loc == 1) {
usedNode = current;
break;
}
else if (loc < (std::pow(2, d - 1) * 3)) {
current = current->leftChild;
}
else {
current = current->rightChild;
}
// Update location and depth to reflect traversal
loc = std::pow(2, d - 1) + loc % power;
d = d - 1;
}
THeapNode *temp = root;
clear_heap(root);
root = usedNode;
delete temp;
bubble_down(root);
return true;
}
Thanks in advance for any help you've got.

Quick inspection reveals errors in your bubble_up and bubble_down functions.
In bubble_up, you have the following:
THeapNode *parent = node->parent;
while ( parent->data < node->data ) {
parent = node->parent;
string temp = parent->data;
parent->data = node->data;
node->data = temp;
}
This is going to fail when a node bubbles all the way to the top, because it doesn't check for the node being at the root. You need to write:
while (parent != NULL && parent->data < node->data)
In bubble_down, you have this code:
while( node->data < node->rightChild->data || node->data < node->leftChild->data ){
if (node->rightChild->data > node->leftChild->data) {
You don't check to see if the rightChild or leftChild are NULL.
More importantly, that looks like an infinite loop to me because you never change the value of node.
Those are just the errors I saw on a quick read through your code. There might be more.

Related

Inserting a Binary Tree in a vector

What I would need is pushing the binary tree in a vector and write out the vector`s contents in the terminal to prove that is works.
I cant seem to make it work , I tried in a few different ways like making funcions that will do that for me. Im a Computer Science student so Im kinda beginnerish , I tried my hardest but I just dont know how to do it.
You gave me a lot of good advice through your posts and answers but, I didn`t find anything in this regard so Im asking for your help.
Here is my binary tree :
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
using namespace std;
class Binfa {
public:
int value;
Binfa *left;
Binfa *right;
int depth() {
int leftd = 0, rightd = 0;
if (left == NULL && right == NULL)
return 1;
if (left != NULL)
leftd = left->depth();
if (right != NULL)
rightd = right->depth();
return (max(leftd, rightd) + 1);
}
static double averagedepth(Binfa *node) {
static int db = 0, sum = 0, depth = 0;
if (node != NULL) {
++depth;
Binfa::averagedepth(node->right);
Binfa::averagedepth(node->left);
--depth;
if (node->left == NULL && node->right == NULL) {
++db;
sum += depth;
}
} else {
return 0.0;
}
return (double) sum / db;
}
static double spread(Binfa *node, double average) {
static int db = 0, depth = 0;
static double sum = 0.0;
if (node != NULL) {
++depth;
spread(node->right, average);
spread(node->left, average);
--depth;
if (node->right == NULL && node->left == NULL) {
++db;
sum += ((depth - average) * (depth - average));
}
}
if (db > 1)
return sqrt(sum / (db - 1));
return sqrt(sum);
}
static void print(Binfa *node) {
static int idepth = 0;
if (node != NULL) {
++idepth;
print(node->right);
for (int i = 0; i < idepth; ++i)
printf("---");
printf("%c(%d)\n",
node->value < 2 ? '0' + node->value : node->value,
idepth - 1);
print(node->left);
--idepth;
}
}
};
int main(int argc, char **argv) {
char b;
int in = 0;
int i, egy_e;
Binfa *root = new Binfa();
root->value = '/';
root->left = root->right = NULL;
Binfa *current = root;
if (argv[1] != NULL) {
in = open(argv[1], O_RDONLY);
}
while (read(in, (void *) &b, 1)) {
for (i = 0; i < 8; ++i) {
egy_e = b & 0x80;
if ((egy_e >> 7) == 1) {
if (current->right == NULL) {
current->right = new Binfa();
current->right->value = 1;
current->right->left = current->right->right = NULL;
current = root;
} else {
current = current->right;
}
} else {
if (current->left == NULL) {
current->left = new Binfa();
current->left->value = 0;
current->left->left = current->left->right = NULL;
current = root;
} else {
current = current->left;
}
}
b <<= 1;
}
}
printf("\n");
Binfa::print(root);
printf("melyseg=%d\n", root->depth() - 1);
printf("altag=%f\n", Binfa::averagedepth(root));
printf("szoras=%f\n", Binfa::spread(root, Binfa::averagedepth(root)));
return 0;
}

C++ circular linked list deletion, counts start at the next node

I have no idea how to delete in a circular link list. For example the head was B, so the list will go from "B, C, D, E, A". The first node will always pick a number from 1-5 which I keep reducing using the counter, so for example if "B" picked 3, the count will start unto it's next node which is "C" so counting from "C", we will have to eliminate "E", once "E" was eliminated.
The new head aka the picker will start unto the next node after the eliminated node, so the next set of nodes will become "A,B,C,D", this function must repeat until there is only 1 last standing node.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <string>
#include <ctime>
using namespace std;
/*
* Node Declaration
*/
struct node
{
string name;
struct node *next;
};
node *t, *head;
node *ex;
int paper;
int ctr = 5;
int num;
void create(string sname)
{
node *n = new node;
n->name = sname;
if (head == NULL)
{
head = n;
t = n;
}
else
{
t->next = n; // connects the nodes
t = t->next; // moves the connecter to the t= last
}
t->next = head;
}
/*
* Deletion of element from the list
*/
void delete_element(string value)
{
}
//Display Circular Link List
void display()
{
node *temp = new node;
temp = head;
if ((head == NULL) && (t == NULL))
{
}
for (int j = 1; j <= 5; j++)
{
cout << temp->name << "\n";
temp = temp->next;
}
}
void firstpic()
{
srand(time(NULL));
paper = rand() % 5 + 1;
int fctr = 5;
bool p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0;
if (paper == 1)
{
create("A");//1
fctr--;
}
else if (paper == 2)
{
create("B");//2
p2 = 1;
}
else if (paper == 3)
{
create("C");//2
p3 = 1;
}
else if (paper == 4)
{
create("D");//2
p4 = 1;
}
else if (paper == 5)
{
create("E");//2
p5 = 1;
}
if (p1)
{
create("B");
create("C");
create("D");
create("E");
}
else if (p2)
{
create("C");
create("D");
create("E");
create("A");
}
else if (p3)
{
create("D");
create("E");
create("A");
create("B");
}
else if (p4)
{
create("E");
create("A");
create("B");
create("D");
}
else if (p5)
{
create("A");
create("B");
create("C");
create("D");
}
}
void drawn()
{
node *holder = head;
ex = holder->next;
cout << holder->name << " has drawn: " <<num <<endl;
}
int main()
{
head == NULL;
t == NULL;
srand(time(NULL));
firstpic();
display();
num = rand() % ctr + 1;
drawn();
system("pause>nul");
return 0;
}
Here is something that may suit your needs:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
/*
* Node Declaration
*/
struct node
{
string name;
struct node *next;
};
node *tail, *head;
void addNode(string sname)
{
node *n = new node;
n->name = sname;
if (head == NULL) {
head = n;
tail = n;
} else {
tail->next = n; // connects the nodes
tail = tail->next; // moves the connecter to the t= last
}
tail->next = head;
}
/*
* Deletion of element from the list
*/
void removeNode(string value)
{
// no elements
if (head == NULL) {
return;
}
node *n = head;
node *prev = tail;
// 1 element
if(n == prev) {
if(n->name == value) {
delete n;
head = tail = NULL;
}
return;
}
bool found = false;
// search
do {
if(n->name == value) {
found = true;
break;
}
prev = n;
n = n->next;
} while (n != head);
// no such element
if(!found) {
return;
}
prev->next = n->next;
if(n == head) {
head = n->next;
} else if(n == tail) {
tail = prev;
}
delete n;
}
void displayList()
{
if (head == NULL) {
cout << "empty!" << endl;
return;
}
node *n = head;
do {
cout << n->name << "\n";
n = n->next;
} while (n != head);
cout << endl;
}
void createList()
{
const int count = 5;
std::string names[count] = {"A", "B", "C", "D", "E"};
int nameIndex = rand() % count;
for(int i = 0; i<count; ++i) {
nameIndex += 1;
nameIndex %= count;
addNode(names[nameIndex]);
}
}
int main()
{
srand(time(NULL));
head = NULL;
tail = NULL;
createList();
displayList();
removeNode("A");
displayList();
removeNode("A");
displayList();
removeNode("E");
displayList();
removeNode("B");
displayList();
removeNode("C");
displayList();
removeNode("D");
displayList();
system("pause>nul");
return 0;
}
Note that during node deletion you should take care of such cases as:
No elements case
Last 1 element to be deleted
Head deletion
Tail deletion
Also, never do things like you did in your firstpic() function: it is a painfull way to do it. drawn() function seems not to do anything that makes sence, but it is out of question's scope.

A count function that counts the leaf nodes of a height balanced tree

I'm writing a function that counts the leaf nodes of a height balanced tree using struct and pointers. The function takes 3 arguments: the tree, pointer to an array and the maximum depth of the tree. The length of the array is the maximum depth. When function is called the array is initialized to zero. The function recursively follows the tree structure,
keeping track of the depth, and increments the right counter whenever it reaches a leaf. The function does not follow any pointer deeper than maxdepth. The function returns 0 if there was no leaf at depth greater than maxdepth, and 1 if there was some pointer togreater depth. What is wrong with my code. Thanks.
typedef int object;
typedef int key;
typedef struct tree_struct { key key;
struct tree_struct *left;
struct tree_struct *right;
int height;
} tree_n;
int count_d (tree_n *tr, int *count, int mdepth)
{
tree_n *tmp;
int i;
if (*(count + 0) == NULL){
for (i =0; i<mdepth; i++){
*(count + i) = 0;
}
}
while (medepth != 0)
{
if (tr == NULL) return;
else if ( tree-> left == NULL || tree->right == NULL){
return (0);
}
else {
tmp = tr;
*(count + 0) = 1;
int c = 1;
while(tmp->left != NULL && tmp->right != NULL){
if(tmp-> left){
*(count + c) = 2*c;
tmp = tmp->left;
return count_d(tmp, count , mdepth);
}
else if(tmp->right){
*(count + c + 1) = 2*c + 1;
tmp = tmp->right;
return count_d(tmp,count, mdepth);
}
c++;
mpth--;
}
}
}
What is wrong with my code
One thing I noticed is that you are missing return in the recursive calls.
return count_d(tmp, count , mdepth);
// ^^^ Missing
There are two such calls. Make sure to add return to both of them.
Disclaimer: Fixing this may not fix all your problems.
Correct Function To Insert,Count All Nodes and Count Leaf Nodes
#pragma once
typedef int itemtype;
#include<iostream>
typedef int itemtype;
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Node
{
public:
Node* left;
Node* right;
itemtype data;
};
class BT
{
private:
int count = 0;
Node* root;
void insert(itemtype d, Node* temp);//Override Function
public:
BT();//Constructor
bool isEmpty();
Node* newNode(itemtype d);
Node* getroot();
void insert(itemtype d);//Function to call in main
int countLeafNodes(Node * temp);
int countAllNodes();//to count all nodes
}
BT::BT()//constructor
{
root = NULL;
}
bool BT::isEmpty()
{
if (root == NULL)
return true;
else
return false;
}
Node* BT::newNode(itemtype d)
{
Node* n = new Node;
n->left = NULL;
n->data = d;
n->right = NULL;
return n;
}
void BT::insert(itemtype d)//Function to call in main
{
if (isEmpty())
{
Node* temp = newNode(d);
root = temp;
}
else
{
Node* temp = root;
insert(d, temp);
}
count++;//to count number of inserted nodes
}
void BT::insert(itemtype d, Node* temp)//Private Function which is overrided
{
if (d <= temp->data)
{
if (temp->left == NULL)
{
Node* n = newNode(d);
temp->left = n;
}
else
{
temp = temp->left;
insert(d, temp);
}
}
else
{
if (temp->right == NULL)
{
temp->right = newNode(d);
}
else
{
temp = temp->right;
insert(d, temp);
}
}
}
int BT::countAllNodes()
{ return count; }
int BT::countLeafNodes(Node* temp)
{
int leaf = 0;
if (temp == NULL)
return leaf;
if (temp->left == NULL && temp->right == NULL)
return ++leaf;
else
{
leaf = countLeafNodes(temp->left) + countLeafNodes(temp->right);
return leaf;
}
}
void main()
{
BT t;
t.insert(7);
t.insert(2);
t.insert(3);
t.insert(15);
t.insert(11);
t.insert(17);
t.insert(18);
cout<<"Total Number Of Nodes:" <<t.countAllNodes() <<endl;
cout << "Leaf Nodes:" << t.countLeafNodes(t.getroot()) << endl;
_getch();
}
Output:
Ouput

Tree structure - incorrect children

I'm trying to build simple structure in C++. It should be similar to AVL tree.
Everything is OK when I build a simple tree with three nodes in main() function.
The problem is when I try use insert() function. The first argument of this function contains information where put the value from the second argument.
Here is the code:
#include <numeric>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct Node {
Node* left;
Node* right;
Node* parent;
int value;
int count_leafs;
int height;
};
Node* root;
void insert2(int p, int value, Node* node, int left)
{
//printf("insert %d %d - (%d, %d) %d \n", p, value, node->left, node->right, left);
if (root == NULL) {
// creating a tree root
Node new_node;
new_node.left = NULL;
new_node.right = NULL;
new_node.parent = NULL;
root = &new_node;
root->value = value;
root->count_leafs = 1;
root->height = 1;
return;
}
if (node->left == NULL && node->right == NULL) {
// joining value to the leaf
Node new_parent;
new_parent.count_leafs = 2;
new_parent.height = 2;
new_parent.value = node->value + value;
new_parent.parent = node->parent;
new_parent.left = NULL;
new_parent.right = NULL;
Node new_leaf;
new_leaf.value = value;
new_leaf.count_leafs = 1;
new_leaf.left = NULL;
new_leaf.right = NULL;
new_leaf.height = 1;
new_leaf.parent = &new_parent;
new_parent.left = &new_leaf;
new_parent.right = node;
if (node->parent != NULL && node->parent->left != NULL && node->parent->left == node) {
printf("a");
node->parent->left = &new_parent;
}
if (node->parent != NULL && node->parent->right != NULL && node->parent->right == node) {
printf("b");
node->parent->right = &new_parent;
}
node->parent = &new_parent;
return;
}
//printf("GOTO: %d %d \n", left + node->left->count_leafs, p);
node->value += value;
node->count_leafs += 1;
if (left + node->left->count_leafs + 1 >= p) {
//printf("W left\n");
insert2(p, value, node->left, left);
} else {
//printf("W right\n");
insert2(p, value, node->right, left + node->left->count_leafs);
}
}
void insert(int p, int value)
{
insert2(p, value, root, 0);
}
int main()
{
Node new_root;
root = NULL;
new_root.value = 10;
new_root.height = 2;
new_root.count_leafs = 2;
new_root.parent = NULL;
root = &new_root;
Node left;
left.value = 6;
left.height = 1;
left.count_leafs = 1;
left.parent = root;
left.left = NULL;
left.right = NULL;
Node right;
right.value = 4;
right.height = 1;
right.count_leafs = 1;
right.parent = root;
right.left = NULL;
right.right = NULL;
root->left = &left;
root->right = &right;
// PLACE A
insert(0, 1);
// PLACE B
return 0;
}
As you see before PLACE A is building a tree with 3 nodes. It look like this in PLACE A:
10
/ \
6 4
Next, in a line between PLACE A and PLACE B, I want to add a new node. After that (in PLACE B) the tree should looks like this:
11
/ \
7 4
/ \
1 6
But I gets something like this:
11
/ \
1972250912 4
/ \
2 2
I can't figure out what is wrong. It should be the problem in insert2() function, but I can't find it.
Do you see it?
Thanks in advance for you help!
The cause of a such behavior is that you use scope variables out of the scope. You must not use the pointer that points to the scope variable out of that scope. Scope variables exists only withing the scope were they were declared. If decide to access a scope variable out of the scope, you will access some piece of stack that would have some other data overwritten that variable that would result in undefined behavior.
I mean, that you must NOT do like this:
if (root == NULL)
{
Node new_node;
root = &new_node;
return;
}
You may use operator new to create a new instance of struct Node in the heap and use it later.
if (root == NULL)
{
root = new Node;
return;
}
But you have to delete this node later. Or you may use smart pointers, see this.
Read this and this for more information.
The code below do just what you expected. However it does not delete created nodes that would result in memory leak, so this code have to be improved, but this is a separate issue.
#include <numeric>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct Node {
Node* left;
Node* right;
Node* parent;
int value;
int count_leafs;
int height;
};
Node* root;
void insert2(int p, int value, Node* node, int left)
{
//printf("insert %d %d - (%d, %d) %d \n", p, value, node->left, node->right, left);
if (root == NULL) {
// creating a tree root
Node* new_node = new Node;
new_node->left = NULL;
new_node->right = NULL;
new_node->parent = NULL;
root = new_node;
root->value = value;
root->count_leafs = 1;
root->height = 1;
return;
}
if (node->left == NULL && node->right == NULL) {
// joining value to the leaf
Node* new_parent = new Node;
new_parent->count_leafs = 2;
new_parent->height = 2;
new_parent->value = node->value + value;
new_parent->parent = node->parent;
new_parent->left = NULL;
new_parent->right = NULL;
Node* new_leaf = new Node;
new_leaf->value = value;
new_leaf->count_leafs = 1;
new_leaf->left = NULL;
new_leaf->right = NULL;
new_leaf->height = 1;
new_leaf->parent = new_parent;
new_parent->left = new_leaf;
new_parent->right = node;
if (node->parent != NULL && node->parent->left != NULL && node->parent->left == node) {
printf("a");
node->parent->left = new_parent;
}
if (node->parent != NULL && node->parent->right != NULL && node->parent->right == node) {
printf("b");
node->parent->right = new_parent;
}
node->parent = new_parent;
return;
}
//printf("GOTO: %d %d \n", left + node->left->count_leafs, p);
node->value += value;
node->count_leafs += 1;
if (left + node->left->count_leafs + 1 >= p) {
//printf("W left\n");
insert2(p, value, node->left, left);
}
else {
//printf("W right\n");
insert2(p, value, node->right, left + node->left->count_leafs);
}
}
void insert(int p, int value)
{
insert2(p, value, root, 0);
}
int main()
{
Node new_root;
root = NULL;
new_root.value = 10;
new_root.height = 2;
new_root.count_leafs = 2;
new_root.parent = NULL;
root = &new_root;
Node left;
left.value = 6;
left.height = 1;
left.count_leafs = 1;
left.parent = root;
left.left = NULL;
left.right = NULL;
Node right;
right.value = 4;
right.height = 1;
right.count_leafs = 1;
right.parent = root;
right.left = NULL;
right.right = NULL;
root->left = &left;
root->right = &right;
// PLACE A
insert(0, 1);
// PLACE B
return 0;
}

C++ linked list doesn stop growing [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
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.
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;
}