Assuming you have the following ordered-array class definition:
class ordered_array {
public:
ordered_array(int c) {
data = new int[c];
cp = c;
sz = 0;
}
...
private:
int sz, cp; // Current size, total capacity
int* data = nullptr;
};
Suppose that ordered_arrays can contain duplicates. Implement a method remove_duplicates(e)which takes an element e and removes it and any duplicates of it. (Note that e is the actual value to be removed, not its index in the array.) If e does not exist in the array then the array should be left unchanged. (Hint: this is just the normal remove except that all copies of the element are removed.)
void ordered_array::remove_duplicates(int e) {
Given below is the code for the question. Please do rate the answer
if it helped. Thank you.
void ordered_array::remove_duplicates(int e) {
int firstIdx = -1, lastIdx = -1;
int numOccurences = 0;
for(int i = 0; i < sz; i++){
if(data[i] == e){
firstIdx =
i;
numOccurences =
1;
break;
}
}
if(firstIdx != -1){
//find the index of non-matching
element which should overwrite existing spaces
int index;
for(index = firstIdx + 1; index
< sz; index++)
{
if(data[index]
!= e)
break;
numOccurences++;
}
//shift the elements to cover the
deleted elements
for(int i = firstIdx, j = index; j
< sz; i++, j++){
data[i] =
data[j];
}
//update size
sz -= numOccurences;
}
}
Assuming you have the following ordered-array class definition: class ordered_array { public: ordered_array(int c) { data...
Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int virtualArrayLength; // the number of elements in the dynamic array // Throws an IndexOutOfBoundsException if i is not a valid index // for adding to the dynamic array, otherwise inserts s at index i. // Elements can be added from index 0 to this.size(). public void add(int i, String s) { // If there is no room for s in data, create a new...
Consider the following definition of a C++ class that acts like an array with bounds-checking (i.e. ensuring that a valid array index is used each time an array element is referenced). class c_array { public: c_array(int s){ size = s; a = new int[size]; } int &operator[](int i); int get_size() { return size; } private: int size; int *a; }; 1- Write the header for the c-array class in a different header file (c_array.h). 2- Complete the definition of the...
I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item) Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...
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
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...
Array with Iterator. Java style
Implement an array data structure as a class JstyArray<E>
to support the Iterable interface such that the following code
works:
JstyArray<Integer> data;
data = new JstyArray<Integer>(10);
for (int i = 0; i < 10; ++i) {
data.set(i, new Integer(i) );
}
int sum = 0;
for ( int v : data ) {
if (v == null)
continue; // empty cell
sum += v;
}
The iterator provided by this class follows the behaviour of...
Write a generic array list class. Task Description Your goal for this lab is to write a generic ArrayList class similar to the one in the given lecture notes on array lists. The ArrayList Class The public constructors and methods required for the ArrayList class are listed here. The type E is the generic type of an element of the list. ArrayList() Construct an empty ArrayList object. int size() Return the size (number of items) in this ArrayList. boolean isEmpty()...
Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue and LinkedQueue: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add N Integers to the structure and how long it takes to remove N Integers from the structure. N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. Depending...
Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> { // Data fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; @SuppressWarnings("unchecked") public KWArrayList() { capacity...
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...