Question

In Java please, These are instance methods which modify the linked list ,* accessed through the instance variable: first * Note that this list keeps track of the number of elements in the instance varible N * It is important that N accurately reflect the length of the list.

* You may not add any fields to the node or list classes.

* You may not add any methods to the node class.

static class Node ; } public Node (double item, Node next) { this. i tem item ; this. next next public double item; public Node next int N; Node first; 1/ reference to the first node in the list //number of nodes in list static boolean showMeSuccess = false; // set to true to also see success notifications for tests // set to false to only see Failure notifications for tests

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package com.HomeworkLib.test;

import java.util.LinkedList;

public class LinkedListTest

{

static class Node{

private double item;

private Node next;

public Node(double item,Node next){

this.item=item;

this.next=next;

}

}

public static void main(String args[])

{

// Creating object of class linked list

LinkedList<String> linkedList = new LinkedList<String>();

// Adding elements to the linked list

linkedList.add("A");

linkedList.add("B");

linkedList.addLast("C");

linkedList.addFirst("D");

linkedList.add(2, "E");

linkedList.add("F");

linkedList.add("G");

System.out.println("Linked list : " + linkedList);

// Removing elements from the linked list

linkedList.remove("B");

linkedList.remove(3);

linkedList.removeFirst();

linkedList.removeLast();

System.out.println("Linked list after deletion: " + linkedList);

// Finding elements in the linked list

boolean status = linkedList.contains("E");

if(status)

System.out.println("List contains the element 'E' ");

else

System.out.println("List doesn't contain the element 'E'");

// Number of elements in the linked list

int size = linkedList.size();

System.out.println("Size of linked list = " + size);

// Get and set elements from linked list

Object element = linkedList.get(2);

System.out.println("Element returned by get() : " + element);

linkedList.set(2, "Y");

System.out.println("Linked list after change : " + linkedList);

}

  

  

}

Add a comment
Know the answer?
Add Answer to:
In Java please, These are instance methods which modify the linked list ,* accessed through the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

    // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...

  • JAVA: * You may not add any fields to the node or list classes. * You...

    JAVA: * You may not add any fields to the node or list classes. * You may not add any methods to the node class. * Do not change any 'Utility' methods that I have included. * You may NOT use any arrays or other Java classes ~ Testing purposes ~ public class DSIList {    Node first,last;        // three instance variables    int N;                   // maintain the size of the list    static class Node...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains...

    iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...

  • A(n) _______ can typically modify the values of instance variables. a. mutator method b. accessor method...

    A(n) _______ can typically modify the values of instance variables. a. mutator method b. accessor method c. get method d. set method Choose the correct responses.  static methods can be _______. a. called only from other static methods b. called only when an object has been instantiated c. accessed through a reference to any object of the class if they are public d. called when no objects of the class have been instantiated.

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Java Double Max Function I need help with this top problem. The bottom is the assignment...

    Java Double Max Function I need help with this top problem. The bottom is the assignment // return Double .NEGATIVE-INFINÄ°TY if the linked list is empty public double max return max (first); h private static double max (Node x) f e I TODO 1.3.27 return 0; 1 package algs13; 2 import stdlib.*; 4 public class MyLinked f static class Node public Node() t 1 public double item; public Node next; 10 int N; Node first; 12 13 14 public MyLinked...

  • Java: Return an array of booleans in a directed graph. Please complete the TODO section in...

    Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT