PART 1
Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is, if the list is full when an item is being added to this list, the elements will be moved to a larger array. The new array should have twice the size of the original array. Using the new class ArrayList, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should be set to 100. Print the numbers
/******************************
* Week 1 lab - exercise 1: *
* a simple ArrayList class *
*******************************/
#include
#include "ArrayList.h"
using namespace std;
/*
* Default constructor. Sets length to 0, initializing the list as
an empty
* list. Default size of array is 20.
*/
ArrayList::ArrayList()
{
SIZE = 20;
list = new int[SIZE];
length = 0;
}
/*
* Destructor. Deallocates the dynamic array list.
*/
ArrayList::~ArrayList()
{
delete [] list;
list = NULL;
}
/*
* Determines whether the list is empty.
*
* Returns true if the list is empty, false otherwise.
*/
bool ArrayList::isEmpty()
{
return length == 0;
}
/*
* Prints the list elements.
*/
void ArrayList::display()
{
for (int i=0; i < length; i++)
cout << list[i] << "
";
cout << endl;
}
/*
* Adds the element x to the end of the list. List length is
increased by 1.
*
* x: element to be added to the list
*/
void ArrayList::add(int x)
{
if (length == SIZE)
{
cout << "Insertion Error:
list is full" << endl;
}
else
{
list[length] = x;
length++;
}
}
/*
* Removes the element at the given location from the list. List
length is
* decreased by 1.
*
* pos: location of the item to be removed
*/
void ArrayList::removeAt(int pos)
{
if (pos < 0 || pos >= length)
{
cout << "Removal Error:
invalid position" << endl;
}
else
{
for ( int i = pos; i < length -
1; i++ )
list[i] =
list[i+1];
length--;
}
}
PART 2
Create a class Bag (multiset) that uses an expandable array to store the bag items. The item type must be int. The class should have the methods listed below. Create a main class to test your bag class. This main class should fill a bag of integers with 10 random numbers, each in the interval [0, 20], and print how many times each integer in the interval [0, 20] appears in the bag.
PART 3
Write a program that fills a vector object with the keywords of the C++ language. Once filled, the vector elements should be displayed.
PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is,...
C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...
This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...
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: ...
Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...
C++ Please ensure code is fully working, will rate Question to be answered: Repeat Exercise 1 for the class linkedStackType question one: Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...
I'm just not sure how to tackle all the template class this header wants me to write. I got this far into making the template for public and private and would like help on the template functions. Thank you! This is in C++ by the way. #include <iostream> #include <cassert> using namespace std; #ifndef ARRAYLIST_H #define ARRAYLIST_H template<typename T> class arrayList { public: arrayList(); //Constructor with default parameter. //Sets maxSize = 100 and length = 0 if no parameter...
1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...
Please help me finish my code. Before the final pause in the main function, create an ArrayList of another type such as double, char, short, bool, float, etc. Your choice. Add five data items to your array as we did for the int and string array lists. Print them out with a for loop as we did before. Print out the count and capacity of your new array list. Source.cpp #include #include #include #include "ArrayList.h" using namespace std; /// Entry...
I am currently using eclipse to write in java.
A snapshot of the output would be greatly appreciated to verify
that the program is indeed working. Thanks in advance for both your
time and effort.
Here is the previous exercise code:
/////////////////////////////////////////////////////Main
/*******************************************
* Week 5 lab - exercise 1 and exercise 2: *
* ArrayList class with search algorithms *
********************************************/
import java.util.*;
/**
* Class to test sequential search, sorted search, and binary search
algorithms
* implemented in...
#include <iostream> using namespace std; template <typename Item> class MyArray{ private: Item *myarray; int size; int used; void doubleSize(); public: MyArray(); ~MyArray(); int length(); void insertHead(Item i); void insertTail(Item i); void deleteHead(); void deleteTail(); void sortAscending(); void sortDescending(); Item operator [](int i){ return myarray[i]; } }; template <typename Item> MyArray<Item>::MyArray(){ size = 5; used = 0; myarray = new Item[size];...