Question

In: Computer Science

get the minimum element from linked list c++

get the minimum element from linked list c++

Solutions

Expert Solution

#include <iostream> 

using namespace std; 
//linked list node 
struct Node { 
        int data; 
        struct Node* next; 
}; 

// function that returns smallest element from linked list

int minimumElement(struct Node* head) 
{ 
        //declare min as largest value
        int min = 9999999; 

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

//main function
int main() 
{ 
        //initially empty list 
        struct Node* head = NULL; 

        //using push() function to construct singly linked list 10->22->18->14->15 
        push(&head, 15); 
        push(&head, 14); 
        push(&head, 18); 
        push(&head, 22); 
        push(&head, 10); 
        
        cout << "Minimum element in linked list: "; 

        //call smallestElement() function to get minimum element from linked list 

        cout << minimumElement(head) << endl; 

        return 0; 
} 

Please refer below screenshot of code for better understanding of code indentation.

Above image is shown main function with output of code.


Related Solutions

Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
(Java) Create a new linked list from two given arrays with the greater element from each...
(Java) Create a new linked list from two given arrays with the greater element from each corresponding array element placed into the linked list. Given two arrays of varying size initialized with integers of varying values, the task is to create a new linked list using those arrays. The condition is that the greater element value from each corresponding array element will be added to the new linked list in the list position that maintains the integers in ascending order....
Given two sorted linked lists, merge them into a third sorted linked list. If an element...
Given two sorted linked lists, merge them into a third sorted linked list. If an element is present in both the lists, it should occur only once in the third list. Code needed in java.
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
In C++, write a member method delete() that deletes a node from a linked list at...
In C++, write a member method delete() that deletes a node from a linked list at a random position. (It should first randomly generate that position. and then delete that node).
C++ 1. The function removeAt of the class arrayListType removes an element from the list by...
C++ 1. The function removeAt of the class arrayListType removes an element from the list by shifting the elements ofthe list. However, if the element to be removed is at the beginning ofthe list and the list is fairly large, it could take a lot ofcomputer time. Because the list elements are in no particular order, you could simply remove the element by swapping the last element ofthe list with the item to be removed and reducing the length of...
Write a program of linked list in which in which element can be inserted only at...
Write a program of linked list in which in which element can be inserted only at the end of the linked list and deletion can also take place at the end. Code needed in java.
Find the largest and smallest element of a linked list, print total of all elements and...
Find the largest and smallest element of a linked list, print total of all elements and find out the average. Code needed in java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT