C++ C+中的移位元件+;大堆

C++ C+中的移位元件+;大堆,c++,arrays,algorithm,sorting,C++,Arrays,Algorithm,Sorting,我有一个包含35个插槽的整数数组。我想插入一个值,每次插入一个新值时,我希望第一个值保持尾部,新值成为头部。我不能使用链表或队列,我必须使用void函数。我想不出一个算法,但我想到的每件事都包含一个for循环,我只是不知道如何正确地实现它 ArrayList.cpp #include <iostream> #include "ArrayList.h" using namespace std; ArrayList::ArrayList() { capacity = 8;

我有一个包含35个插槽的整数数组。我想插入一个值,每次插入一个新值时,我希望第一个值保持尾部,新值成为头部。我不能使用链表或队列,我必须使用void函数。我想不出一个算法,但我想到的每件事都包含一个for循环,我只是不知道如何正确地实现它

ArrayList.cpp

#include <iostream>
#include "ArrayList.h"
using namespace std;

ArrayList::ArrayList() {
    capacity = 8;
    length = 0;
    array = new int[capacity];
}

ArrayList::ArrayList(const ArrayList& other) {
    length = other.length;
    capacity = other.capacity;
    array = new int[other.capacity];
    for (int i = 0; i <= capacity; i++)
        array[i] = other.array[i];
}

void ArrayList::add(int item) {
    if (length <= capacity) {
        changeCapacityTo(2 * capacity);
    }
    length++;
    array[length++] = item;
}

void ArrayList::add(int index, int item) {
    while (index > capacity || length == capacity) {
        capacity *= 2;
    }

    if (length != 0 && length < index) {
        length = index;
    }

    int temp;
    int i;
    for (i = 0; i <= length; i++) {
        array[index] = item;
        temp = array[index];
        array[index + 1] = temp;
    }
    length++;
}

int ArrayList::get(int index) const {
    return array[index];
}

void ArrayList::changeCapacityTo(int newCapacity) {
    int *newArray = new int[newCapacity];
    int numItemsToCopy = length < newCapacity ? length : newCapacity;
    for (int i = 0; i < numItemsToCopy; i++)
    newArray[i] = array[i];
    delete[] array;
    array = newArray;
}
#include <iostream>
#include "ArrayList.h"
using namespace std;

void verifyArrayList(ArrayList arrayList) {
   for(int i = 0; i < 1000; ++i) {
      int item;
      int itemToAdd = 2 * i;
      if((item = arrayList.get(i)) != itemToAdd)
         cout << "OOPS - Error at index " << i << ": " << item << " should be "
                     << itemToAdd << endl;
   }

}
void printArrayList(ArrayList arrayList) {
   for(int i = 0; i < arrayList.getLength(); ++i) {
      int item = arrayList.get(i);
      cout << i << ":" << item << endl;
   }
}
int main(int argc, char *argv[]) {
   ArrayList arrayList;

   const int highIndex = 35;
   for(int i = highIndex; i > 0; --i) {
      int itemToAdd = 2 * i;
      arrayList.add(1, itemToAdd);

      //cout << "VALUE OF itemToAdd: " << itemToAdd << endl;
      //cout << endl;
   }
   arrayList.add(99);
   printArrayList(arrayList);
   cout << "#items = " << arrayList.getLength() << endl;
   cout << "capacity = " << arrayList.getCapacity() << endl;

   arrayList.add(2000, 9999);
   cout << "#items = " << arrayList.getLength() << endl;
   cout << "capacity = " << arrayList.getCapacity() << endl;
}
}

  • 初始化指向数组结尾的指针
  • 对于每个新元素,将其设置为当前指针指向的值,并将当前指针减少1

  • 如果使用void返回函数,则可以传递新元素值和当前指针作为参数。并注意检查当前指针。

    是否可以使用std::swap?在数组中的何处插入一个值?在引用数组时,您所说的头和尾是什么意思?请编辑您的问题,并包括一个包含您迄今为止编码的内容的列表,以及一些示例输入、实际结果和预期结果。查找以下函数:
    std::copy
    memmove
    。这些函数将帮助您“移动”数组中的元素。
    #ifndef ARRAYLIST_H_
    #define ARRAYLIST_H_
    #include <iostream>
    
    class ArrayList {
    public:
       /*
        * Initialize list with a capacity of 8
        */
       ArrayList();
    
       /*
        * Copy constructor
        */
       ArrayList(const ArrayList& other);
       virtual ~ArrayList() {
          std::cout << "Destructing ArrayList at " << array << std::endl;
          delete [] array;
          array = NULL;
       }
    
       /*
        * Add item to end of list
        * @param item item to add to list
        */
       void add(int item);
    
       /*
        * Adds item to list, at index, shifting items as necessary and increasing
        * capacity of list as necessary. If capacity must increase, it must always
        * be a power of 2. Note that if index is beyond capacity, capacity must be
        * increased to allow adding the item at that index. Also, length should
        * reflect the HIGHEST index (plus one, naturally) at which an item is
        * stored, even if lower-indexed slots contain undefined values.
        *
        * @param item item to add to list
        */
       void add(int index, int item);
    
       /*
        * Return item at index. For now, we assume index is legal.
        * Later we will throw an exception when index is illegal.
        * @param index index of item to return
        * @return item at index
        */
       int get(int index) const;
    
       /*
        * Return capacity
        * @return capacity
        */
       int getCapacity() const {
          return capacity;
       }
    
       /*
        * Return current length
        * @return current length
        */
       int getLength() const {
          return length;
       }
    
    private:
       int *array;
       int length;
       int capacity;
       /*
        * Change capacity to that specified by newCapacity.
        * @param newCapacity the new capacity
        */
       void changeCapacityTo(int newCapacity);
    };
    
    #endif /* ARRAYLIST_H_ */
    
    void ArrayList::add(int index, int item) {
    while (index > capacity || length == capacity) {
        capacity *= 2;
    }
    
    if (length != 0 && length < index) {
        length = index;
    }
    
    int temp;
    int i;
    for (i = 0; i <= length; i++) {
        array[index] = item;
        temp = array[index];
        array[index + 1] = temp;
    
    }
    length++;
    if(length > 35) {
        int reverseArray[35];
        reverse_copy(array, array + 35, reverseArray);
        for(int i = 0; i <= 35; i++) {
            array[i] = reverseArray[i];
        }
    }