Question

In: Computer Science

Question 1: Write a method getSmallest(), which returns the smallest number in the linked list. Question...

Question 1:

Write a method getSmallest(), which returns the smallest number in the linked list.

Question 2:

Write a member method getPosition(int entry) which returns the position of the entry is in the linked list. If the entry is not in the list, return -1.

Please use C++ language for both questions, I only need functions.

Solutions

Expert Solution

Question 1:

Function getSmallest() that returns smallest element can be defined as follows:

// Function that returns smallest element 
// from the linked list. 
int getSmallest(struct Node* head) 
{ 
    // Declare a min variable and initialize 
    // it with head->data 
    int min = head->data; 
  
    // Check loop while head not equal to NULL 
    while (head != NULL) { 
  
        // If min is greater then head->data then 
        // assign value of head->data to min 
        // otherwise node point to next node. 
        if (min > head->data) 
            min = head->data; 
  
        head = head->next; 
    } 
    return min; 
} 

Question 2:

Function getPosition(int entry) that returns position of a particular element can be defined as follows:

    //Checks whether the value entry is present in linked list 
    int getPosition(struct Node* head, int entry) 
    { 
        int pos=1;    //Initialize node count 
        while (head != NULL) 
        { 
            if (head->data == entry) 
                return pos;    //data found 
             head = head->next; 
             pos++; 
        } 
        return -1;    //data not found 
    } 

Related Solutions

In C++, type a method getSmallest(), which returns the smallest number in the following linked list....
In C++, type a method getSmallest(), which returns the smallest number in the following linked list. 8->4->6->7->5 (8 is the head).
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list....
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list. If the list is empty, then it should return 0. use the provided code below public class Question03 { public class ListNode//public for testing purposes { public int data;//public for testing purposes public ListNode link;//public for testing purposes public ListNode(int aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//public for testing purposes public int getMaxValue() { //----------------------------------------------------------------------------------- //Write...
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array
Using python. Produce a method for a linked list that is called FIND , which returns...
Using python. Produce a method for a linked list that is called FIND , which returns the index of a lookup value within the linked list
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest...
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest value in the array. If the array is empty, return the “end” pointer ·and takes as parameters: (1)   a pointer to double. The pointer is the address of the start of an array, (2)   the “end” pointer to the address after the array (3)   and the address of the smallest value seen so far ·Write main() to test this function – try a case where the array...
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT