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)
Problem Description: Write a method that returns the smallest element in a specified column in a...
Problem Description: Write a method that returns the smallest element in a specified column in a matrix using the following header: public static double columnMin(double[][] m, int columnIndex) Write a program that reads from the keyboard the number of rows and columns, and an array of double floating point numbers with the specified number of rows and columns. The program will then invoke the columnMin method and displays the minimum value of all columns.
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
Write a LISP function COUNTX which takes an atom and a list and returns the number...
Write a LISP function COUNTX which takes an atom and a list and returns the number of top-level occurrences of the atom in the list. For example: (COUNTX ‘A ‘(A (A B) B A B A (B A)) Returns the value 3, the other two A’s are not at the top level
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT