Question

(Programming) Use p2-1 linkedListClass as a reference, add the following operations in the class LinkedList Find the average data values of the linked list. Find the item with largest key, and then delete the node. Test ALL operations 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 6. 7. (Programming) Modify p2-1 SingleLinedList programs so that it handles employee objects. Make your program menu-driven. The class employee is the same as in Assignment 2: laport Java.util.scaner pubiic class esployee public int Ld public String nane public double salary public vold Input O Systen.out.printin( Enter nane:) nane- new Scanner(Systos.in) nextLine Systes.out.printin(Enter 0 d-Integer parsetat (new sanner (Systes.In) nextin) Systee.out.printinf Enter Salary alary Double.parseboublenow Scanner(Syston.1n).nextl)) public vold Output Systen.out.printane: iSs, 10: 25, Grade: assnane, 10, salary) blic String tostring
media%2Ffad%2Ffad39cba-4939-458a-b616-58


P1 is below

package p6_linkedList;

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) // The only node in the list is to be deleted.
{
header = header.link;
}
else // The list has more than one node.
{
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);
}
}
}
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation:

Below is the C# code for above problem with proper description provided within comments itself. Below code some sample output screenshots are attached.

C# code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SingleLinkedListAllinOne
{
class Node
{
public int element;
public Node link;

public Node()
{
element = -1;
}

public Node(int n)
{
element = n;
}

}

class LinkedList
{
public Node header;
public LinkedList()
{
header = null;
}

// -------------------------------------------------------------
public bool isEmpty() // true if no nodes
{
return header == null;
}
// -------------------------------------------------------------

public Node Search(int KEY)
{
Node current = header;
while (current != null && current.element != KEY)
current = current.link;
return current;
}

public void ShowLinkedList()
{
if (header == null)
Console.WriteLine("The list is empty!");
else
{
Node current = header;
Console.Write("{0}->", current.element);
while (!(current.link == null))
{
current = current.link;
Console.Write("{0}->", current.element);

}
Console.Write("null");
Console.WriteLine();
}
}

public Node searchPrevious(int KEY)
{
if (header == null)
return header;
else
{
Node current = header;
if (current.element == KEY)
return null;
while (!(current.link == null) && (current.link.element != KEY))
current = current.link;
return current;
}
}

public void Insert(int newItem, int preKey)
{
Node current;
Node newNode = new Node(newItem);
current = Search(preKey);
newNode.link = current.link;
current.link = newNode;
}

public void Delete(int KEY)
{
if (header.element == KEY)
header = header.link;
else
{
Node p = searchPrevious(KEY);
if (p.link == null)
Console.WriteLine("there is no such item.");
if (!(p.link == null))
p.link = p.link.link;
}
}

public void Append(int newItem)
{
Node newNode = new Node(newItem);
newNode.link = header;
header = newNode;
}

public void Remove()
{
if (header != null)
header = header.link;
}

// function to find the average of all data values
public float averageDataValues() {
// check whether list is empty or not
if (header == null) {
Console.WriteLine("The list is empty!");
return 0;
}
else
{
Node current = header;
// intial count and total is 0
int total = 0, count = 0;
// loop for find total or all data values and total number of data values
while (current != null) {
// add current element in total
total = total + current.element;
// increament the count
count++;
// proceed to next link
current = current.link;
}

// return average
return total/count;
}
}

// function to find avergae of all data values and delete the aprticular item
public void findLargestAndDelete() {
// check whether list is empty or not
if (header == null)
Console.WriteLine("The list is empty!");
else
{
Node current = header;
// initial largest value
int largest = header.element;
// loop to find largest element
while (current != null) {
// check largest b/w current element and largest one
if(largest < current.element) {
// set to largest
largest = current.element;
}
// next link
current = current.link;
}
// call for delete the largest element
Delete(largest);
}
}


}


class SingleLinkedList
{
static int ShowMenu()
{
Console.WriteLine("\n\tLinked List Testing Program\n");
// add two sub menu also
Console.WriteLine("\nEnter the operation number: \n(1)Output\n(2)Insert\n(3)Search\n(4)Remove\n(5)Deletion\n(6)Append\n(7)Avergae Data value\n(8)Find largest key element & Delete it\n(9)Exit\n\n");
return int.Parse(Console.ReadLine());
}
static void Main(string[] args)
{
LinkedList LL = new LinkedList();

int sel = 0;
// increase 2 sub menu so now 7 to 9
while (sel != 9)
{
sel = ShowMenu();
switch (sel)
{
case 1: //Output
Console.WriteLine("The current data in the list: ");
LL.ShowLinkedList();
break;
case 2: //Insert
if (LL.isEmpty())
{
Console.WriteLine("Empty list...Append first!");
break;
}
Console.WriteLine("Before insert: ");
LL.ShowLinkedList();
Console.Write("Enter a value to insert: ");
int x = int.Parse(Console.ReadLine());
Console.Write("Enter where to insert: ");
int pos = int.Parse(Console.ReadLine());
LL.Insert(x, pos);
Console.WriteLine("After insert: ");
LL.ShowLinkedList();
break;
case 3: //Search
Console.Write("Enter target to search for: ");
int target = int.Parse(Console.ReadLine());
Node found = LL.Search(target);
if (found != null)
Console.WriteLine("{0} is found.", found.element);
else
Console.WriteLine("{0} is not found.", target);
break;
case 4: //Remove
Console.WriteLine("Before remove: ");
LL.ShowLinkedList();
LL.Remove();
Console.WriteLine("After remove: ");
LL.ShowLinkedList();
break;
case 5:
Console.WriteLine("Before delete: ");
LL.ShowLinkedList();
Console.WriteLine("Enter the number to delete: ");
int d = int.Parse(Console.ReadLine());
LL.Delete(d);
Console.WriteLine("After delete: ");
LL.ShowLinkedList();
break;
case 6:
Console.Write("Enter value to append: ");
int x_appd = int.Parse(Console.ReadLine());
LL.Append(x_appd);
LL.ShowLinkedList();
break;
// case for find averageDataValues
case 7:
// call function averageDataValues()
float value = LL.averageDataValues();
Console.WriteLine("Avergae of data values is : {0}" , value);
break;
// case to delete largest node
case 8:
// call function findLargestAndDelete
LL.findLargestAndDelete();
Console.WriteLine("Largest key element deleted.");
break;
case 9:
Console.WriteLine("End of the testing. Goodbye!\a\a");
break;

default: Console.WriteLi
ne("Invalid input!");
break;
}
}

///////////////////////////////////////////////////////////////

}
}
}


Sample Output :

Enter the operation number: (1)Output (2)Insert (3)Search (4)Remove (5)Deletion (6)Append (7)Avergae Data value (8)Find largest key element & Delete it (9)Exit 1 The current data in the list: 39->24-12->null Linked List Testing Program Enter the operation number: (1)Output (2)Insert (3)Search (4)Remove (5)Deletion (6)Append (7)Avergae Data value (8)Find largest key element & Delete it (9)Exit 7 Avergae of data values is : 22--------------------------------------------------------End-------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList()...
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
  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

    When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> {    Node<T> itsFirstNode;    Node<T> itsLastNode;    private int size;    public LinkedList() {        itsFirstNode = null;        itsLastNode = null;          size = 0;    }    public Iterator<T> getIterator() {        return new Iterator(this);    }    // THIS WILL NEED...

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

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