In: Computer Science
Must be using Java Eclipse Please
4. Test program LinkedList.java, and make sure that you
understand each operation in the
program. (refer to linkedListApplication.java)
6. (Programming) Use LinkedList as a reference, add the
following operations in the class
LinkedList;
a) Find the average data values of the linked list.
b) Find the node with largest key, and then delete the node. (Note,
you must return
the node, not just the largest key)
c) Test ALL operations (Search, Insert, Delete, Append/Remove
to/from the header)
in the Main method. (Also display the average of the data values of
the linked list,
the largest key, the linked list before and after deleting the node
with the largest
key;
7. (Programming) Modify LinkedList.java programs so that it
handles employee objects.
Make your program menu-driven.
a) Find the average salary of the employees in the linked
list.
b) Find the employee with largest ID, and then delete the node.
(Note, you must return
the employee, not just the largest key, and delete it)
c) Test ALL operations (Search, Insert, Delete, Append/Remove
to/from the header) in
the Main method. (Also display the average salary, the employee
with largest ID, the
linked list before and after deleting the employee with the largest
key;
LinkedList.java
import java.util.*;
public class LinkedList
{
public Node header;
public LinkedList()
{
header = null;
}
public final Node Search(int key)
{
Node current = header;
while (current != null &&
current.item != key)
{
current =
current.link;
}
return current;
}
public final void Append(int newItem)
{
Node newNode = new
Node(newItem);
newNode.link = header;
header = newNode;
}
public final Node Remove()
{
Node x = header;
if (header != null)
{
header =
header.link;
}
return x;
}
public final Node searchPrevious(int key)
{
if (header == null)
{
return
header;
}
else
{
Node current =
header;
while
(!(current.link == null) && (current.link.item !=
key))
{
current = current.link;
}
return
current;
}
}
public final void Insert(int newItem, int
preKey)
{
Node current;
Node newNode = new
Node(newItem);
current = Search(preKey);
if (current == null)
{
System.out.println("there is no such preKey!");
}
else
{
newNode.link =
current.link;
current.link =
newNode;
}
}
public final void Delete(int key)
{
if (header == null) // The list is
empty!
{
System.out.println("The list is empty!");
}
else
{
if (header.item
== key) // header to be deleted.
{
header = header.link;
}
else
{
Node p = searchPrevious(key);
if (p.link == null)
{
System.out.println("There is
no such item!");
}
else
{
p.link = p.link.link;
}
}
}
}
public final void ShowLinkedList()
{
if (header == null)
System.out.println("The list is empty!");
else
{
Node current = header;
System.out.printf("%1$s->", current.item);
while (!(current.link == null))
{
current = current.link;
System.out.printf("%1$s->", current.item);
}
System.out.printf("null");
System.out.println();
}
}
public final void PrintList()
{
if (header == null)
{
System.out.println("The list is empty!");
}
else
{
Node current =
header;
System.out.println(current.item);
while
(!(current.link == null))
{
current = current.link;
System.out.println(current.item);
}
}
}
}
//----------- LinkedList.java -------
import java.util.*;
class Node {
public int item;
public Node link;
Node(int num) {
item = num;
link = null;
}
}
public class LinkedList {
public Node header;
public LinkedList() {
header = null;
}
public final Node Search(int key) {
Node current = header;
while (current != null &&
current.item != key) {
current =
current.link;
}
return current;
}
public final void Append(int newItem) {
Node newNode = new
Node(newItem);
newNode.link = header;
header = newNode;
}
public final Node Remove() {
Node x = header;
if (header != null) {
header =
header.link;
}
return x;
}
public final Node searchPrevious(int key) {
if (header == null) {
return
header;
} else {
Node current =
header;
while
(!(current.link == null) && (current.link.item != key))
{
current = current.link;
}
return
current;
}
}
public final void Insert(int newItem, int preKey)
{
Node current;
Node newNode = new
Node(newItem);
current = Search(preKey);
if (current == null) {
System.out.println("there is no such preKey!");
} else {
newNode.link =
current.link;
current.link =
newNode;
}
}
public final void Delete(int key) {
if (header == null) // The list is
empty!
{
System.out.println("The list is empty!");
} else {
if (header.item
== key) // header to be deleted.
{
header = header.link;
} else {
Node p = searchPrevious(key);
if (p.link == null) {
System.out.println("There is
no such item!");
} else {
p.link = p.link.link;
}
}
}
}
public final void ShowLinkedList() {
if (header == null)
System.out.println("The list is empty!");
else {
Node current =
header;
System.out.printf("%1$s->", current.item);
while
(!(current.link == null)) {
current = current.link;
System.out.printf("%1$s->",
current.item);
}
System.out.printf("null");
System.out.println();
}
}
public final void PrintList() {
if (header == null) {
System.out.println("The list is empty!");
} else {
Node current =
header;
System.out.println(current.item);
while
(!(current.link == null)) {
current = current.link;
System.out.println(current.item);
}
}
}
//method getAverage() that returns the average of Node
values
//in linked list.
public double getAverage()
{
//if header is null then average
will be 0.
if(header == null)
{
return 0;
}
//to store average and sum of node
values
double avg = 0.0;
double sum = 0;
//store header in current
node.
Node current = header;
//count is the number of nodes in
linked list.
int count = 0;
//till last node.
while (current != null)
{
//add current
node value to the sum variable
sum +=
current.item;
//move to next
node.
current =
current.link;
//increment the
count of nodes.
count++;
}
//average is the division of sum /
number of nodes.
avg = sum / count;
//return the average.
return avg;
}
//function that returns the largest Node in the
list.
public Node getLargestNode()
{
//if header is null then there is
no Largest Node.
if(header == null)
{
return
null;
}
//to store current node while
processing.
Node current = header;
//set largest node value of item as
the header node value.
Node largest = new
Node(current.item);
//till end of last node.
while (current!=null)
{
//if current
node value is greater than the largest item value.
if(current.item
> largest.item)
{
//then largest item value will be current node
value.
largest.item = current.item;
}
//move to next
node.
current =
current.link;
}
//return the largest node.
return largest;
}
}
//-------- linkedListApplication.java ------
//class that tests all the functionalities of LinkedList
class.
public class linkedListApplication {
public static void main(String[] args)
{
//crate linked list object
LinkedList list = new
LinkedList();
//add values using Append()
method
list.Append(5);
list.Append(1);
list.Append(2);
list.Append(3);
list.Append(4);
//display linked list.
System.out.println("\nLinked List
After insertion using Append()-----");
list.ShowLinkedList();
//insert using Insert(10,3) which
inserts 10 after key 3.
System.out.println("\nInserting 10
at previous key 3 using Insert() method");
list.Insert(10,3);
System.out.println("\nLinked List
After insertion using Insert()-----");
list.ShowLinkedList();
//call getAverage() and store the
result
double avg =
list.getAverage();
System.out.println(String.format("\nAverage of Linked List values
using getAverage() method : %.2f",avg));
//call getLargestNode() and store
the result
Node largest =
list.getLargestNode();
System.out.println("\nLargest value
in Linked list: "+largest.item);
System.out.println("\nLinked List
before deleting largest node: "+largest.item);
list.PrintList();
//remove the largest item value in
linked list.
list.Delete(largest.item);
System.out.println("\nLinked List
After Deleting Largest Node Value using Delete() method:
"+largest.item+" -----");
list.ShowLinkedList();
System.out.println("\nLinked List
before performing Remove() method");
list.ShowLinkedList();
//call Remove() method to remove
head from linked list.
list.Remove();
System.out.println("\nLinked List
After performing Remove() method");
list.ShowLinkedList();
}
}
//---------- OUTPUT -------------
// ----------------------- PART 7 ---------------------------
//----------- LinkedList.java -------
import java.util.*;
class Employee {
public String name;
public int id;
public double salary;
public Employee link;
Employee(String name,int id,double salary) {
this.name =name;
this.id = id;
this.salary = salary;
link = null;
}
@Override
public String toString() {
return "Employee [ name=" + name +
" , id=" + id + " , salary=" + salary + " ]";
}
}
public class LinkedList {
public Employee header;
public LinkedList() {
header = null;
}
public final Employee Search(int key) {
Employee current = header;
while (current != null &&
current.id != key) {
current =
current.link;
}
return current;
}
public final void Append(String name,int id,double
salary) {
Employee newEmployee = new
Employee(name,id,salary);
if(Search(newEmployee.id) ==
null)
{
newEmployee.link
= header;
header =
newEmployee;
}
else
{
System.out.println("\nDuplicate User.User with id: "+id+" is
already Existed");
}
}
public final Employee Remove() {
Employee x = header;
if (header != null) {
header =
header.link;
}
return x;
}
public final Employee searchPrevious(int key)
{
if (header == null) {
return
header;
} else {
Employee current
= header;
while
(!(current.link == null) && (current.link.id != key))
{
current = current.link;
}
return
current;
}
}
public final void Insert(String name,int id,double
salary, int preKey) {
Employee current;
Employee newEmployee = new
Employee(name,id,salary);
current = Search(preKey);
if (current == null) {
System.out.println("there is no such preKey!");
} else {
if(Search(id) ==
null)
{
newEmployee.link = current.link;
current.link = newEmployee;
}
else
{
System.out.println("\nDuplicate User.User with
id: "+id+" is already Existed");
}
}
}
public final void Delete(int key) {
if (header == null) // The list is
empty!
{
System.out.println("The list is empty!");
} else {
if (header.id ==
key) // header to be deleted.
{
header = header.link;
} else {
Employee p = searchPrevious(key);
if (p.link == null) {
System.out.println("There is
no such item!");
} else {
p.link = p.link.link;
}
}
}
}
public final void ShowLinkedList() {
if (header == null)
System.out.println("The list is empty!");
else {
Employee current
= header;
System.out.printf("[ %s,%d ] -> ", current.name,
current.id);
while
(!(current.link == null)) {
current = current.link;
System.out.printf("[ %s,%d ] -> ",
current.name, current.id);
}
System.out.printf("null");
System.out.println();
}
}
public final void PrintList() {
if (header == null) {
System.out.println("The list is empty!");
} else {
Employee current
= header;
System.out.println(current);
while
(!(current.link == null)) {
current = current.link;
System.out.println(current);
}
}
}
//method getAverage() that returns the average of
Employee Salary values
//in linked list.
public double getAverageSalary()
{
//if header is null then average
will be 0.
if(header == null)
{
return 0;
}
//to store average and sum of
employee salary
double avg = 0.0;
double sum = 0;
//store header in current
node.
Employee current = header;
//count is the number of nodes in
linked list.
int count = 0;
//till last node.
while (current != null)
{
//add current
node salary value to the sum variable
sum +=
current.salary;
//move to next
node.
current =
current.link;
//increment the
count of nodes.
count++;
}
//average is the division of sum /
number of nodes.
avg = sum / count;
//return the average.
return avg;
}
//function that returns the largest Employee in the
list.
public Employee getLargestEmployeeId()
{
//if header is null then there is
no Largest Employee.
if(header == null)
{
return
null;
}
//to store current node while
processing.
Employee current = header;
//set largest node value of item as
the header node value.
Employee largest = new
Employee(current.name,current.id,current.salary);
//till end of last node.
while (current!=null)
{
//if current
node value is greater than the largest item value.
if(current.id
> largest.id)
{
//then largest item value will be current node
value.
largest.id = current.id;
largest.name = current.name;
largest.salary = current.salary;
}
//move to next
node.
current =
current.link;
}
//return the largest node.
return largest;
}
}
//-------- linkedListApplication.java ------
//class that tests all the functionalities of LinkedList
class.
public class linkedListApplication {
public static void main(String[] args)
{
//crate linked list object
LinkedList list = new
LinkedList();
//add values using Append()
method
list.Append("John
wick",1,3000);
list.Append("Tony
Stark",2,40000);
list.Append("Barry
Allen",2,20000);
list.Append("Rambo",3,40000);
//display linked list.
System.out.println("\nEmployees
List After insertion using Append()-----");
list.ShowLinkedList();
//insert using Insert() which
inserts Barray Allen with id 4.
System.out.println("\nInserting
Barry Allen with id 4 at previous key 3 using Insert()
method");
list.Insert("Barry
Allen",4,25000,3);
System.out.println("\nEmployees
List After insertion using Insert()-----");
list.ShowLinkedList();
//call getAverage() and store the
result
double avg =
list.getAverageSalary();
System.out.println(String.format("\nAverage Salary of Employees
List values using getAverage() method : %.2f",avg));
//call getLargestNode() and store
the result
Employee largest =
list.getLargestEmployeeId();
System.out.println("\nLargest Id in
Linked list: "+largest.id);
System.out.println("Employee
Details: "+largest);
System.out.println("\nEmployees
List before deleting largest node with id: "+largest.id);
list.ShowLinkedList();
//remove the largest item value in
linked list.
list.Delete(largest.id);
System.out.println("\nEmployees
List After Deleting Largest Node with Id Value using Delete()
method: "+largest.id+" -----");
list.ShowLinkedList();
System.out.println("\nEmployees
List before performing Remove() method");
list.ShowLinkedList();
//call Remove() method to remove
head from linked list.
list.Remove();
System.out.println("\nEmployees
List After performing Remove() method");
list.PrintList();
}
}
//-------------- OUTPUT ----------------