Question

3 b. Add a non-recursive method: public boolean remove( T item ) that takes item and remove all occurrences of that item. If everything went well, remove then return true. If the something went wrong or item wasnt exists return false Example (List<integer> Original 3 Post-lab: Given the same List<T class, add a non-recursive method: that removes the first negative integer on the linked list. public void removeFisrtNegative ()

in java programing language

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

Screenshot

//Creat a Ceneric ะไส88 wit, linked list cperstian« class ode noe Links 1ked List tter rerove nagativei public boolean insert T ite) node x-rew rode Syster owt.printiz+- Syster.otriz.va)s

-----------------------------------

Program

//Create a generic class with linked list operations
public class intLink<T> {
private node head;
class node{
   T val;
   node link;
}
//Constructor
public intLink(T item) {
   head=new node();
   head.val=item;
   head.link=null;
}
//insert values
public boolean insert(T item) {
   node x=new node();
   x.val=item;
   x.link=head;
   head=x;
   return true;
}
//Print linked list
public void print() {
   node z=head;
   while(z!=null) {
       if(z.link!=null) {
           System.out.print(z.val+"->");
       }
       else {
           System.out.print(z.val);
       }
       z=z.link;
   }
   System.out.println();
}
//Remove items
public boolean remove(T item) {
   int flag=0;
   if(item==head.val) {
       head=head.link;
       flag=1;
   }
  
       node x=head;
       node y=head.link;
       while(true) {
           if(y==null) {  
               break;
              
           }
           else if(y.val==item) {
               x.link=y.link;
               x=y;
               y=x.link;
               flag=1;
           }
           else {
               x=y;
               y=x.link;
           }
       }
       if(flag==1) {
           return true;
       }
       else {
           return false;
       }
}
//Remove negative
public void removeFirstNegative() {
  
   if(Integer.parseInt(head.val.toString())<0) {
       head=head.link;
       return;
   }
   else {
       node x=head;
       node y=head.link;
       while(true) {
           if(y==null||Integer.parseInt(y.val.toString())<0) {
               break;
           }
           else {
               x=y;
               y=x.link;
           }
       }
  
       if(y!=null) {
           x.link=y.link;
           return ;
       }
   }
}
//Test method
public static void main(String[] args) {
   //Create linked list
   intLink intlink=new intLink(-8);
   intlink.insert(3);  
   intlink.insert(-9);
   intlink.insert(3);
   intlink.insert(2);
   intlink.insert(3);
   //Print
   System.out.println("Liked List before remove:");
   intlink.print();
   //Remove all 3's and print
   intlink.remove(3);
   System.out.println("Liked List after remove:");
   intlink.print();
   //Remove negative and print
   intlink.removeFirstNegative();
   System.out.println("Liked List after remove negative:");
   intlink.print();
}
}

---------------------------------------

Output

Liked List before remove:
3->2->3->-9->3->-8
Liked List after remove:
2->-9->-8
Liked List after remove negative:
2->-8

Add a comment
Know the answer?
Add Answer to:
in java programing language 3 b. Add a non-recursive method: public boolean remove( T item )...
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
  • Write a recursive method remove(int target, LLNode list) that removes all occurrences of target from list...

    Write a recursive method remove(int target, LLNode list) that removes all occurrences of target from list and returns a reference to the new list. For our example list the statement values = remove(6, values); would result in values referencing the list containing 3 9 12 15 18 19 19 and 20. If target is not contained in list then the list remains unchanged. Please also add a driver class to demonstrate that the method is working correctly. LLNode.java //---------------------------------------------------------------------------- //...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • JAVA language Add a recursive method to the program shown in the previous section that states...

    JAVA language Add a recursive method to the program shown in the previous section that states how many times a particular value appears on the stack. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack()...

  • JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{    private static final...

    JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{    private static final int DEFAULT_CAPACITY = 2;   //default initial capacity / minimum capacity    private T[] data;   //underlying array    // ADD MORE PRIVATE MEMBERS HERE IF NEEDED!       @SuppressWarnings("unchecked")    public SmartArray(){        //constructor        //initial capacity of the array should be DEFAULT_CAPACITY    }    @SuppressWarnings("unchecked")    public SmartArray(int initialCapacity){        // constructor        // set the initial capacity of...

  • Java you are required to add a public Object get( Object key) method into the Hashtable...

    Java you are required to add a public Object get( Object key) method into the Hashtable class. As defined in the Java documentation, the get() method returns the value with which the specified key is associated, or null if this hash table contains no record for the key. More formally, if this hash table contains a mapping from a key k to a value v such that (key.equals(k)), then this method returns v; otherwise it returns null. (There can be...

  • USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test...

    USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test all fuctionality. ------------- Interface code: public interface CustomList { /** * This method should add a new item into the CustomList and should * return true if it was successfully able to insert an item. * @param item the item to be added to the CustomList * @return true if item was successfully added, false if the item was not successfully added (note: it...

  • Complete LinkedListSet.java in java programming language. package Homework3; public class LinkedListSet extends LinkedListCollection { LinkedListSet() {...

    Complete LinkedListSet.java in java programming language. package Homework3; public class LinkedListSet extends LinkedListCollection { LinkedListSet() { } public boolean add(T element) { // Code here return true; } } Below is the LinkedListCollection.java code. Use to complete LinkedListSet.java. public class LinkedListCollection <T> { protected Node<T> head = null; public LinkedListCollection() { } public boolean isEmpty() { return head == null; } public int size() { int counter = 0; Node<T> cursor = head; while (cursor != null) { cursor =...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • In the first exercise, you will override the add method in a subclass in order to...

    In the first exercise, you will override the add method in a subclass in order to improve its performance. In the second exercise, you will implement an Iterator for your new linked list class. Exercise 1 The add method of the linked list class discussed during lecture performs in O(N) time. What can be done to improve its performance to O(1)? What are the boundary cases? Define a new class that inherits from the CS20bLinkedList class introduced during the lecture....

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