Question

Please write in Java Recall that the ADT list class methods are;  void List() ...

Please write in Java

Recall that the ADT list class methods are;

 void List()

 bool isEmpty()

 int size()

 void add(int item, int pos)//inserts item at specified position (first postion is 1)

 void remove(int index)//removes item from specified position

 void removeAll()

 int indexOf(int item)//returns the index of item

 int itemAt(int index)//returns the item in position specified by index

Implementation of LIST ADT operations and details are hidden. Only the methods listed above are given. Assume list contains some integer numbers and this list may contain repeated consecutive integer numbers. Write a function removeRepeated(LIST &) to remove those consecutively repeated integer numbers from the list by leaving only one copy of them.

Consider following example:

Before:{10,50,10,60,60,60,60,40,10,10,10,10,80,3,3,3}

After:{10,50,10,60,40,10,80,3}

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

solution:
This solution has an explanation, full java code, comments with code and screenshot of

execution of the program for your clear and better understanding.

EXPLANATION:

Before going to directly answer first let's see the approach for solving this problem.

First, declare the list of type integers. and then take inputs by the scanner class.

Take inputs as the string with space splitting and then parse the string into integers and

store them into the temporary array.

after that add all integers into the list using add() method.

Now call the removeRepeated method with the passing list.

Now the main logic comes. so check two continuous number. If both are equal then don't consider it.

If they are not equal then add into another list which will be final results.

Now print the result.

for more visualization see below:

Java code :

// import the lib
import java.util.*;
// class
public class Main
{   
//method to remove repeated numbers
static void removeRepeated(   List<Integer> list1){
// new list
List<Integer> list2 = new ArrayList<Integer>();
System.out.println("before removing repeated numbers : ");
System.out.println(list1);
int i,item1,item2,len = list1.size(); // find size of list1
for(i=0;i<len-1;i++){
// find current and next item & compare
item1 = list1.get(i);
item2 = list1.get(i+1);
// if not equals then only add to new list
if(list1.indexOf(item1)!=list1.indexOf(item2)) // find index of item and compare
list2.add(item1);
}
// now check for last item
item2 = list1.get(i);
item1 = list1.get(i-1);
if(list1.indexOf(item1)==list1.indexOf(item2)) // find index of item and compare
list2.add(item2);
  
System.out.println("After removing repeated numbers : ");
System.out.println(list2);
}
// main method
   public static void main (String[] args)
   {
   // Creating a list of type integers
List<Integer> list1 = new ArrayList<Integer>();
// take inputs
   System.out.println("Enter repeated numbers : ");
   Scanner s1 = new Scanner(System.in);
   // separated by space
String[] listStr = s1.nextLine().split(" ");
int len = listStr.length;
// temporary array to store integers
Integer[] arrTemp = new Integer[len];
// parse to int and store in array
for (int i = 0; i < len; i++) {
arrTemp[i] = Integer.parseInt(listStr[i]);
}
//close the scanner
s1.close();
// convert array Integers array to list
for (int i : arrTemp) {
list1.add(i);
}
// now call function to remove repeated numbers
   removeRepeated(list1);
   }
}


screenshot:

This code is compiled and executed in the terminal.


since each and everything is provided for your full understanding.

However further if any difficulty in understanding the code and approach

then feel free to ask in the comments section.

I will definitely help you.

Add a comment
Know the answer?
Add Answer to:
Please write in Java Recall that the ADT list class methods are;  void List() ...
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
  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 25...

    Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 25 items. The pseudocode for the ADT Sorted List Operations are provided on page 210. Use this information to create an ADT for handling a collection of Person objects, where each object will contain a Social Insurance Number (validate this), a first name, a last name, a gender and a data of birth. This implementation should prevent duplicate entries – that is, the Social Insurance...

  • To solve real world problem, the programer uses ADT like list, stack, queue, set, map, ... etc. t...

    To solve real world problem, the programer uses ADT like list, stack, queue, set, map, ... etc. to build our data model. In AS8, we pick and choose STL queue and stack to build a postfix calculator. In this assignment you are to Design your own linked-list, a series of integer nodes. The private attributes of this IntLinked Queue including a integer node struct and private control variables and methods: private: struct Node { int data; Node *next; }; Node...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • 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...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • C# public int IndexOf(T element) { for (var i = 0; i < Count; i++) {...

    C# public int IndexOf(T element) { for (var i = 0; i < Count; i++) { if (data[i].Equals(element)) return i; } return -1; } You must complete the Vector<T> implementation by providing the following functionality: void Insert(int index, T item) Inserts a new element into the data structure at the specified index. This will involve four parts (Note a part may be more than a single line of code): o If Count already equals Capacity (eg the currently allocated space...

  • 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....

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