Concept explainers
Explanation of Solution
Complete executable code:
Filename: “Node.java”
Refer “Node.java” from chapter 19 in the textbook
Filename: “Example1LinkedList.java”
//Define "Example1LinkedList" class
public class Example1LinkedList
{
//Define main function
public static void main(String[] args)
{
/* Create a linked list with two string values */
Node colorList = new Node("Yellow", new Node("Purple"));
/* Add a string value to the beginning of the list */
colorList = new Node("Brown", colorList);
/* Display a first string value of "colorList" linked list */
System.out.print(colorList.value);
/* Display a next string value of "colorList" linked list */
System.out.print(colorList.next.value);
/* Display a another next string value of "colorList" linked list */
System.out.print(colorList.next.next.value);
System.out...
Want to see the full answer?
Check out a sample textbook solutionChapter 19 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Java. Refer to screenshot. There is starter code for this question. public class LinkedList {Node head;Node tail; protected class Node{String data;Node next;} public String removeFirst(){//Complete the implementation of removeFirst} public String toString(){ String result = ""; Node n = head;while(n != null){result += n.data + " -> ";n = n.next;} return result;} }arrow_forwardAdd comments to the code and display outputInt main(void)//Input array containing edges of the graph//(X,Y,W)tuple represents an edge from 'x' to y' having weight w'struct Edge edges I= {0,1,4.).(1,2,4).(2,0,4),(2,1,4)int n=size of (edges)/size of (edges|0]);struct Graph *graph=createGraph(edges,n);return 0; static int minsum=100000;static voidgetMinPathSum(Dictionary<int,ArrayList>graph,bool[visited,ArrayLinecessary,int source,int dest,int currsum)if(src==dest) bool flag=true;for each(int i in necessary)if(!visitedi])flag=false;break: if (flag )minsum=Math. Min(minsum,currsum);return;elselvisited[src]=true;for(auto node:graph[src]) if (Ivisited[node.first])visited [node.first])=true;getMinPathSum(graph, visited,necessary,node.first,dest,currsum+node.second);visited [node.first]=false;arrow_forwardJava. Refer to screenshot. There is starter code. I need help with the method. public class LinkedList {Node head;Node tail; protected class Node{String data;Node next;} public void addLast(String s){//Complete the method here} public String toString(){ String result = ""; Node n = head;while(n != null){result += n.data + " -> ";n = n.next;} return result;} } //DO NOT MODIFY THIS CLASS//All work must be done in the LinkedList.java classimport java.util.Scanner; public class P1 {public static void main(String[] args) {Scanner kb = new Scanner(System.in);int numNames = kb.nextInt(); LinkedList list = new LinkedList();for (int i = 0; i < numNames ; i++) {list.addLast(kb.next());System.out.println(list);}}}arrow_forward
- IN JAVA, WRITE A CUT METHOD ABOUT THE INSTRUCTIONS BY USING FIRST, NEXT, NODE ext. DO IT UNDER THE CODE BELOW, public class GenericCollection<T> { private Node first; private Node last; private int count; private classNode { T data; Node next; } public GenericCollection<T> cut(int k) { //WRITE HERE PLEASE }arrow_forward// interface method ==================================================public Node get(int val) {/*See BST.java for method specification *//* Hint: Make sure you return a Node, not an int *//* Your code here */return null; // Dummy return statement. Remove when you implement!} FILL OUT METHOD AND ALL DESCRIPTIONS AND INFORMATION IS IN PICTURES PROVIDED! IN JAVA LANGUAGE PLS AND THANK YOU!!!!!!arrow_forwardA Memory Matching Game in java code with a 4x4 grid of Squares that when you click on a square it shows a number. It would have to be 2 of each number 1-8 and if the two squares that are clicked match then the squares stay on the grid. If they do not match after picking 2 squares they flip back overarrow_forward
- Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones Title: You Don't Own Me Length: 151 Artist: Lesley Gore class SongNode { // Fields for song information and reference to the next node private String title; private int length; private String artist; private SongNode next; // Constructors for creating SongNode instances public SongNode() { title = ""; length = 0; artist = ""; next = null; } public SongNode(String title, int length, String artist) { this.title = title; this.length = length; this.artist =…arrow_forwardDesign and implement a getLastHalf() method for the MyLinkedList class. The method should return the last half of the list as a new list leaving the original list intact. If the list has an uneven number of elements the extra one element must be in the list returned. Example : [1,3,5,4] return [5,4] and [1,6,9] return [6,9]. Follow the three step method for designing the method Develop the java code for the method Create a test program to test the method thoroughly using the Integer wrapper class.arrow_forwardDesign and implement a getLastHalf() method for the MyLinkedList class. The method should return the last half of the list as a new list leaving the original list intact. If the list has an uneven number of elements the extra one element must be in the list returned. Example : [1,3,5,4] return [5,4] and [1,6,9] return [6,9]. Follow the three step method for designing the method Develop the java code for the method Create a test program to test the method thoroughly using the Integer wrapper class. public class MyLinkedList<E> { private Node<E> head, tail; public MyLinkedList() { head = null; tail = null; } /** Return the head element in the list */ public E getFirst() { if (head == null) { return null; } else { return head.element; } } /** Return the last element in the list */ public E getLast() { if (head==null) { return null; } else { return tail.element; } } /** Add an element to the beginning of the list */ public void prepend(E e) { Node<E> newNode = new…arrow_forward
- Assume that the following method is within the KWLinkedList class, what does this method do? public boolean undefined(E item) { if(head == null) return false; Node<E> p = head.next; Node<E> newNode= new Node<E>(item, p); newNode.prev = head; head.next = newNode; p.prev = newNode; size++; return true; } nserts a new node with its data as item at the front of the list. Inserts a new node with its data as item after the first node of the list. Deletes the node with its data as item. Inserts a new node with its data as item, at the end of the list.arrow_forwardA linked list is built in this lab. Make sure to keep track of the head node. ContactNode.java - Class definition ContactList.java - Contains main() method (2) Build the ContactNode class per the following specifications: Private fields String contactName String contactPhoneNumber ContactNode nextNodePtr Constructor with parameters for name followed by phone number Public member methods getName() - Accessor getPhoneNumber() - Accessor insertAfter() getNext() - Accessor printContactNode() Ex: If the name is Roxanne Hughes and the phone number is 443-555-2864, printContactNode() outputs: Name: Roxanne Hughes Phone number: 443-555-2864 (3) Define main() to read the name and phone number for three contacts and output each contact. Create three ContactNodes and use the nodes to build a linked list. Ex: If the input is: Roxanne Hughes 443-555-2864 Juan Alberto Jr. 410-555-9385 Rachel Phillips 310-555-6610 the output is: Person 1: Roxanne Hughes, 443-555-2864 Person 2: Juan…arrow_forwardin java please Create a method that print values of a given interval in a chain of strings beginning with the first node. Chain should pass not alter outside the method. method header: public static void printAtInterval(Node<String> firstNode, int interval)arrow_forward
- COMPREHENSIVE MICROSOFT OFFICE 365 EXCEComputer ScienceISBN:9780357392676Author:FREUND, StevenPublisher:CENGAGE L