Related questions
import java.util.NoSuchElementException;
class LinkedList {
private Node head;
private Node tail;
private class Node {
private int value;
private Node next;
public Node(int value) {
this.value = value;
}
}
private boolean isEmpty() {
return (head == null);
}
private boolean hasNext(Node node) {
return (node.next != null);
}
public void print() {
Node current = head;
System.out.print("[");
while (current != null) {
if (hasNext(current)) {
System.out.print(current.value + "' ");
} else {
System.out.print(current.value);
}
current = current.next;
}
System.out.println("]");}
public void addFirst(int value) {
Node node = new Node(value);
if (isEmpty()) {
head = tail = node;
} else {
node.next = head;
head = node;
}
}
public void addLast(int value) {
Node node = new Node(value);
if (isEmpty()) {
head = tail = node;
} else {
tail.next = node;
tail = node;
}
}
public int indexOf(int value) {
int i = 0;
Node current = head;
while (current != null) {
if (current.value == value) {
return i;
}
i++;
current = current.next;
}
return -1;
}
public boolean contains(int value) {
return (indexOf(value) != -1);
}
public void deleteFirst() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException();
}
if (head == tail) {
head = tail = null;
return;
}
Node formerHeadNext = head.next;
head.next = null;
head = formerHeadNext;
}
public Node previous(Node node) throws NoSuchElementException {
if (isEmpty())
throw new NoSuchElementException();
Node current = head;
while (current.next != node) {
if (!hasNext(current)) {
throw new NoSuchElementException();
}
current = current.next;
}
return current;
}
public void deleteLast() throws NoSuchElementException {
if (isEmpty())
throw new NoSuchElementException();
if (head == tail) {
head = tail = null;
return;
}
Node lastButOne = previous(tail);
lastButOne.next = null;
tail = lastButOne;
}
}
public class LinkedListFromScratch {
public static void main(String[] args) {
LinkedList myList = new LinkedList();
myList.addLast(2);
myList.addLast(4);
myList.addLast(8);
myList.addFirst(-2);
myList.addFirst(-4);
myList.addLast(9);
myList.print();
System.out.println(myList.indexOf(4));
System.out.println(myList.contains(9));
for (int i = 0; i < 6; ++i) {
myList.deleteLast();
myList.print();
}
}
}
In this linked list Add in an O(n) method that reverse to the class LinkedList created in the problem
1. This method must reverse the order of the nodes in the list. For
example, having applied reverse to the list [2 → 4 → 8 → 9 → 8], we
will change it to [8 → 9 → 8 → 4 → 2].
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- Given the code: import java.util.HashMap; import java.util.LinkedList; import java.util.Scanner; public class Controller { privateHashMap<String,LinkedList<Stock>>stockMap; publicController(){ stockMap=newHashMap<>(); Scannerinput=newScanner(System.in); do{ // Prompt for stock name or option to quit System.out.print("Enter stock name or 3 to quit: "); StringstockName=input.next(); if(stockName.equals("3")){ break;// Exit if user inputs '3' } // Get or create a list for the specified stock LinkedList<Stock>stockList=stockMap.computeIfAbsent(stockName,k->newLinkedList<>()); // Prompt to buy or sell System.out.print("Input 1 to buy, 2 to sell: "); intcontrolNum=input.nextInt(); System.out.print("How many stocks: "); intquantity=input.nextInt(); if(controlNum==1){ // Buying stocks System.out.print("At what price: "); doubleprice=input.nextDouble(); buyStock(stockList,stockName,quantity,price); }else{ // Selling stocks System.out.print("Press...arrow_forwardTree Define a class called TreeNode containing three data fields: element, left and right. The element is a generic type. Create constructors, setters and getters as appropriate. Define a class called BinaryTree containing two data fields: root and numberElement. Create constructors, setters and getters as appropriate. Define a method balanceCheck to check if a tree is balanced. A tree being balanced means that the balance factor is -1, 0, or 1.arrow_forwardimport java.util.HashSet; import java.util.Set; // Define a class named LinearSearchSet public class LinearSearchSet { // Define a method named linearSearch that takes in a Set and an integer target // as parameters public static boolean linearSearch(Set<Integer> set, int target) { // Iterate over all elements in the Set for () { // Check if the current value is equal to the target if () { // If so, return true } } // If the target was not found, return false } // Define the main method public static void main(String[] args) { // Create a HashSet of integers and populate integer values Set<Integer> numbers = new HashSet<>(); // Define the target to search for numbers.add(3); numbers.add(6); numbers.add(2); numbers.add(9); numbers.add(11); // Call the linearSearch method with the set...arrow_forward
- import java.util.*;public class MyLinkedListQueue{ class Node { public Object data; public Node next; private Node first; public MyLinkedListQueue() { first = null; } public Object peek() { if(first==null) { throw new NoSuchElementException(); } return first.data; } public void enqueue(Object element) { Node newNode = new newNode(); newNode.data = element; newNode.next = first; first = newNode; } public boolean isEmpty() { return(first==null); } public int size() { int count = 0; Node p = first; while(p ==! null) { count++; p = p.next; } return count; } } } Hello, I am getting an error message when I execute this program. This is what I get: MyLinkedListQueue.java:11: error: invalid method declaration; return type required public MyLinkedListQueue() ^1 error I also need to have a dequeue method in my program. It is very similar to the enqueue method. I'll...arrow_forwardJava - class IntBTNode { private int data; private IntBTNode left; private IntBTNode right; } Write a new static method of the IntBTNode class to meet the following specification. public static boolean all10(IntBTNode root) // Precondition: root is the root reference of a binary tree (but // NOT NECESSARILY a search tree). // Postcondition: The return value is true if every node in the tree // contains 10. NOTE: If the tree is empty, the method returns true.arrow_forward1. Complete the function evaluate_postfix(String exp): Input: "10 23 - 5 15 + +" Retur: 7 Node.java public class Node {Object info;Node next;Node(Object info, Node next){this.info=info;this.next=next;}}// "((1+2)"// ['(','(','1','+','2',')']// stack : ')' -> '2' -> '1' -> '(' -> '(' -> null Stack.java public class Stack {private Node top;public Stack() {top = null;}public boolean isEmpty() {return (top == null);}public void push(Object newItem) {top = new Node(newItem, top);}public Object pop() {if (isEmpty()) {System.out.println("Trying to pop when stack is empty");return null;} else {Node temp = top;top = top.next;return temp.info;}}void popAll() {top = null;}public Object peek() {if (isEmpty()) {System.out.println("Trying to peek when stack is empty");return null;} else {return top.info;}}} Runner.java public class Runner {public static void main(String[] args) {String expression1 = "((1+2)))))+(2+2))(1/2)";boolean res =...arrow_forward
- /** * This class will use Nodes to form a linked list. It implements the LIFO * (Last In First Out) methodology to reverse the input string. * **/ public class LLStack { private Node head; // Constructor with no parameters for outer class public LLStack( ) { // to do } // This is an inner class specifically utilized for LLStack class, // thus no setter or getters are needed private class Node { private Object data; private Node next; // Constructor with no parameters for inner class public Node(){ // to do // to do } // Parametrized constructor for inner class public Node (Object newData, Node nextLink) { // to do: Data part of Node is an Object // to do: Link to next node is a type Node } } // Adds a node as the first node element at the start of the list with the specified...arrow_forwardclass BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)...arrow_forwardwrite this code below as algorithim to determine the leaf node reclusively ? public static void printLeafNodes(TreeNode node) { // base case if (node == null) { return; } if (node.left == null && node.right == null) { System.out.printf("%d ", node.value); } printLeafNodes(node.left); printLeafNodes(node.right); }arrow_forward
- 1 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 find This, Assume that find this is in the list public class List { protected Node head; protected int size; fublic Public Node find (char find This)arrow_forwardclass BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)...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_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education