#ifndef CSC116_ARRAY_LIST_H #define CSC116_ARRAY_LIST_H #include <iostream> clas
ID: 671092 • Letter: #
Question
#ifndef CSC116_ARRAY_LIST_H
#define CSC116_ARRAY_LIST_H
#include <iostream>
class array_list
{
static const unsigned int INITIAL_SIZE = 6;
static const unsigned int GROW_FACTOR = 2;
private:
//Pointer to the array storing elements in the list
int* m_arraystorage;
//Size of the array pointed to by m_arraystorage
//(Note that not every position available in the array
// will necessarily be used by the list)
unsigned int m_capacity;
//Number of elements currently in the list
unsigned int m_size;
public:
// array_list constructor
//
// Construct a new, empty list
//
// Pre-conditions:
// none
//
// Post-conditions:
// m_capacity is set to INITIAL_SIZE
// m_arraystorage is initialized to point to an array of size INITIAL_SIZE
// m_size is set to 0
//
array_list();
// array_list destructor
// Empties the list and deallocates all internal
// storage
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// m_arraystorage has been deallocated.
//
~array_list();
// clear()
// Resets the list to its initial state.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// m_capacity is set to INITIAL_SIZE
// m_arraystorate is deallocated, then reallocated to point to an array of size INITIAL_SIZE
// m_size is set to 0
//
void clear();
// size()
// Returns the number of elements in the list.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// none
//
// Returns:
// The number of elements in the list.
//
unsigned int size() const;
// is_empty()
// Returns whether or not the list is empty.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// none
//
// Returns:
// true if the number of elements is 0
// false otherwise
//
bool is_empty() const;
// insert_front(value)
// Insert the provided value at the beginning of the list.
//
// If the insertion would cause the number of elements
// to exceed the capacity of m_arraystorage (that is, if
// m_size equals m_capacity - 1 before the insertion), then
// this method should call expand_storage() to increase the
// size of m_arraystorage.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// A new element with the provided value is added
// to the beginning of the list.
// m_size increases by 1
//
// Example:
// If the list is {1,2,3,4}, then insert_front(6)
// will result in the list {6,1,2,3,4}
//
void insert_front(const int value);
// insert_end(value)
// Insert the provided value at the end of the list.
//
// If the insertion would cause the number of elements
// to exceed the capacity of m_arraystorage (that is, if
// m_size equals m_capacity - 1 before the insertion), then
// this method should call expand_storage() to increase the
// size of m_arraystorage.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// A new element with the provided value is added
// to the end of the list.
// m_size increases by 1
//
// Example:
// If the list is {1,2,3,4}, then insert_end(10)
// will result in the list {1,2,3,4,10}
//
void insert_end(const int value);
// insert(value, pos)
// Insert the provided value into the list before the index
// given by pos. After the insertion, the value will be located
// at index pos. Note that indices start at 0.
//
// If the insertion would cause the number of elements
// to exceed the capacity of m_arraystorage (that is, if
// m_size equals m_capacity - 1 before the insertion), then
// this method should call expand_storage() to increase the
// size of m_arraystorage.
//
// Pre-conditions:
// This array_list object has been initialized.
// 0 <= pos < m_size
//
// Post-conditions:
// A new element with the provided value is inserted into
// the list at position pos.
// m_size increases by 1
//
// Example:
// If the list is {100,200,300,400}, then insert(10, 1)
// will result in the list {100, 10, 200, 300, 400}
//
void insert(const int value, const unsigned int pos);
// in_list(value)
// Search for the provided value in the list and return whether or
// not it was found.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// none
//
// Returns:
// true if value is found in the list
// false otherwise
//
bool in_list(const int value);
// get(pos)
// Retrieve and return the element at index pos in the list. Note that
// indices start at 0.
//
// Pre-conditions:
// This array_list object has been initialized.
// 0 <= pos < m_size
//
// Post-conditions:
// none
//
// Returns:
// Element at index pos
//
int get(const unsigned int pos);
// remove_front()
// Remove and return the first element of the list.
//
// Pre-conditions:
// This array_list object has been initialized.
// m_size >= 1
//
// Post-conditions:
// The first element of the list is deleted.
// m_size is decremented.
//
// Returns:
// The element that was removed.
//
// Example:
// If the list is {100,200,300,400}, then remove_front()
// would return the value 100 and the list would become
// {200, 300, 400}.
//
int remove_front();
// remove_end()
// Remove and return the last element of the list.
//
// Pre-conditions:
// This array_list object has been initialized.
// m_size >= 1
//
// Post-conditions:
// The last element of the list is deleted.
// m_size is decremented.
//
// Returns:
// The element that was removed.
//
// Example:
// If the list is {100,200,300,400}, then remove_front()
// would return the value 400 and the list would become
// {100, 200, 300}.
//
int remove_end();
// remove(pos)
// Remove and return the last element of the list.
//
// Pre-conditions:
// This array_list object has been initialized.
// m_size >= 1
// 0 <= pos < m_size
//
// Post-conditions:
// The element at index pos in the list is deleted.
// m_size is decremented.
//
// Returns:
// The element that was removed.
//
// Example:
// If the list is {100,200,300,400}, then remove_front(2)
// would return the value 300 and the list would become
// {100, 200, 400}.
//
int remove(const unsigned int pos);
// bubble_sort()
// Sort the contents of this array_list with the bubble sort
// algorithm. See the wikipedia article at
// http://en.wikipedia.org/wiki/Bubble_sort
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// The elements of the list have been rearranged into ascending
// (i.e. smallest to largest) order.
//
void bubble_sort();
// Operator for stream printing.
// This operator allows array_list objects to be printed to output
// streams (such as cout) using the << operator.
//
// The list should be printed as comma-separated values inside
// a pair of braces. For example, "{1,2,3,4}" or "{10}". An empty
// list should be represented by "{}". This method must not print
// any newline characters (" " or endl) to the stream.
//
// Pre-conditions:
// The output stream s is valid.
// The array_list l is valid and has been initialized.
//
// Post-conditions:
// The contents of the list have been printed to the provided
// stream s using the format described above.
//
// Returns:
// The stream s
//
friend std::ostream & operator<< (std::ostream& s, const array_list& l);
private:
// expand_storage()
// Expand the internal m_arraystorage array by a factor of GROW_FACTOR.
//
// This is a five step process:
// 1. Create a new array of size GROW_FACTOR*m_capacity
// 2. Copy all elements from m_arraystorage into the new array
// 3. Deallocate m_arraystorage.
// 4. Set m_arraystorage to point to the new array.
// 5. Update m_capacity to reflect the size of the new array.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// m_capacity has been increased by a factor of GROW_FACTOR
// m_arraystorage points to an array whose size is the new value
// of m_capacity and which contains all elements previously stored
// in m_arraystorage.
//
void expand_storage();
//The signatures below describe two different forms of the
//copy operations. Normally, the compiler provides a default
//implementation of both operations unless they are otherwise
//defined. The default implementations will not work with this
//object, so we declare the operations private (since we will
//not be implementing either operation).
array_list (const array_list &);
array_list& operator= (array_list &);
};
#endif
Explanation / Answer
Complete Program:
File: array_list.h
#ifndef CSC116_ARRAY_LIST_H
#define CSC116_ARRAY_LIST_H
#include <iostream>
class array_list
{
static const unsigned int INITIAL_SIZE = 6;
static const unsigned int GROW_FACTOR = 2;
private:
//Pointer to the array storing elements in the list
int* m_arraystorage;
//Size of the array pointed to by m_arraystorage
//(Note that not every position available in the array
// will necessarily be used by the list)
unsigned int m_capacity;
//Number of elements currently in the list
unsigned int m_size;
public:
// array_list constructor
//
// Construct a new, empty list
//
// Pre-conditions:
// none
//
// Post-conditions:
// m_capacity is set to INITIAL_SIZE
// m_arraystorage is initialized to point to an array of size INITIAL_SIZE
// m_size is set to 0
//
array_list();
// array_list destructor
// Empties the list and deallocates all internal
// storage
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// m_arraystorage has been deallocated.
//
~array_list();
// clear()
// Resets the list to its initial state.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// m_capacity is set to INITIAL_SIZE
// m_arraystorate is deallocated, then reallocated to point to an array of size INITIAL_SIZE
// m_size is set to 0
//
void clear();
// size()
// Returns the number of elements in the list.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// none
//
// Returns:
// The number of elements in the list.
//
unsigned int size() const;
// is_empty()
// Returns whether or not the list is empty.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// none
//
// Returns:
// true if the number of elements is 0
// false otherwise
//
bool is_empty() const;
// insert_front(value)
// Insert the provided value at the beginning of the list.
//
// If the insertion would cause the number of elements
// to exceed the capacity of m_arraystorage (that is, if
// m_size equals m_capacity - 1 before the insertion), then
// this method should call expand_storage() to increase the
// size of m_arraystorage.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// A new element with the provided value is added
// to the beginning of the list.
// m_size increases by 1
//
// Example:
// If the list is {1,2,3,4}, then insert_front(6)
// will result in the list {6,1,2,3,4}
//
void insert_front(const int value);
// insert_end(value)
// Insert the provided value at the end of the list.
//
// If the insertion would cause the number of elements
// to exceed the capacity of m_arraystorage (that is, if
// m_size equals m_capacity - 1 before the insertion), then
// this method should call expand_storage() to increase the
// size of m_arraystorage.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// A new element with the provided value is added
// to the end of the list.
// m_size increases by 1
//
// Example:
// If the list is {1,2,3,4}, then insert_end(10)
// will result in the list {1,2,3,4,10}
//
void insert_end(const int value);
// insert(value, pos)
// Insert the provided value into the list before the index
// given by pos. After the insertion, the value will be located
// at index pos. Note that indices start at 0.
//
// If the insertion would cause the number of elements
// to exceed the capacity of m_arraystorage (that is, if
// m_size equals m_capacity - 1 before the insertion), then
// this method should call expand_storage() to increase the
// size of m_arraystorage.
//
// Pre-conditions:
// This array_list object has been initialized.
// 0 <= pos < m_size
//
// Post-conditions:
// A new element with the provided value is inserted into
// the list at position pos.
// m_size increases by 1
//
// Example:
// If the list is {100,200,300,400}, then insert(10, 1)
// will result in the list {100, 10, 200, 300, 400}
//
void insert(const int value, const unsigned int pos);
// in_list(value)
// Search for the provided value in the list and return whether or
// not it was found.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// none
//
// Returns:
// true if value is found in the list
// false otherwise
//
bool in_list(const int value);
// get(pos)
// Retrieve and return the element at index pos in the list. Note that
// indices start at 0.
//
// Pre-conditions:
// This array_list object has been initialized.
// 0 <= pos < m_size
//
// Post-conditions:
// none
//
// Returns:
// Element at index pos
//
int get(const unsigned int pos);
// remove_front()
// Remove and return the first element of the list.
//
// Pre-conditions:
// This array_list object has been initialized.
// m_size >= 1
//
// Post-conditions:
// The first element of the list is deleted.
// m_size is decremented.
//
// Returns:
// The element that was removed.
//
// Example:
// If the list is {100,200,300,400}, then remove_front()
// would return the value 100 and the list would become
// {200, 300, 400}.
//
int remove_front();
// remove_end()
// Remove and return the last element of the list.
//
// Pre-conditions:
// This array_list object has been initialized.
// m_size >= 1
//
// Post-conditions:
// The last element of the list is deleted.
// m_size is decremented.
//
// Returns:
// The element that was removed.
//
// Example:
// If the list is {100,200,300,400}, then remove_front()
// would return the value 400 and the list would become
// {100, 200, 300}.
//
int remove_end();
// remove(pos)
// Remove and return the last element of the list.
//
// Pre-conditions:
// This array_list object has been initialized.
// m_size >= 1
// 0 <= pos < m_size
//
// Post-conditions:
// The element at index pos in the list is deleted.
// m_size is decremented.
//
// Returns:
// The element that was removed.
//
// Example:
// If the list is {100,200,300,400}, then remove_front(2)
// would return the value 300 and the list would become
// {100, 200, 400}.
//
int remove(const unsigned int pos);
// bubble_sort()
// Sort the contents of this array_list with the bubble sort
// algorithm. See the wikipedia article at
// http://en.wikipedia.org/wiki/Bubble_sort
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// The elements of the list have been rearranged into ascending
// (i.e. smallest to largest) order.
//
void bubble_sort();
// Operator for stream printing.
// This operator allows array_list objects to be printed to output
// streams (such as cout) using the << operator.
//
// The list should be printed as comma-separated values inside
// a pair of braces. For example, "{1,2,3,4}" or "{10}". An empty
// list should be represented by "{}". This method must not print
// any newline characters (" " or endl) to the stream.
//
// Pre-conditions:
// The output stream s is valid.
// The array_list l is valid and has been initialized.
//
// Post-conditions:
// The contents of the list have been printed to the provided
// stream s using the format described above.
//
// Returns:
// The stream s
//
friend std::ostream & operator<< (std::ostream& s, const array_list& l);
private:
// expand_storage()
// Expand the internal m_arraystorage array by a factor of GROW_FACTOR.
//
// This is a five step process:
// 1. Create a new array of size GROW_FACTOR*m_capacity
// 2. Copy all elements from m_arraystorage into the new array
// 3. Deallocate m_arraystorage.
// 4. Set m_arraystorage to point to the new array.
// 5. Update m_capacity to reflect the size of the new array.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// m_capacity has been increased by a factor of GROW_FACTOR
// m_arraystorage points to an array whose size is the new value
// of m_capacity and which contains all elements previously stored
// in m_arraystorage.
//
void expand_storage();
//The signatures below describe two different forms of the
//copy operations. Normally, the compiler provides a default
//implementation of both operations unless they are otherwise
//defined. The default implementations will not work with this
//object, so we declare the operations private (since we will
//not be implementing either operation).
array_list (const array_list &);
array_list& operator= (array_list &);
};
#endif
File: array_list.cpp
#include "array_list.h"
// array_list constructor
//
// Construct a new, empty list
//
// Pre-conditions:
// none
//
// Post-conditions:
// m_capacity is set to INITIAL_SIZE
// m_arraystorage is initialized to point to an array of size INITIAL_SIZE
// m_size is set to 0
//
array_list::array_list()
{
m_capacity = INITIAL_SIZE;
m_arraystorage = new int[INITIAL_SIZE];
m_size = 0;
}
// array_list destructor
// Empties the list and deallocates all internal
// storage
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// m_arraystorage has been deallocated.
//
array_list::~array_list()
{
delete [] m_arraystorage;
}
// clear()
// Resets the list to its initial state.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// m_capacity is set to INITIAL_SIZE
// m_arraystorate is deallocated, then reallocated to point to an array of size INITIAL_SIZE
// m_size is set to 0
//
void array_list::clear()
{
m_capacity = INITIAL_SIZE;
delete [] m_arraystorage;
m_arraystorage = new int[INITIAL_SIZE];
m_size = 0;
}
// size()
// Returns the number of elements in the list.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// none
//
// Returns:
// The number of elements in the list.
//
unsigned int array_list::size() const
{
return m_size;
}
// is_empty()
// Returns whether or not the list is empty.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// none
//
// Returns:
// true if the number of elements is 0
// false otherwise
//
bool array_list::is_empty() const
{
return (m_size == 0);
}
// insert_front(value)
// Insert the provided value at the beginning of the list.
//
// If the insertion would cause the number of elements
// to exceed the capacity of m_arraystorage (that is, if
// m_size equals m_capacity - 1 before the insertion), then
// this method should call expand_storage() to increase the
// size of m_arraystorage.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// A new element with the provided value is added
// to the beginning of the list.
// m_size increases by 1
//
// Example:
// If the list is {1,2,3,4}, then insert_front(6)
// will result in the list {6,1,2,3,4}
//
void array_list::insert_front(const int value)
{
if(m_size == m_capacity - 1)
expand_storage();
for(int i = m_size; i >= 0; i--)
{
m_arraystorage[i] = m_arraystorage[i - 1];
}
m_arraystorage[0] = value;
m_size++;
}
// insert_end(value)
// Insert the provided value at the end of the list.
//
// If the insertion would cause the number of elements
// to exceed the capacity of m_arraystorage (that is, if
// m_size equals m_capacity - 1 before the insertion), then
// this method should call expand_storage() to increase the
// size of m_arraystorage.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// A new element with the provided value is added
// to the end of the list.
// m_size increases by 1
//
// Example:
// If the list is {1,2,3,4}, then insert_end(10)
// will result in the list {1,2,3,4,10}
//
void array_list::insert_end(const int value)
{
if(m_size == m_capacity - 1)
expand_storage();
m_arraystorage[m_size] = value;
m_size++;
}
// insert(value, pos)
// Insert the provided value into the list before the index
// given by pos. After the insertion, the value will be located
// at index pos. Note that indices start at 0.
//
// If the insertion would cause the number of elements
// to exceed the capacity of m_arraystorage (that is, if
// m_size equals m_capacity - 1 before the insertion), then
// this method should call expand_storage() to increase the
// size of m_arraystorage.
//
// Pre-conditions:
// This array_list object has been initialized.
// 0 <= pos < m_size
//
// Post-conditions:
// A new element with the provided value is inserted into
// the list at position pos.
// m_size increases by 1
//
// Example:
// If the list is {100,200,300,400}, then insert(10, 1)
// will result in the list {100, 10, 200, 300, 400}
//
void array_list::insert(const int value, const unsigned int pos)
{
if(pos < 0 || pos >= m_size)
{
std::cout << pos << " is an invalid position to insert a value ";
return;
}
if(m_size == m_capacity - 1)
expand_storage();
for(int i = m_size; i >= pos; i--)
{
m_arraystorage[i] = m_arraystorage[i - 1];
}
m_arraystorage[pos] = value;
m_size++;
}
// in_list(value)
// Search for the provided value in the list and return whether or
// not it was found.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// none
//
// Returns:
// true if value is found in the list
// false otherwise
//
bool array_list::in_list(const int value)
{
for(int i = 0; i < m_size; i++)
{
if(m_arraystorage[i] == value)
return true;
}
return false;
}
// get(pos)
// Retrieve and return the element at index pos in the list. Note that
// indices start at 0.
//
// Pre-conditions:
// This array_list object has been initialized.
// 0 <= pos < m_size
//
// Post-conditions:
// none
//
// Returns:
// Element at index pos
//
int array_list::get(const unsigned int pos)
{
if(pos < 0 || pos >= m_size)
{
std::cout << pos << " is an invalid position to get a value ";
return -9999;
}
return m_arraystorage[pos];
}
// remove_front()
// Remove and return the first element of the list.
//
// Pre-conditions:
// This array_list object has been initialized.
// m_size >= 1
//
// Post-conditions:
// The first element of the list is deleted.
// m_size is decremented.
//
// Returns:
// The element that was removed.
//
// Example:
// If the list is {100,200,300,400}, then remove_front()
// would return the value 100 and the list would become
// {200, 300, 400}.
//
int array_list::remove_front()
{
if(m_size < 1)
{
std::cout << "List is empty ";
return -9999;
}
int removed_value = m_arraystorage[0];
for(int i = 0; i < m_size - 1; i++)
m_arraystorage[i] = m_arraystorage[i + 1];
m_size--;
return removed_value;
}
// remove_end()
// Remove and return the last element of the list.
//
// Pre-conditions:
// This array_list object has been initialized.
// m_size >= 1
//
// Post-conditions:
// The last element of the list is deleted.
// m_size is decremented.
//
// Returns:
// The element that was removed.
//
// Example:
// If the list is {100,200,300,400}, then remove_front()
// would return the value 400 and the list would become
// {100, 200, 300}.
//
int array_list::remove_end()
{
if(m_size < 1)
{
std::cout << "List is empty ";
return -9999;
}
int removed_value = m_arraystorage[m_size - 1];
m_size--;
return removed_value;
}
// remove(pos)
// Remove and return the last element of the list.
//
// Pre-conditions:
// This array_list object has been initialized.
// m_size >= 1
// 0 <= pos < m_size
//
// Post-conditions:
// The element at index pos in the list is deleted.
// m_size is decremented.
//
// Returns:
// The element that was removed.
//
// Example:
// If the list is {100,200,300,400}, then remove_front(2)
// would return the value 300 and the list would become
// {100, 200, 400}.
//
int array_list::remove(const unsigned int pos)
{
if(m_size < 1)
{
std::cout << "List is empty ";
return -9999;
}
if(pos < 0 || pos >= m_size)
{
std::cout << pos << " is an invalid position to remove a value ";
return -9999;
}
int removed_value = m_arraystorage[pos];
for(int i = pos; i < m_size - 1; i++)
m_arraystorage[i] = m_arraystorage[i + 1];
m_size--;
return removed_value;
}
// bubble_sort()
// Sort the contents of this array_list with the bubble sort
// algorithm. See the wikipedia article at
// http://en.wikipedia.org/wiki/Bubble_sort
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// The elements of the list have been rearranged into ascending
// (i.e. smallest to largest) order.
//
void array_list::bubble_sort()
{
int n = m_size;
do
{
int newn = 0;
for(int i = 1; i <= n -1; i++)
{
if(m_arraystorage[i - 1] > m_arraystorage[i])
{
int temp = m_arraystorage[i - 1];
m_arraystorage[i - 1] = m_arraystorage[i];
m_arraystorage[i] = temp;
newn = i;
}
}
n = newn;
}while(n != 0);
}
// Operator for stream printing.
// This operator allows array_list objects to be printed to output
// streams (such as cout) using the << operator.
//
// The list should be printed as comma-separated values inside
// a pair of braces. For example, "{1,2,3,4}" or "{10}". An empty
// list should be represented by "{}". This method must not print
// any newline characters (" " or endl) to the stream.
//
// Pre-conditions:
// The output stream s is valid.
// The array_list l is valid and has been initialized.
//
// Post-conditions:
// The contents of the list have been printed to the provided
// stream s using the format described above.
//
// Returns:
// The stream s
//
std::ostream & operator<<(std::ostream& s, const array_list& l)
{
for(int i = 0; i < l.m_size; i++)
s << l.m_arraystorage[i] << " ";
s << " ";
return s;
}
// expand_storage()
// Expand the internal m_arraystorage array by a factor of GROW_FACTOR.
//
// This is a five step process:
// 1. Create a new array of size GROW_FACTOR*m_capacity
// 2. Copy all elements from m_arraystorage into the new array
// 3. Deallocate m_arraystorage.
// 4. Set m_arraystorage to point to the new array.
// 5. Update m_capacity to reflect the size of the new array.
//
// Pre-conditions:
// This array_list object has been initialized.
//
// Post-conditions:
// m_capacity has been increased by a factor of GROW_FACTOR
// m_arraystorage points to an array whose size is the new value
// of m_capacity and which contains all elements previously stored
// in m_arraystorage.
//
void array_list::expand_storage()
{
int *newArray = new int[GROW_FACTOR * m_capacity];
for(int i = 0; i < m_size; i++)
newArray[i] = m_arraystorage[i];
delete [] m_arraystorage;
m_arraystorage = newArray;
m_capacity = GROW_FACTOR * m_capacity;
}
//The signatures below describe two different forms of the
//copy operations. Normally, the compiler provides a default
//implementation of both operations unless they are otherwise
//defined. The default implementations will not work with this
//object, so we declare the operations private (since we will
//not be implementing either operation).
array_list::array_list(const array_list &l)
{
delete [] m_arraystorage;
m_arraystorage = new int[l.m_capacity];
for(int i = 0; i < l.m_size; i++)
m_arraystorage[i] = l.m_arraystorage[i];
m_capacity = l.m_capacity;
m_size = l.m_size;
}
array_list& array_list::operator=(array_list &l)
{
if(this == &l)
return *this;
delete [] m_arraystorage;
m_arraystorage = new int[l.m_capacity];
for(int i = 0; i < l.m_size; i++)
m_arraystorage[i] = l.m_arraystorage[i];
m_capacity = l.m_capacity;
m_size = l.m_size;
return *this;
}
File: array_list_test.cpp
#include "array_list.h"
#include <iostream>
// start main function
int main()
{
array_list alist;
alist.insert_front(5);
alist.insert_front(2);
alist.insert_front(8);
alist.insert_front(1);
alist.insert_front(6);
alist.insert_front(3);
alist.insert_end(7);
alist.insert_end(4);
alist.insert(9, 5);
std::cout << "List before sorting: " << alist;
alist.bubble_sort();
std::cout << "List after sorting: " << alist;
std::cout << "remove(4): " << alist.remove(4) << " ";
std::cout << "remove_end(): " << alist.remove_end() << " ";
std::cout << "remove_front(): " << alist.remove_front() << " ";
std::cout << " List: " << alist;
std::cout << "Size: " << alist.size() << " ";
std::cout << "is_empty(): " << alist.is_empty() << " ";
std::cout << "in_list(3): " << alist.in_list(3) << " ";
std::cout << "get(2): " << alist.get(2) << " ";
std::cout << std::endl;
system("pause"); // for MS visual studio
return 0;
} // end if main function
Sample Output:
List before sorting: 3 6 1 8 2 9 5 7 4
List after sorting: 1 2 3 4 5 6 7 8 9
remove(4): 5
remove_end(): 9
remove_front(): 1
List: 2 3 4 6 7 8
Size: 6
is_empty(): 0
in_list(3): 1
get(2): 4
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.