Question

1. Given the already build class and struct for singly linked list, write a recursive function...

1. Given the already build class and struct for singly linked list, write a recursive function that finds the minimum value of Singly Linked List (please make use of a helper function)

2. Given the already built class and struct for singly linked list, write a recursive function that finds the sum of a singly linked list (please make sure of a helper function)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

// Node of Linked List
struct Node
{
    int data;
    Node *next;
};
// Linked List Class
class LL
{
    private:
        Node *head,*tail;
    public:
        LL() // constructor
        {
            head = NULL; // initially head and tail will be null
            tail = NULL;
        }

        void insertNode(int elem) // add a Node in the list
        {
            Node *newNode = new Node; // create a new node with given element and next will be null
            newNode->data = elem;
            newNode->next = NULL;

            if(head == NULL) // if head is null then make new node as head
            {
                head = newNode;
                tail = newNode;
            }
            else // else insert new node at tail and make tail to point to its next
            {
                tail->next = newNode;
                tail = tail->next;
            }
        }

        int minimumHelper(Node* head, int min)
        {
            if(head == NULL) // if head is null return min
                return min;
            if(min > head->data) // if min is greater than head data then update the min
                min = head->data;
            minimumHelper(head->next,min); // recursive call to minimum helper with head next and min
        }
        int minimum()
        {
            Node *temp = head; // we will pass temp as we don't want modification in head
            return minimumHelper(temp,INT_MAX); // call minimumHelper with temp and Maximum Integer value possible
        }

        int sumHelper(Node* head, int sum)
        {
            if(head == NULL)// if head is null then return the sum
                return sum;

            sum += head->data; // update the sum
            sumHelper(head->next,sum); // recursive call to sumHelper with head next and sum
        }
        int sum()
        {
            Node *temp = head;
            return sumHelper(temp,0); // call sumHelper with temp and 0 as current sum is 0
        }
};
int main()
{
    LL list;
    list.insertNode(1);
    list.insertNode(2);
    list.insertNode(-3);
    list.insertNode(4);
    list.insertNode(5);

    cout<<"Our Linked List is : 1 -> 2 -> -3 -> 4 -> 5"<<endl;
    int min = list.minimum();
    cout<<"Minimum in List is : "<<min<<endl;
    int total_sum = list.sum();
    cout<<"Sum of elements in List is : "<<total_sum<<endl;

    return 0;
}


Output:-

Add a comment
Know the answer?
Add Answer to:
1. Given the already build class and struct for singly linked list, write a recursive function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • C programming Write an iterative and recursive version of a function to print out a linked...

    C programming Write an iterative and recursive version of a function to print out a linked list from head to tail and then do the same for printing a linked list from tail to head. Assume a singly linked list in all cases and access only to a head pointer at the time of the function call. struct node; typedef struct node Node; struct node int data; Node next;

  • [C++] Create three functions for a singly linked list: - Function 1: Insert a string into...

    [C++] Create three functions for a singly linked list: - Function 1: Insert a string into the linked list - Function 1: Insert a node after a given node (Node* curNodeptr, Node* newNodePtr) - Function 2: Delete the node passed to it by a pointer, it will take in the head and curPtr '(Node*, Node*)' struct Node{ string data; Node *next; };

  • Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Nod...

    Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Node; Ret (3,3.8) (4,2.5) (20pts)( Recursive function) Show the code for a function that receives a pointer to the head of an ordered singly linked list that uses the structure in the top left. The function will return the pointer node that shows the highest...

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • Build a singly linked list with 10 noes. The data are "This is my first project...

    Build a singly linked list with 10 noes. The data are "This is my first project in Data structure and algorithm". First node is "This" second "is" and so on. Build insert function, insert "the " before "Data" and delete function, delete "my" before "first". Search "in" and output it location and then append "C++" and output the linked list again. In writing this program use menu which includes ”1. Create; 2. Insert; 3. Delete; 4. Append; 5. Search; 6....

  • Simple singly-linked list question: write an iterative function position(p, target) that returns position (1, 2, 3,...

    Simple singly-linked list question: write an iterative function position(p, target) that returns position (1, 2, 3, or .......) of the target in the list to which p points. If target is not on the list the function returns -1. please write the function in C++ thanks

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas....

    I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas. public class Node ! int id; Node next: public Node (int id) ( this.id id: Write program codes for the traditional: 1) append int id), prepend(int id), removeFirstNodeO. displayAlINodesO, and findById(int id) operations for a singly linked list 2) pushint id), pop), peek0, displayAllNodes0 operations for a stack 3) enQueue(int id), deQueuel), displayAINodes() operations for a gueue Please make sure that you declare separate...

  • In C++ Assume entries in a linked list are of type struct listrec: struct listrec {...

    In C++ Assume entries in a linked list are of type struct listrec: struct listrec {             struct listrec    *prev; float                 value;             struct listrec    *next;   }; listrec *head, *tail; Write a main() routine in which the user is asked the number of nodes to create in the list (number greater than or equal to zero) then create the following type of linked list (use a loop to initialize list) based on the number of nodes requested: Write a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT