Sicily 2250. Vector Implementation

2250. Vector Implementation

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB , Framework Judge

Description

Implement  the following class Vector:

typedef int T;


class Vector {
public: //  methods of the List ADT
   Vector();//construct an empty list
   Vector(int n);//construct a list of n objects
   ~Vector();
   Vector(const Vector & c);
   const Vector& operator = (Vector & c);
   int size() const;
   int capacity()const;
   bool empty() const;
   void clear(); // erase all objects
   T & operator [] (int position);
   int find( T &x) const; //return the position of the first occurrence of x if it exists, return -1 otherwise.
   int insert(int position, const T &x);//insert x at position
   int push_back(const T &x);//put x after the last item 
   int erase(int position);
   void traverse(void (*visit)(T &));
 private:
    T *elems;
    int counter; //number of items in the list
    int arraySize; // the size of the array 
};
 

 typedef Stack MyStack;

If you are using templates, then define MyStack in the following way:

typedef Stack<int> MyStack;

 

 

Hint

Submit only class implementation

 

Problem Source

List Implementations and Applications

// Problem#: 2250
// Submission#: 3370988
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
typedef int T;
class Vector {
public:
   Vector() {
       counter = 0;
       arraySize = 0;
       elems = NULL;
   }
   Vector(int n) {
       counter = n;
       arraySize = n;
       if (n) elems = new int[arraySize];
       else elems = NULL;
       for (int i = 0; i < n; i++) elems[i] = T();
   }
   ~Vector() {
       if (elems) delete[] elems;
       elems = NULL;
   }
   Vector(const Vector & c) {
       counter = c.size();
       arraySize = c.capacity();
       elems = NULL;
       if (arraySize) elems = new int[arraySize];
       for (int i = 0; i < counter; i++) elems[i] = c.elems[i];
   }
   const Vector& operator = (Vector & c) {
       if (elems) delete[] elems;
       elems = NULL;
       counter = c.size();
       arraySize = c.capacity();
       if (arraySize) elems = new int[arraySize];
       for (int i = 0; i < counter; i++) elems[i] = c.elems[i];
       return *this;
   }
   int size() const {
       return counter;
   }
   int capacity() const {
       return arraySize;
   }
   bool empty() const {
       return counter == 0;
   }
   void clear() {
       counter = 0;
   }
   T & operator [] (int position) {
       if (0 <= position && position < counter)
            return elems[position];
   }
   int find( T &x) const {
       for (int i = 0; i < counter; i++) {
           if (elems[i] == x) return i;
       }
       return -1;
   }
   int insert(int position, const T &x) {
       if (position < 0 || position > counter) return -1;
       if (counter + 1 > arraySize) {
           int * newElems = new int[(arraySize ? 2 * arraySize : 1)];
           for (int i = 0; i < position; i++) newElems[i] = elems[i];
           newElems[position] = x;
           for (int i = position; i < counter; i++) newElems[i + 1] = elems[i];
           counter++;
           arraySize = arraySize ? 2 * arraySize : 1;
           delete[] elems;
           elems = newElems;
       } else {
           counter++;
           for (int i = counter - 1; i > position; i--) elems[i] = elems[i - 1];
           elems[position] = x;
       }
       return position;
   }
   int push_back(const T &x) {
       return insert(counter, x);
   }
   int erase(int position) {
       if (position < 0 || position >= counter) return -1;
       for (int i = position; i < counter - 1; i++) elems[i] = elems[i + 1];
       counter--;
       return position + 1;
   }
   void traverse(void (*visit)(T &)) {
       for (int i = 0; i < counter; i++) visit(elems[i]);
   }
 private:
    T *elems;
    int counter; //number of items in the list
    int arraySize; // the size of the array 
};
typedef Vector MyVector;                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值