import java.io.*;
import java.util.stream.*;
public class Solution {
static class ListCell<T> {
public T datum; // Data for this cell
public ListCell<T> next; // Next cell
public ListCell(T datum, ListCell<T> next) {
this.datum = datum;
this.next = next;
}
}
static class LinkedList<T> {
private static final String STRING = " ";
Solution.ListCell<T> head; // head (first cell) of the List
public LinkedList() {
head = null;
}
public void insert(T element) {
head = new ListCell<T>(element, head);
}
public void delete(T element) {
delete(element, head);
}
private ListCell<T> delete(T element, ListCell<T> cell) {
if (cell == null)
return null;
if (cell.datum.equals(element))
return cell.next;
cell.next = delete(element, cell.next);
return cell;
}
public int size() {
return size(head);
}
private int size(ListCell<T> cell) {
if (cell == null)
return 0;
return size(cell.next) + 1;
}
public String toString() {
return toString(head);
}
private String toString(ListCell<T> cell) {
if (cell == null)
return "";
return cell.datum.toString() + STRING + toString(cell.next);
}
}
// Complete the mergeSort function below.
// !!! Leave the code as is except for the below function, !!!
// !!! though writing helper function(s) are allowed. !!!
private static void sort(Solution.LinkedList<Integer> llist) {
}
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int llistCount = Integer.parseInt(br.readLine().trim());
LinkedList<Integer> llist = new LinkedList<>();
IntStream.range(0, llistCount).forEach(i -> {
try {
Integer llistItem = Integer.parseInt(br.readLine().trim());
llist.insert(llistItem);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
sort(llist);
bufferedWriter.write(llist.toString().trim());
bufferedWriter.close();
br.close();
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- package hw5; public class LinkedIntSet {private static class Node {private int data;private Node next; public Node(int data, Node next) {this.data = data;this.next = next;}} private Node first; // Always points to the first node of the list.// THE LIST IS ALWAYS IN SORTED ORDER!private int size; // Always equal to the number of elements in the set. /*** Construts an empty set.*/public LinkedIntSet() {throw new RuntimeException("Not implemented");} /*** Returns the number of elements in the set.* * @return the number of elements in the set.*/public int size() {throw new RuntimeException("Not implemented");} /*** Tests if the set contains a number* * @param i the number to check* @return <code>true</code> if the number is in the set and <code>false</code>* otherwise.*/public boolean contains(int i) {throw new RuntimeException("Not implemented");} /*** Adds <code>element</code> to this set if it is not already present and* returns…arrow_forwardComplete TODOs in c++ #include <iostream> #include "QueueLL.hpp" using namespace std; QueueLL::QueueLL() { queueFront = nullptr; queueEnd = nullptr; } QueueLL::~QueueLL() { while( !isEmpty() ) dequeue(); } bool QueueLL::isEmpty() { /*if(queueFront == nullptr || queueEnd == nullptr) return true; return false;*/ return (!queueFront || !queueEnd); } // TODO void QueueLL::enqueue(int key) { Node *nn = newNode; nn->key = key; nn->next = nullptr; // TODO Complete this function, handle the case when you're enqueuing in an empty queue } //TODO void QueueLL::dequeue() { if(!isEmpty()) { // TODO Complete this function, handle the case when your queue becomes empty after dequeuing } else{ cout<<"queue is empty. can not deque"<<endl; } } int QueueLL::peek() { if( !isEmpty() ) returnqueueFront->key; else { cout<<" queue is empty. can not peek"<<endl; return -1; } //return 0; }arrow_forwardAssume you have a class SLNode representing a node in a singly-linked list and a variable called list referencing the first element on a list of integers, as shown below: public class SLNode { private E data; private SLNode next; public SLNode( E e){ data = e; next = null; } public SLNode getNext() { return next; } public void setNext( SLNoden){ next = n; } } SLNode list; Write a fragment of Java code that would append a new node with data value 21 at the end of the list. Assume that you don't know if the list has any elements in it or not (i.e., it may be empty). Do not write a complete method, but just show a necessary fragment of code.arrow_forward
- #include <iostream> usingnamespace std; class Queue { int size; int* queue; public: Queue(){ size = 0; queue = new int[100]; } void add(int data){ queue[size]= data; size++; } void remove(){ if(size ==0){ cout <<"Queue is empty"<<endl; return; } else{ for(int i =0; i < size -1; i++){ queue[i]= queue[i +1]; } size--; } } void print(){ if(size ==0){ cout <<"Queue is empty"<<endl; return; } for(int i =0; i < size; i++){ cout<<queue[i]<<" <- "; } cout << endl; } //your code goes here }; int main(){ Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5); Queue q3 = q1+q2; q3.print();…arrow_forwardplease fix code to match "enter patients name in lbs" thank you import java.util.LinkedList;import java.util.Queue;import java.util.Scanner; interface Patient { public String displayBMICategory(double bmi); public String displayInsuranceCategory(double bmi);} public class BMI implements Patient { public static void main(String[] args) { /* * local variable for saving the patient information */ double weight = 0; String birthDate = "", name = ""; int height = 0; Scanner scan = new Scanner(System.in); String cont = ""; Queue<String> patients = new LinkedList<>(); // do while loop for keep running program till user not entered q do { System.out.print("Press Y for continue (Press q for exit!) "); cont = scan.nextLine(); if (cont.equalsIgnoreCase("q")) { System.out.println("Thank you for using BMI calculator"); break;…arrow_forwardGiven the tollowiıng class template detinition: template class linkedListType { public: const linked ListType& operator=(const linked ListType&); I/Overload the assignment operator. void initializeList(); /Initialize the list to an empty state. //Postcondition: first = nullptr, last = nullptr, count = 0; linked ListType(); Ildefault constructor //Initializes the list to an empty state. /Postcondition: first = nullptr, last = nullptr, count = 0; -linkedListīype(); Ildestructor /Deletes all the nodes from the list. //Postcondition: The list object is destroyed. protected: int count; //variable to store the number of elements in the list nodeType *first; //pointer to the first node of the list nodeType *last; //pointer to the last node of the list 1) Write initializeList() 2) Write linkedListType() 3) Write operator=0 4) Add the following function along with its definition: void rotate(); I/ Remove the first node of a linked list and put it at the end of the linked listarrow_forward
- In Java. The following is a class definition of a linked list Node:class Node{int info;Node next;}Show the instructions required to create a linked list that is referenced by head and stores in order, the int values 13, 6 and 2. Assume that Node's constructor receives no parameters.arrow_forwardRedesign LaptopList class from previous project public class LaptopList { private class LaptopNode //inner class { public String brand; public double price; public LaptopNode next; public LaptopNode(String brand, double price) { // add your code } public String toString() { // add your code } } private LaptopNode head; // head of the linked list public LaptopList(String fname) throws IOException { File file = new File(fname); Scanner scan = new Scanner(file); head = null; while(scan.hasNextLine()) { // scan data // create LaptopNode // call addToHead and addToTail alternatively } } private void addToHead(LaptopNode node) { // add your code } private void addToTail(LaptopNode node) { // add your code } private…arrow_forwardclass Queue { private static int front, rear, capacity; private static int queue[]; Queue(int c) { front = rear = 0; capacity = c; queue = new int[capacity]; } static void queueEnqueue(int data) { if (capacity == rear) { System.out.printf("\nQueue is full\n"); return; } else { queue[rear] = data; rear++; } return; } static void queueDequeue() { if (front == rear) { System.out.printf("\nQueue is empty\n"); return; } else { for (int i = 0; i < rear - 1; i++) { queue[i] = queue[i + 1]; } if (rear < capacity) queue[rear] = 0; rear--; } return; } static void queueDisplay() { int i; if (front == rear) { System.out.printf("\nQueue is Empty\n"); return; } for (i = front; i < rear; i++) { System.out.printf(" %d <-- ", queue[i]); } return; } static void queueFront() { if (front == rear) { System.out.printf("\nQueue is Empty\n"); return; } System.out.printf("\nFront Element is: %d", queue[front]);…arrow_forward
- Please help with this Java program, and include explanations and commentsarrow_forward1.Assume some Node class with info link fields . Complete this method in class List that returns a reference to the node containing the data item in the argument findThis, Assume that findThis is in the list public class list { protected Node head ; Protected int size ; Public Node find (char find this) { Node curr = head; while(curr != null) { if(curr.info == findThis) return curr; curr = curr.link; } return -1; } }arrow_forwardBelow you're given a Node class and a LinkedList class. You will implement a method for the LinkedList class named "delete48in148". That is, whenever there is a sequence of nodes with values 1, 4, 8, we delete the 4 and 8. For exCample, Before: 1 -> 4 -> 8 LAfter: 1 Before: 7 -> 1 -> 4 -> 8 -> 9 -> 4 -> 8 After: 7 -> 1 -> 9 -> 4 -> 8 Before: 7 -> 1 -> 4 -> 8 -> 4 -> 8 -> 4 -> 8 -> 9 After: 7 -> 1 -> 9 Note from the above example that, after deleting one instance of 48, there may be new instances of 148 formed. You must delete ALL of them. Requirement: Your implementation must be ITERATIVE (i.e., using a loop). You must NOT use recursion. Recursive solutions will NOT be given marks. import ... # No other import is allowedarrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY