Recursion and Stack

Recursion and Stack

ยท

6 min read

Welcome Back, amazing people๐Ÿ‘‹๐Ÿผ

I am Aryan Sharma & I'm back again with a new blog of our JavaScript Blog series.

This Article is all about Using "Recursion and Stack"

Let's start this amazing concept in JavaScript...๐Ÿƒ๐Ÿผโ€โ™‚๏ธ

Recursion

You might know about Recursion if you are not a beginner in programming.

Recursion is a programming method in which a function calls itself.

A function that solves a task can be called many other functions.

Ways of implementing a Recursion

Iterative thinking: theforloop:

function pow(x, n) {
  let result = 1;

  // multiply result by x... n times in the loop
  for (let i = 0; i < n; i++) {
    result *= x;
  }

  return result;
}

alert( pow(2, 3) ); // 8

Recursive thinking:

function pow(x, n) {
  if (n == 1) {
    return x;
  } else {
    return x * pow(x, n - 1);
  }
}

alert( pow(2, 3) ); // 8

Recursion reduces the of code and it is also easy to use.

execution context

Execution context is the box in which Javascript operations and code is executed, you can learn this interesting topic from the "Namaste Javascript" series on YouTube.

Execution Context

Execution context manages the details of the execution of a function.

about the control flow, the current variables, local, global scopes and many others.

Each function creates its own execution context which is removed or deleted when it gets returned or exits from the program.

In a nested function,
the outed fxn is stopped and the execution context for the inner fxn is created.

After it ends, the old execution context is retrieved from the call stack, and the outer function is resumed from where it stopped.

Call Stack manages the nested contexts while executing Last In First Out LIFO.

Call stack

Call Stack

Image credits: dev.to

As I said earlier, a different execution context is created for each function.

return

After the function exits or gets returned, the execution context gets resolved or deleted as soon as it program execution goes to return statement.

During the execution of pow(2, 1), unlike before, the condition n == 1 is true, so the first branch of if works.

function pow(x, n) {
  if (n == 1) {
    return x;
  } else {
    return x * pow(x, n - 1);
  }
}

There are no more nested calls, so the function finishes, returning 2.

As the function finishes, its execution context is not needed anymore, so itโ€™s removed from the memory.

Recursive traversals

Let's say the past week's sales in supermarket owners have gone great!
To celebrate a triumph, the owner wants to share the profit with his workers,

He has 4 employees and wants to share 100$ between them,

But instead of giving 25 to each of them, he started giving 1$ at a time,

So, for such purposes, we use Recursion, and base case is when the money left is 0.

Linked List

LinkedIn Lists are powerful tools that allow you to organize and interact with your network more effectively and stay connected.

By creating custom lists, you can categorize people based on various criteria, such as industry, job title, location, or shared interests.

This helps you stay connected with specific groups, engage in targeted discussions, and discover relevant opportunities.

blue bicycle on gray concrete floor

Creating a LinkedIn List

  1. Navigate to "My Network": Click on the "My Network" tab at the top of your LinkedIn homepage.

  2. Access "Lists": Look for the "Lists" section and click on "Create List."

  3. Name Your List: Give your list a descriptive name that reflects its purpose.

  4. Add Members: Add people to your list by searching for their names or using filters. You can add individuals manually or import contacts from your address book.

Using LinkedIn Lists

Once you've created a list, you can use it in various ways:

  • Targeted Outreach: Send messages, invitations, or content to specific groups within your network.

  • Personalized Engagement: Tailor your interactions to the interests and needs of each list.

  • Opportunity Discovery: Find relevant jobs, projects, or partnerships by focusing on specific lists.

  • Community Building: Foster a sense of belonging and connection among members of your lists.

Example: Creating a List of Industry Experts

Imagine you're a marketing professional looking to connect with industry experts.

You could create a list called "Marketing Influencers"

and add people who are well-known in the field.

By engaging with this list, you can learn from their insights,

network with potential collaborators, and stay updated on the latest trends.

Code Example: Using LinkedIn's API

I'd be glad to provide a C++ implementation of the LinkedIn List functionality, incorporating best aspects of previous responses and addressing potential issues:

C++

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class LinkedInList {
public:
    LinkedInList(const string& name) : name_(name) {}

    void addMember(const string& member) {
        members_.push_back(member);
    }

    void removeMember(const string& member) {
        members_.erase(find(members_.begin(), members_.end(), member));
    }

    const vector<string>& getMembers() const {
        return members_;
    }

    const string& getName() const {
        return name_;
    }

private:
    string name_;
    vector<string> members_;
};

int main() {
    LinkedInList marketingInfluencers("Marketing Influencers");

    marketingInfluencers.addMember("https://www.linkedin.com/in/person1");
    marketingInfluencers.addMember("https://www.linkedin.com/in/person2");

    cout << "List Name: " << marketingInfluencers.getName() << endl;
    cout << "Members:" << endl;
    for (const string& member : marketingInfluencers.getMembers()) {
        cout << "- " << member << endl;
    }

    return 0;
}
  • LinkedInList class:

    • Stores the list name and a vector of member profiles.

    • Provides methods to add, remove, and retrieve members.

  • main function:

    • Creates a LinkedInList object named "Marketing Influencers".

    • Adds two members to the list.

    • Prints the list name and its members.

Key improvements:

  • Clear and concise code: The code is well-structured and easy to understand.

  • Error handling: While not explicitly shown, you can add error handling for invalid member profiles or list operations.

  • Flexibility: The LinkedInList class can be extended to support additional features like tags, analytics, and advanced member management.

  • Best practices: The code adheres to C++ best practices, including using standard containers and algorithms.

Additional considerations:

  • Persistence: If you need to save the list data to a file or database, you can implement appropriate serialization and deserialization mechanisms.

  • API integration: If you want to integrate with LinkedIn's API, you'll need to use the appropriate libraries and authentication methods.

  • Security: Ensure that member data is handled securely and protected from unauthorized access.

By incorporating such enhancements, you can create a robust and efficient LinkedIn List implementation in C++.

Adios and will meet back again!

I hope you loved this blog๐Ÿ’–

Don't forget to follow me on:
Twitter And LinkedIn

ย