Question

plzzz help me. in java it should all be in one program. plz help 1.3.19 Give...

plzzz help me. in java it should all be in one program. plz help

1.3.19 Give a code fragment that removes the last node in a linked list whose first node is first.

1.3.21 Write a method find() that takes a linked list and a string key as arguments and returns true if some node in the list has key as its item field, false otherwise.

1.3.26 Write a method remove() that takes a linked list and a string key as arguments and removes all of the nodes in the list that have key as its item field.

starting from nothing

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

Please find the code below:

LinkedlistOperations.java

package polygon;

import java.util.Scanner;

import staticclasses.bookType;

class Node {
   String data;
   Node next;
   Node(String value) {
       this.data = value;
   }
   void display() {
       System.out.print(data+" ");
   }
}
class linkedList {
   public Node first, lastnode;
   linkedList() {
       first = null;
       lastnode = null;
   }
   void insertnode(String value) {
       Node listnode = new Node(value);
       listnode.next = null;
       if(first == null) {
           first = lastnode = listnode;
       }
       else {
           lastnode.next = listnode;
           lastnode = listnode;
       }
   }
   void delete() {
       int count = 0, number, i;
       Node listnode, node1;
       Scanner input = new Scanner(System.in);

       for(listnode = first; listnode != null; listnode = listnode.next)
           count++;
       display();
       listnode = node1 = first;
       System.out.println(count+" nodes available here!");
       System.out.println("Enter the listnode number which you want to delete:");
       number = Integer.parseInt(input.nextLine());
       if(number != 1) {
           if(number <= count) {
               for(i = 2; i <= number; i++)
                   listnode = listnode.next;

               for(i = 2; i <= number-1; i++)
                   node1 = node1.next;

               node1.next = listnode.next;
               listnode.next = null;
               listnode = null;
               if(number==count){
                   lastnode = node1;
               }
           }
           else{
               System.out.println("Invalid listnode number! ");
               return;
           }

       }
       else {
           first = listnode.next;
           listnode = null;
       }

   }

   /* Display linked list */
   void display() {
       Node listnode = first;
       System.out.print("List is : ");
       while(listnode != null) {
           listnode.display();
           listnode = listnode.next;
       }
   }

   void removeLastNode() {
       Node listnode = first;
       Node lastnode = null;
       Node beforeLastNode = null;
       while(listnode != null) {
           beforeLastNode = lastnode;
           lastnode = listnode;
           listnode = listnode.next;
       }
       if(beforeLastNode!=null){
           beforeLastNode.next=null;
       }
   }
}

class LinkedlistOperations {

   static boolean find(linkedList list,String findMe){
       Node listnode = list.first;
       while(listnode != null) {
           if(listnode.data.equalsIgnoreCase(findMe)){
               return true;
           }
           listnode = listnode.next;
       }
       return false;
   }
  
   static void remove(linkedList list,String findMe){
       //removing from front
       while(list.first!=null && list.first.data.equalsIgnoreCase(findMe)){
           list.first = list.first.next;
       }
      
      
       Node listnode = list.first.next;
       Node lastnode = null;
       while(listnode != null) {
           if(listnode.data.equalsIgnoreCase(findMe)){
               if(lastnode!=null){
                   lastnode.next = listnode.next;
               }
           }else{
               lastnode = listnode;
           }
           listnode = listnode.next;
       }
      
      
       //removing from mid
      
   }
   public static void main(String args[ ]) {
       linkedList myList = new linkedList();
       myList.insertnode("30");
       myList.insertnode("30");
       myList.insertnode("30");
       myList.insertnode("10");
       myList.insertnode("20");
       myList.insertnode("30");
       myList.insertnode("30");
       myList.insertnode("30");
       myList.insertnode("40");
       myList.insertnode("50");
       myList.insertnode("30");
       myList.insertnode("30");
       myList.insertnode("30");
       myList.insertnode("50");
       myList.insertnode("30");
       myList.insertnode("30");
      
       myList.display();
       System.out.println(" Removing lastNode");
       myList.removeLastNode();
       myList.display();
       System.out.println(" Finding 20 : "+find(myList,"20"));
       System.out.println("Finding 90 : "+find(myList,"90"));
       System.out.println("Removing 30...");
       remove(myList,"30");
       myList.display();
      

   }
}

output:

Console X rinated> LinkedlistOperations Java Application] CAProgram Files Java jre7binjavaw.exe (Feb 8, 2019, 12:15:42 AM) Li

Add a comment
Know the answer?
Add Answer to:
plzzz help me. in java it should all be in one program. plz help 1.3.19 Give...
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
  • Exercise 1-3-26 on page 165: Write a method remove() in Java that takes a linked list...

    Exercise 1-3-26 on page 165: Write a method remove() in Java that takes a linked list and a string key as arguments and removes all of the nodes in the list that have key as its item field. (Book: Algorithms, 4th edition, by Sedgewick and Wayne)

  • *********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked...

    *********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked list recursively . The method returns a string with every element in the list and a space in between each element in reverse order. THE METHOD DOES NOT REVERSE THE LINKED LIST. IT JUST RETURNS A STRING CONTAINING THE ELEMENTS OF THE LIST IN REVERSE ORDER. ASSUME ALL THE ELEMENTS IN THE LINKED LIST ARE int... method signature is something like : public String...

  • Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up...

    Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up for helping. True/False (13) Chapter 14 - A List Implementation that Links Data Adding a node to an empty chain is the same as adding a node to the beginning of a chain. Adding a node at the end of a chain of n nodes is the same as adding a node at position n. You need a temporary variable to reference nodes as...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • Java Write the function void insertAtTail (int v). Don’t add any class variables to the List...

    Java Write the function void insertAtTail (int v). Don’t add any class variables to the List class. Here are the class definitions of Node and List that implement a linked list. class Node {private Node next; private int key; Node (Node nxt, int keyValue);//constructor Node getNext(); int getKey(); void putNext(Node nxt);} class List {//assume the class does not use a dummy Node private Node head; List ();//constructor boolean exists (int ky);//returns true if v is in the list void insertAtHead(int...

  • c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store...

    c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store one integer. For example, 12->3->5->777-111 is such a list. There are five nodes in this list. 12 is the head node and 111 is the tail node. (111 points to NULL.) Your linked list starts empty. It should support the following three operations: Add x to tail: Create a new node whose data field contains x. Append this node at the end of the...

  • in java programing language 3 b. Add a non-recursive method: public boolean remove( T item )...

    in java programing language 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 wasn't 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 ()

  • java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait)...

    java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...

  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

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