在C ++数组中移位元素

时间:2016-03-09 03:18:22

标签: c++ arrays algorithm sorting

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

ArrayList.cpp

$x = .\GetDataFromDatabase.ps1
echo $x

testArrayList.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;
}

ArrayList.h

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

这是输出应该是什么样子: CORRECT OUTPUT

以下是我的输出结果: MY OUTPUT

新功能

我正在尝试反向复制#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_ */ 的前35个值,将它们分配给array,然后将反转的顺序分配回数组。以下代码无法正常工作。

reverseArray

}

1 个答案:

答案 0 :(得分:4)

  1. 初始化指向数组末尾的指针。
  2. foreach每个新元素,将其设置为当前指针指向的值,并将当前指针减少1。
  3. 如果使用void return函数,则可以将新元素值和当前指针作为参数传递。并注意检查当前指针。