Implement array-based list for integers; following methods should be implemented. Dynamic array would be preferred.
void insertAtPosition(int x,int pos); //insert value x at given position
void insertAtEnd(int x); //insert value at the end of the array
void insertAtFront(int x); //insert value at the first position of the array
void removeFromFront(); //remove element from first position and move all element up //to fill the gap
void removeFromEnd(); //remove element from the end of the array
void removeFromPosition(int pos); //remove element from the position given by user. After the //operation there should be no gap in the elements of array
int find(int x) const; //return the index
int findPosition(int pos) const; //find at given position and return the value
bool IsEmpty() const; //return true if list is empty
bool isFull() const; //return true if list is full
void print() const; //print the whole list
Note: Implement the main() to show that all above given methods are working properly
1
Expert's answer
2013-03-29T06:17:24-0400
#include <iostream> using std::cout; using std::endl;
template<class T> class CArray { struct Item { T value; };
Item * items;
int size;
int capacity;
public: CArray() { capacity = 1;
items = new Item[capacity];
size = 0; }
CArray(const CArray<T>& a) { if (this != &a) { capacity = a.GetSize();
items = new Item[capacity];
size = 0;
for (int i = 0; i < a.GetSize(); i++) { PushToEnd(a[i]); } } }
virtual ~CArray() { delete items; }
CArray& operator=(const CArray& a) { if (this != &a) { delete items;
capacity = a.GetSize();
items = new Item[capacity];
size = 0;
for (int i = 0; i < a.GetSize(); i++) { PushToEnd(a[i]); } } }
void PushToBegin(const T& value) { if (size == capacity) { int newCapacity = 2 * capacity;
Item * buffer = new Item[newCapacity];
for (int i = 0; i < size; i++) { buffer[i] = items[i]; }
delete items;
capacity = newCapacity;
items = new Item[capacity];
for (int i = 0; i < capacity; i++) { items[i] = buffer[i]; }
delete buffer; }
for (int i = size; i >= 0; i--) { items[i] = items[i - 1]; }
items[0].value = value;
size++; }
void PushToEnd(const T& value) { if (size == capacity) { int newCapacity = 2 * capacity;
Item * buffer = new Item[newCapacity];
for (int i = 0; i < size; i++) { buffer[i] = items[i]; }
delete items;
capacity = newCapacity;
items = new Item[capacity];
for (int i = 0; i < capacity; i++) { items[i] = buffer[i]; }
delete buffer; }
items[size].value = value;
size++; }
void RemoveFromBegin() { if (size > 0) { for (int i = 0; i < size; i++) { items[i] = items[i + 1]; }
size--; } }
void RemoveFromEnd() { size--; }
void Clear() { size = 0; }
int GetSize() const { return size; }
T operator[](const int index) const {
//if ((size < 0) || (index >= size)) { // throw IllegalArgumentException("Index must be more then zero."); //}
return items[index].value; } T& operator[](const int index)& {
//if ((size < 0) || (index >= size)) { // throw IllegalArgumentException("Index must be more then zero."); //}
return items[index].value; } };
template <class T> std::ostream& operator<<(std::ostream& out, const CArray<T>& a) { out << "["; for (int i = 0; i < a.GetSize(); i++) { out << a[i] << (i == (a.GetSize() - 1) ? "" : ", "); } out << "]"; return out; }
Comments
Leave a comment