Concept explainers
Direction: Continue the attached code below. It can only insert a value to a linkedlist. Your goal is to add new functionality to the linkedlist. You may only choose 3 more functions:
1. getValue - this function should be able to display a node by specifying its position (index).
2. clear - resets the linkedlist by assigning null to head.
3. insertNewHead this function can be used to assign a new head to the linkedlist
4. insertAt - this function can be used to insert a node to a specific location.
5. displayAll this function will display all nodes
import java.util.Scanner;
class MainLL
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
SinglyLL sll= new SinglyLL();
String msg = "Enter an action: [1] Insert, [2]Get an element, [3] clear, [0] Exit";
System.out.println(msg);
int choice = sc.nextInt();
while(choice != 0) {
switch(choice) {
case 1: System.out.println("Enter a value:");
sll.insert(sc.next());
System.out.println("Successfully added a node!\n" + msg);
break;
default: System.out.println("Invalid input! Please try again. \n"+msg);
break;
}
choice sc.nextInt();
}
}
Step by stepSolved in 4 steps with 2 images
- Our hash map data is saved as a LinkedList due to improper map building. This makes hash maps less helpful.arrow_forwardBased on the previous questions, create a Queue class that uses the LinkedList for its data storage. Create the __init__, isEmpty, insert, remove, and size methods. Assume that LinkedList class has the add, removeFront and size methods defined.arrow_forwardlinked_list_stack_shopping_list_manager.py This is a file that includes the class of linked list based shopping list manager. This class contains such methods as init, insert_item, is_list_empty, print_item_recursive_from_top, print_items_from_top, print_item_recursive_from_bottom, print_items_from_bottom, getLastItem, removeLastItem. In addition, this class requires the inner class to hold onto data as a linked list based on Stack. DO NOT use standard python library, such as deque from the collections module. Please keep in mind the following notes for each method during implementation: Init(): initializes linked list based on Stack object to be used throughout object life. insert_item(item): inserts item at the front of the linked list based on Stack. Parameters: item name. is_list_empty (): checks if the current Stack object is empty (ex. check if head is None). print_item_recursive_from_top(currentNode): a helper method to print linked list based on Stack item recursively. Note:…arrow_forward
- A map has the form Map <k,v> where: K: specifies the type of keys maintained in this map.V: defines the type of mapped values.Furthermore, the Map interface provides a set of methods that must be implemented. In this section, we will discuss about the most famous methods: clear: Removes all the elements from the map.containsKey: Returns true if the map contains the requested key.containsValue: Returns true if the map contains the requested value.equals: Compares an Object with the map for equality.get: Retrieve the value of the requested key.entrySet: Returns a Set view of the mappings contained in this map.keySet: Returns a Set that contains all keys of the map.put: Adds the requested key-value pair in the map.remove: Removes the requested key and its value from the map, if the key exists.size: Returns the number of key-value pairs currently in the map. Here is an example of TreeMap with a Map: import java.util.Map; import java.util.TreeMap; public class TreeMapExample {…arrow_forwardIt is python language Write the code that creates a new Node class. It will store data and next attributes. You only need to create the __init__ method. data and next variables will have default values, both set to None. Assume you are using the Node class from the previous connection to create a LinkedList. You have the code below, create a method that removes the first node from the LinkedList. class LinkedList: def __init__(self): self.head = None Based on the code from the last two questions, create a new LinkedList. Add 2 values to the LinkedList (there is an add method that accepts data as an argument, called add). Then call the removeFront method created in the previous question. Based on the previous questions, create a Queue class that uses the LinkedList for its data storage. Create the __init__, isEmpty, insert, remove, and size methods. Assume that LinkedList class has the add, removeFront and size methods defined. Based on the LinkedList code already…arrow_forwardYou may have found it somewhat tedious and unpleasant to use the debugger and visualizer to verify the correctness of your addFirst and addLast methods. There is also the problem that such manual verification becomes stale as soon as you change your code. Imagine that you made some minor but uncertain change to addLast]. To verify that you didn't break anything you'd have to go back and do that whole process again. Yuck. What we really want are some automated tests. But unfortunately there's no easy way to verify correctness of addFirst and addLast] if those are the only two methods we've implemented. That is, there's currently no way to iterate over our list and get bad its values and see that they are correct. That's where the toList method comes in. When called, this method returns a List representation of the Deque. For example, if the Deque has had addLast (5) addLast (9) addLast (10), then addFirst (3) called on it, then the result of toList() should be a List with 3 at the…arrow_forward
- def upgrade_stations(threshold: int, num_bikes: int, stations: List["Station"]) -> int: """Modify each station in stations that has a capacity that is less than threshold by adding num_bikes to the capacity and bikes available counts. Modify each station at most once. Return the total number of bikes that were added to the bike share network. Precondition: num_bikes >= 0arrow_forwardIf you want to create a linkedlist, how many nodes should it have to be effective? How long does the longest one take?arrow_forwardWrite a program that records and displays league standings for a baseball league. The program will ask the user to enter five team names, and five win amounts. It will store the data in memory, and print it back out sorted by wins from highest to lowest. The sample output from your program should look something like this (user input in bold orange): Enter team #1: PadresEnter the wins for team #1: 75Enter team #2: DodgersEnter the wins for team #2: 91Enter team #3: GiantsEnter the wins for team #3: 92Enter team #4: Rockies Enter the wins for team #4: 65Enter team #5: DiamondbacksEnter the wins for team #5: 70League Standings:Giants: 92Dodgers: 91Padres: 75Diamondbacks: 70Rockies: 65 Requirements The data must be stored in two parallel arrays: an array of strings named teams, and an array of ints named wins. These arrays must be declared in the main() function. You can assume that the league has five teams, so each of the arrays should have five elements. As usual, you may not use any…arrow_forward
- Based on the code from the last two questions, create a new LinkedList. Add 2 values to the LinkedList (there is an add method that accepts data as an argument, called add). Then call the removeFront method created in the previous question.arrow_forwardnumbers = (38, 42, 15, 22, 46, 61, 88, 89, 64, 48) Partition(numbers, 5, 9) is called. Assume quicksort always chooses the element at the midpoint as the pivot. 1). What is the pivot? 2). What is the low partition? 3). What is the high partition? 4). What are the numbers after Partition(numbers, 5, 9) completes?arrow_forwardUnfortunately instructions do not allow for a method that removes duplicates after linked list is created. How would you sort the file through the addNode Method while the list is being built?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education