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

You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
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!
Answer the following questions(5pt) a)   Write an algorithm that finds the largest item in an array...
Answer the following questions(5pt) a)   Write an algorithm that finds the largest item in an array of n items by using divide-and-conquer algorithm design pattern. b)   Analyze the worst-case time complexity of your algorithm in (a), and show the results using asymptotic notation (ϴ)
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]
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
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 ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT