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...
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
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]
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...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
1. Considering singly linked list, be able to determine if inserting nodes at the end of...
1. Considering singly linked list, be able to determine if inserting nodes at the end of a LinkedList is less time-efficient than inserting them at the front of the list. 2. Be able to differentiate between the data structures Stack, Queue, and Linked List. 3. Determine between the acronyms LIFO and FIFO, and be able to give one real life example where each is applicable
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT