Question: Write a Java program to implement a singly linked list of integer values (MyLinkedList) with the following operations: 1. void add(int x) - add a

Write a Java program to implement a singly linked list of integer values (MyLinkedList) with the following operations:

1. void add(int x) - add a node with value x at the tail of a list.

2. void traverse() - traverse from head to tail and display info of all nodes in the list.

3. void delete(int x) - delete the first node whose info is equal to x.

4. Node search(int x) - search and return the reference to the first node having info x.

5. int count() - count and return number of nodes in the list.

6. void sort() - sort the list by ascending order of info.

7. int max() - find and return the maximum value in the list.

8. int sum() - return the sum of all values in the list.

9. int avg() - return the average of all values in the list. T

he main() functions should be:

MyLinkedList a = new MyLinkedList();

a.add(5);

a.add(7);

a.add(2);

a.add(8);

a.add(3);

a.traverse(); // 5, 7, 2, 8, 3

a.search(1); // Not found

a.search(8); // Found at the 3-th position.

a.max(); // 8;

a.sum(); // sum: 25

a. avg(); // avg: 5

a.count(); // count: 5

a.sort(); // 2, 3, 5, 7, 8

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!