Question
Modify listlink.java program (non generic) by adding the following methods:

public void insertsorted(x); // Inert x in a sorted list.

public void deletex(x); //Search for x in the sorted list, if found, delete it from the sorted list.

Assume you have a data file p2.txt with the following contents:

8

4 15 23 12 36 5 36 42

3

5 14 4

and your java program is in xxxxx.java file, where xxxxx is the first 5 characters of your last name.

To compile: javac xxxxx.java

To execute: java xxxxx < any data file name say p2.txt


Your output should be formatted (i.e. using %4d for print) as follows:

The 8 inserted data are as follow:

4 5 12 15 23 36 36 42


Delete 3 data from the sorted list.

5: is deleted, now list is: 4 12 15 23 36 36 42

14: Ooops is not in the list?

4: is deleted! Now list is: 12 15 23 36 36 42



//ADT LIST (Link list implementation & no header in JAVA import java.util.*; import java.io.*; //* Class Definition /** Imple
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Done accordingly. Please comment for further help. Please uprate.

Code:

listlink.java

package Testing;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class listlink implements List{
  
   private class Node{
       int data;
       Node next;
       Node(){next=null;}
   }
   private Node head;
   private int count=0;
  
   listlink(){head=null;}
  
   public int length() {return count;}
   @Override
   public int size() {
       // TODO Auto-generated method stub
       return 0;
   }

   @Override
   public boolean isEmpty() {
       return (head==null);
   }
  
   public static void prt(String s) {
       System.out.print(s);
   }

   @Override
   public boolean contains(Object o) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public Iterator iterator() {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public Object[] toArray() {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public Object[] toArray(Object[] a) {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public boolean add(Object e) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public boolean remove(Object o) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public boolean containsAll(Collection c) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public boolean addAll(Collection c) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public boolean addAll(int index, Collection c) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public boolean removeAll(Collection c) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public boolean retainAll(Collection c) {
       // TODO Auto-generated method stub
       return false;
   }

   @Override
   public void clear() {
       // TODO Auto-generated method stub
      
   }

   @Override
   public Object get(int index) {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public Object set(int index, Object element) {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public void add(int index, Object element) {
       // TODO Auto-generated method stub
      
   }

   @Override
   public Object remove(int index) {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public int indexOf(Object o) {
       // TODO Auto-generated method stub
       return 0;
   }

   @Override
   public int lastIndexOf(Object o) {
       // TODO Auto-generated method stub
       return 0;
   }

   @Override
   public ListIterator listIterator() {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public ListIterator listIterator(int index) {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public List subList(int fromIndex, int toIndex) {
       // TODO Auto-generated method stub
       return null;
   }
  
   public String toString() {
       String s="[";
       for(Node cur=head;cur!=null;cur=cur.next)
           s+=cur.data+", ";
       return s+"]";
   }
  
   public void insertSorted(int x) {
       Node node=new Node();
       node.data=x;
       if(head==null) {
           head=node;
       }else {
           Node cur=head;
           Node pre=head;
           while(cur!=null && cur.data<x) {
               pre=cur;
               cur=cur.next;
           }
           if(cur==head) {
               node.next=head;
               head=node;
           }else {
           node.next=pre.next;
           pre.next=node;
           }
       }
   }
  
   public void delete(int x) {
           Node cur=head;
           Node pre=head;
       while(cur!=null && cur.data!=x) {
           pre=cur;
           cur=cur.next;
       }
      
       if(cur!=null && cur.data==x) {
           if(cur==head) {
               head=cur.next;
           }else {
               pre.next=cur.next;
           }
           return;
       }
       System.out.println(x+ ": Ooops is not in the list?\r\n");
   }
  
}

main.java

package Testing;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
   public static void main(String[] args) {
       listlink obj=new listlink();
       BufferedReader reader;
       try {
           reader = new BufferedReader(new FileReader(
                   "p2t.txt"));
           int totalInsert=Integer.parseInt(reader.readLine());
           String line = reader.readLine();
           String insertions[]=line.split(" ");
           for(String x:insertions) {
               System.out.println("Inserting :"+x);
               obj.insertSorted(Integer.parseInt(x));
           }
           System.out.println("After Inserting all data:"+obj.toString());
           int totalDeletion=Integer.parseInt(reader.readLine());
           line = reader.readLine();
           String deletion[]=line.split(" ");
           for(String x:deletion) {
               System.out.println("Deleting :"+x);
               obj.delete(Integer.parseInt(x));
           }
           System.out.println("After Deleting all data:"+obj.toString());
           reader.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
      
      
   }

}

Output:

Add a comment
Know the answer?
Add Answer to:
Modify listlink.java program (non generic) by adding the following methods: public void insertsorted(x); // Inert x...
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
  • Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k...

    Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k = 0, 1, 2 private void delete(x, k) // returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2 private int search(x, k) import java.util.Scanner; class xxxxxp3{ private node[] head = new node[3]; private class node{ int num; node link; node(int x){ num=x; link = null; } } public void prtlst(int k){ System.out.printf("\nContents of List-%d:",k); for(node cur...

  • JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private...

    JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private Link prev; public CircularList() { // implement: set both current and prev to null } public boolean isEmpty() { // implement return true; } public void insert(int id) { // implement: insert the new node behind the current node } public Link delete() { // implement: delete the node referred by current return null; } public Link delete(int id) { // implement: delete the...

  • Im making a generic linked list. I cant figure out how to make an object of...

    Im making a generic linked list. I cant figure out how to make an object of my class from main. My 3 files are main.cpp dan.h dan.cpp The error is: 15 6 [Error] prototype for 'void ll<T>::insert()' does not match any in class 'll<T>' #include <iostream> #include <string> #include "dan.h" using namespace std; int main() { ll<int> work; work.insert(55);//THIS IS THE LINE THATS GIVING ME THE ERROR, Without this line it compiles and //runs. } #ifndef DAN_H #define DAN_H #include...

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • 1. private Node head; private Node tail; public class Node { String name; Node next; Node...

    1. private Node head; private Node tail; public class Node { String name; Node next; Node prev; } public void displayInReverse(){ //write your code here to display names in reverse } 2. public class NodeOne { String name; NodeOne next; public NodeOne(String name) { this.name = name; } } // Complete the code snippet below so that the delete method works properly public void delete(String name) { NodeOne temp = head, prev = head; while (temp != null) { if...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args )...

    //LinkedList import java.util.Scanner; public class PoD {    public static void main( String [] args ) { Scanner in = new Scanner( System.in ); LinkedList teamList = new LinkedList(); final int TEAM_SIZE = Integer.valueOf(in.nextLine()); for (int i=0; i<TEAM_SIZE; i++) { String newTeamMember = in.nextLine(); teamList.append(newTeamMember); } while (in.hasNext()) { String removeMember = in.nextLine(); teamList.remove(removeMember); }    System.out.println("FINAL TEAM:"); System.out.println(teamList); in.close(); System.out.print("END OF OUTPUT"); } } =========================================================================================== //PoD import java.util.NoSuchElementException; /** * A listnked list is a sequence of nodes with...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add...

    Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add an instance method deleteFront such that … Method deleteFront has no input. Method deleteFront returns … an empty Optional instance if the list is empty an Optional instance whose value is the integer that was deleted from the front of the list, otherwise Hints https://docs.oracle.com/javase/10/docs/api/java/util/Optional.html import java.util.Optional; public class IntegerLinkedList { private IntegerNode head ; private int numberOfItems ; public int getNumberOfItems() { return...

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