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.
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...
C++ Write a program to create a linked list which stores the details of employees(Employee number,...
C++ Write a program to create a linked list which stores the details of employees(Employee number, employee name, rate, hours worked). Create a menu to manage the emoployee data. MENU 1. ADD EMPLOYEE DETAILS 2. DELETE EMPLOYEE 3. SEARCH EMPLOYEE 4. PRINT EMPLOYEE PAYROLL 5. EXIT When the user selected option #4 the program should print the following pay report for each employee: EmpNo.     Name      Rate    Hours    Regular Pay      Overtime Pay     Gross Pay Any hours worked above 40 hours are...
(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...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT