Linked Lists and Recursion

Linked Lists and Recursion

Question:
I am using several large data structures as linked lists to process some data. Many of my helper functions use recursion. Because of the size/amount of data that I’m handling, the recursion has become bulky and uses up all of the memory on my system.

How can I rewrite this addnode function as interative rather than recursive?

 struct node {  int data;  node* next;};node* addnode (node* addtothis, int n) {  if (addtothis==NULL) {    node* temp=new node;    temp->data=n;    temp->next=NULL;  }  else {    addtothis->next=addnode(addtothis                           ->next, n);  }  return addtothis;}

This is not the actual code I’m using, but the answer would help me rewrite it.

Answer:
You can do some cool things with recursion but if the algorithm has the potential of going too deep, you can blow the stack. This is in addition to slowing down due to the overhead of the recursive call.

When a recursive function calls itself toward the end, it is said to be “tail recursive.” A tail-recursive routine is normally easy to convert to a non-recursive loop. You simply update any variables that reflect the current state and loop.

I suspect that you would probably rework your code a little more to make it non-recursive. For example, you might require that the addtothis argument is non-NULL, otherwise there is nothing to add to.

Here’s my change to get you started. Please note that I had no easy way to test this so it is untested just as I typed it:

 void addnode (node* addtothis, int n){  node *pTemp = NULL;  while (addtothis != NULL) {    pTemp = addtothis;    addtothis = addtothis->next;  }  node* temp=new node;  temp->data=n;  temp->next=NULL;  if (pTemp != NULL)    pTemp->next = temp;}

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes