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...

  • Using Python Create a simple linked list by defining a struct (in C/C++) or a class...

    Using Python Create a simple linked list by defining a struct (in C/C++) or a class (in Python3). For example, in C/C++: struct singly_linked_list { int data; singly_linked_list *next; }; (Make sure you understand the concept of pointers - or lack thereof - in Python.) Write a function to determine if there is a cycle in the singly linked list. Return 1 if there is a cycle in the list, 0 if no cycle. This is a very common interview...

  • You are given a pointer to a singly linked list. The singly linked list has cycles...

    You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...

  • 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; };

  • Write a c++ routine that returns true if two elements in a singly linked list of...

    Write a c++ routine that returns true if two elements in a singly linked list of integers add up to a given target (similar to the two sum problem 1). Your function should return true and print the individual numbers, or return false. Examples: - If your list is: 1 → 3 → 4 → 14 → 5 and your target is 8, then you will return true and print: 3, 5 (since 3 + 5 = 8). - If...

  • 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

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