Question

Summer II 2020 - Data Structures and Algorithms (COSC-2336-01W) In the linked-list based bag implementation, we demonstrated
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

There are two bags A and B - Following operation are performed.

1. Minimum element in the list

2. Maximum element in the list

3. Average of the list

Implementation of C++ program -

// Program to find smallest and largest 
// elements in the singly linked list.

// header files 
#include <bits/stdc++.h> 
using namespace std; 

// Linked list node
struct node { 
        int data; 
        struct node* next; 
}; 

// This Function returns the largest element from the linked list. 
int largestElement(struct node* head) 
{ 
        // Declaration of variable
        int max = INT_MIN; 

        // Loop for finding the maximum value in the linked list
        while (head != NULL) { 

                if (max < head->data) 
                        max = head->data; 
                head = head->next; 
        } 
        return max; 
} 

// This Function returns smallest element in the linked list. 
int smallestElement(struct node* head) 
{ 
        // Declaration of variable 
        int min = INT_MAX; 

        // Loop for finding the minimum value in the linked list
        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; 
} 

float avgOfNodes(struct node* head) 
{ 
    // if head = NULL 
    if (!head) 
        return -1; 
  
    int count = 0; // Initialize count 
    int sum = 0; 
    float avg = 0.0; 
  
    struct node* current = head; // Initialize current 
    while (current != NULL) { 
        count++; 
        sum += current->data; 
        current = current->next; 
    } 
  
    // calculate average 
    avg = (double)sum / count; 
  
    return avg; 
} 

// This Function inserts the element in the 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 to the newNode. 
        newNode->data = data; 

        // 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 functions 
int main() 
{ 
        // Start with empty list 
        struct node* A = NULL; 
    struct node* B = NULL;

        push(&A, 25); 
        push(&A, 34); 
        push(&A, 13); 
        push(&A, 42); 
        push(&A, 97); 

    push(&B, 25); 
        push(&B, 34); 
        push(&B, 43); 
        push(&B, 12); 
        push(&B, 37); 
        cout << "Linked list  A is : " << endl; 

        // Print the list 
        printList(A); 
        

        cout << "Maximum element in linked list:"; 
    cout << largestElement(A) << endl; 


        cout << "Minimum element in linked list:"; 
    cout << smallestElement(A) << endl; 

    cout << "Average of List A = " << avgOfNodes(A)<<endl;
    
    cout << "Linked list  B is : " << endl;
    // Print the list 
        printList(B); 

        cout << "Maximum element in linked list:"; 
    cout << largestElement(B) << endl; 


        cout << "Minimum element in linked list:"; 
    cout << smallestElement(B) << endl; 

    cout << "Average of List B = " << avgOfNodes(B);



        return 0; 
} 

Output:

input Output wucLUT LUVUL OU TOOTD Linked list Ais : 97 -> 42 -> 13 -> 34 -> 25 -> NULL Maximum element in linked list:97 Min

input Output Linked list B is : 37 -> 12 -> 43 -> 34 -> 25 -> NULL Maximum element in linked list:43 Minimum element in linke

Thanks!

Keep Chegging.

Add a comment
Know the answer?
Add Answer to:
Summer II 2020 - Data Structures and Algorithms (COSC-2336-01W) In the linked-list based bag implementation, we...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • Rework this project to include a class. As explained in class, your project should have its...

    Rework this project to include a class. As explained in class, your project should have its functionalities moved to a class, and then create each course as an object to a class that inherits all the different functionalities of the class. You createclass function should be used as a constructor that takes in the name of the file containing the student list. (This way different objects are created with different class list files.) Here is the code I need you...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • C++ Modify this program (or create your own) so that it performs the following tasks: 1....

    C++ Modify this program (or create your own) so that it performs the following tasks: 1. Insert the numbers into the list in sorted order (ascending). This will require you to keep the list sorted as you continue to insert new numbers. 2. When adding a new number, first check to see whether it is larger than the last number currently in the list. If it is, add it directly to the end of the list, i.e., do not traverse...

  • Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT