Thread: How to Add N number if nodes to a Linked List using iteration? (chap 16, exercise 3)

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    6

    How to Add N number if nodes to a Linked List using iteration? (chap 16, exercise 3)

    Hi,

    I have a little program where you can add N number of nodes to a linked list, with values from 1 to N, and then it prints these values from 1 to N.

    Then it asks you how much of the list you want to remove. You type in a number M, the program removes M nodes from the list, then it prints the all the numbers in the linked-list again.

    Here's it:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    struct LinkedL
    {
        int x;
        LinkedL* next;
    };
    
    
    // Adding using recursion
    
    LinkedL* Add(LinkedL* head, int n, int o)
    {
        LinkedL* node = new LinkedL;
        node->x = o-n+1;
        node->next = head;
        if(n > 1)
        {
            return Add(node, n-1, o);
        }
        else
        {
            return node;
        }
    }
    
    
    //the little help-function to add using iteration.
    
    LinkedL* Add3(LinkedL* head, int n, int o)
    {
        LinkedL* node = new LinkedL;
        node->x = o-n+1;
        node->next = head;
        return node;
    }
    
    
    // Adding to list using iteration
    
    LinkedL* Add2(LinkedL* head, int n, int o)
    {
        LinkedL* temp = NULL;
        do
        {
            head = Add3(head, n, o);
            n--;
        } while (n >= 1);
        return head;
    }
    
    
    // Printing the LinkedList using recursion
    
    void print(LinkedL* head)
    {
        LinkedL* cur = head;
        if(cur == NULL)
        {
            cout << "All nodes deleted.";
        }
        else
        {
            if(cur->next != NULL)
            {
                print(cur->next);
            }
            cout << cur->x << "\n";
        }
    }
    
    
    // Removing from list using recursion
    
    LinkedL* removefromlist (LinkedL* head, LinkedL*cur, int m, int o)
    {
        if (m > o)
        {
            m = o;
        }
        head = cur;
        cur = head->next;
        delete head;
        if (m > 1)
        {
            removefromlist(head, cur, m-1, o);
        }
        else
        {
            return cur;
        }
    }
    
    
    // Removing from list using iteration
    
    LinkedL* removefromlist2 (LinkedL* head, LinkedL*cur, int m, int o)
    {
        if (m > o)
        {
            m = o;
        }
        while (m >= 1)
        {
            head = cur;
            cur = head->next;
            delete head;
            m--;
        }
        return cur;
    
    
    }
    
    
    int main ()
    {
        int n;
        cout << "Type in lenght of the list: ";
        cin >> n;
        LinkedL* head = NULL;
        int o = n;
        head = Add(head, n, o);
        print(head);
    
    
        int m;
        cout << "How much of the list from the last item you want to remove?";
        cin >> m;
        LinkedL* cur = head;
        head = removefromlist (head, cur, m, o);
        print(head);
        cin.ignore();
        cin.get();
    }
    The exercise 3 from chapter 16 is this: Write a recursive algorithm to remove elements from a linked list. Write a recursive algorithm to add elements into a linked list. Try writing the same algorithms using iteration. Do the recursive implementations or the iterative implementations feel more natural?

    I can write algorithm for removing N elements from the linked list using both recursion and iteration. And I can write algorithm for adding N elements to a linked list using recursion and iteration too.

    But adding N elements to a linked list, when writing the algorithm using iteration, I use two functions, where one is called inside the other. Add3 is used in Add2.

    I wonder how can I write a function to add N nodes to a linked list using iteration, without calling to any other function?

    Here's one of my attempts to write algorithm for adding N elements to a linked list using iteration;

    Code:
    LinkedL* Add2(LinkedL* head, int n, int o)
    {
        LinkedL* temp = NULL;
        do
        {
            LinkedL* node = new LinkedL;
            node->x = o-n+1;
            node->next = head;
            head = node;
            n--;
        } while (n > 1);
    
    
        return temp;
    
    
    }
    When I run it, it only adds one node with node->x = 4.


    I wonder how can I write a function to add N nodes to a linked list using iteration, without calling to any other function?

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528

    First off, Could you give your function arguments names related to what they represent not just 'o', 'm' and the like.

    Write a recursive algorithm to remove elements from a linked list. Write a recursive algorithm to add elements into a linked list.
    Try writing the same algorithms using iteration. Do the recursive implementations or the iterative implementations feel more natural?
    Personally, I think iterative solutions are more natural. Recursion is more like magic to me :-P

    Code:
    LinkedL* Add2(LinkedL* head, int n, int o)
    Code:
    {
       LinkedL* temp = NULL;
       do
       {
           LinkedL* node = new LinkedL;
           node->x = o-n+1;
           node->next = head;
           head = node;
           n--;
       } while (n > 1);
    
       return temp;
    
    }

    I think this is okay except what you're returning. You probably meant to return the most recently created node, that is, 'head'.

    Also, You don't need 'temp' here.

    Code:
    LinkedL* Add2(LinkedL* head, int n, int o)
    {
       LinkedL* temp = NULL;
       do
       {
           head = Add3(head, n, o);
           n--;
       } while (n >= 1);
       return head;
    }
    Other than that, I'd argue that you're creating the first node without the user's consent!

    Last edited by Aslaville; 04-19-2016 at 07:04 AM. Reason: arrgh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-19-2015, 07:23 AM
  2. Linked list and nodes
    By Lina_inverse in forum C Programming
    Replies: 2
    Last Post: 10-23-2012, 01:59 PM
  3. adding new nodes to linked list
    By roaan in forum C Programming
    Replies: 8
    Last Post: 07-15-2009, 12:55 PM
  4. Swap two nodes in linked list
    By shounakboss in forum C Programming
    Replies: 3
    Last Post: 09-12-2007, 10:18 AM
  5. Linked List and Nodes
    By paperbox005 in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2004, 09:12 AM

Tags for this Thread