Question

In: Computer Science

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

Solutions

Expert Solution

The required code and corresponding output are as follows. The main method is included to test the function getSmallest()

#include <iostream>
#include<malloc.h>
using namespace std;
struct Node { 
    int data; 
    struct Node* next; 
}; 
  
// 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; 
} 
// Function that push the element in linked list. 
void push(struct Node** head, int data) 
{ 
    // Allocate dynamic memory for newNode. 
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 
  
    // Assign the data into newNode. 
    newNode->data = data; 
  
    // newNode->next assign the address of 
    // head node. 
    newNode->next = (*head); 
  
    // newNode become the headNode. 
    (*head) = newNode; 
} 
  
// Display linked list. 
void printList(struct Node* head) 
{ 
    while (head != NULL) { 
        printf("%d -> ", head->data); 
        head = head->next; 
    } 
    cout << "NULL" << endl; 
} 
  
// Driver program to test the function 
int main() 
{ 
    // Start with empty list 
    struct Node* head = NULL; 
  
    // Using push() function to construct 
    // singly linked list 
    // Insertion at the begining
    push(&head, 5); 
    push(&head, 7); 
    push(&head, 6); 
    push(&head, 4); 
    push(&head, 8); 
    cout << "Linked list is : " << endl; 
  
    // Call printList() function to display 
    // the linked list. 
    printList(head); 

    cout << "Smallest element in linked list:"; 
  
    // Call getSmallest() function to get smallest 
    // element in linked list. 
    cout << getSmallest(head) << endl; 
  
    return 0; 
} 

Output:


Related Solutions

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.
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
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
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 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 named minimumValue, which returns the smallest value in an array that is passed...
Write a method named minimumValue, which returns the smallest value in an array that is passed (in) as an argument. The method must use Recursion to find the smallest value. Demonstrate the method in a full program, which does not have to be too long regarding the entire code (i.e. program). Write (paste) the Java code in this word file (.docx) that is provided. (You can use additional pages if needed, and make sure your name is on every page...
Write a method named minimumValue, which returns the smallest value in an array that is passed...
Write a method named minimumValue, which returns the smallest value in an array that is passed (in) as an argument. The method must use Recursion to find the smallest value. Demonstrate the method in a full program, which does not have to be too long regarding the entire code (i.e. program). Write (paste) the Java code in this word file (.docx) that is provided. (You can use additional pages if needed, and make sure your name is on every page...
Implement a function that returns the maximum number in a given unsorted linked list. For example,...
Implement a function that returns the maximum number in a given unsorted linked list. For example, there is a linked list 3->5->1->10->9. The printMax() function in max.c should return the maximum number in the linked list, namely 10 in the example. 1. Implement max.c with the completed printMax() function. 2. Provide an explanation for your solution #include <stdio.h> typedef struct node { int value; struct node *next; } node; int printMax(node *first) { // Your implementation return 0; } int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT