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 :
--------------------------------------------------------End-------------------------------------------------
P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; 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 ) { 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 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, 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 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 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 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 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 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 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;...