在C++数组中移动元素

Shifting elements in C++ Array

本文关键字:移动 元素 数组 C++      更新时间:2023-10-16

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

数组列表.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;
}

testArrayList.cpp

#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;
}

ArrayList.h

#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_ */

输出应如下所示:正确的输出

这是我的输出的样子:我的输出

新功能

我正在尝试反向复制array的前 35 个值,将它们分配给reverseArray,然后将相反的顺序分配回数组。下面的代码无法正常工作,无法达到我的预期。

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];
    }
}

}

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

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