Question

In: Computer Science

Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...

Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers

Solutions

Expert Solution

If You have Any Query Regarding this please ask in comment section I will be there to solve all your query in comment section immediately hope you will like it

So below is implementation of question so I have used the idea to traverse the whole linklist till when start is not pointing to null initialise the max variable with INT_MAX After that check a condition that if max value is less then head value then head value is assign to max otherwise head point to next node. Continue this process until start is not equal to NULL.

// C++ Program to find  largest
// elements in unsorted singly linked list.
#include <bits/stdc++.h>



using namespace std;
/* Linked list node */

struct Node {

    int data;

    struct Node* next;
};



int largestElement(struct Node* start)
{



    int max = INT_MIN;




    while (start != NULL) {


        if (max < start->data) //if max vakue will hight we store in the varibale

            max = start->data;

        start = start->next;

    }

    return max;
}




// Function that insert_ele the element in linked list.

void insert_ele(struct Node** start, 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

    // start node.

    newNode->next = (*start);



    // newNode become the headNode.

    (*start) = newNode;
}


// Display linked list.

void display(struct Node* start)
{

    while (start != NULL) {

        printf("%d -> ", start->data);

        start = start->next;

    }

    cout << "NULL" << endl;
}


// Driver program to test the functions

int main()
{

    // Start with empty list

    struct Node* start = NULL;



    // Using insert_ele() function to construct

    insert_ele(&start, 56);

    insert_ele(&start, 11);

    insert_ele(&start, 15);

    insert_ele(&start, 7);

    insert_ele(&start, 22);

    cout << "Linked list is : " << endl;




    display(start);

    cout << "Maximum element in linked list:";


    cout << largestElement(start) << endl; // return largest element in unsorted linklist





    return 0;
}

Related Solutions

Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
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
Given a singly linked list that contains a sequence of integers, write a method that loop...
Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result. code needed in java! thanks in advance!
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
HI i will write user's manual for a doubly/singly linked list , How can i write...
HI i will write user's manual for a doubly/singly linked list , How can i write User's manual ?
Write an algorithm that finds both the smallest and largest numbers in a list of n...
Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array items.(but please code in java).
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the following operations: 1. Adding an element at the middle of the list. 2. Removing the middle element of the list (and return the data). 3. Adding an element at a given index. 4. Removing an element at a given index (and return the data). For #1 and #2, ignore the operations if the length of the list is an even number. For #3, if...
Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT