cpp-algo-cases/chapter_array_and_linkedlist/my_list.cpp

172 lines
4.0 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: my_list.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20>б<EFBFBD><D0B1><EFBFBD> */
class MyList {
private:
int *arr; // <20><><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>
int arrCapacity = 10; // <20>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>
int arrSize = 0; // <20>б<EFBFBD><D0B1><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>ǰԪ<C7B0><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int extendRatio = 2; // ÿ<><C3BF><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD>ݵı<DDB5><C4B1><EFBFBD>
public:
/* <20><><EFBFBD><EFBFBD><ECB7BD> */
MyList() {
arr = new int[arrCapacity];
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
~MyList() {
delete[] arr;
}
/* <20><>ȡ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>ǰԪ<C7B0><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
int size() {
return arrSize;
}
/* <20><>ȡ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD> */
int capacity() {
return arrCapacity;
}
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
int get(int index) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD><E7A3AC><EFBFBD>׳<EFBFBD><D7B3><EFBFBD><ECB3A3><EFBFBD><EFBFBD>ͬ
if (index < 0 || index >= size())
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>");
return arr[index];
}
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
void set(int index, int num) {
if (index < 0 || index >= size())
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>");
arr[index] = num;
}
/* <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
void add(int num) {
// Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD>
if (size() == capacity())
extendCapacity();
arr[size()] = num;
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
arrSize++;
}
/* <20><><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
void insert(int index, int num) {
if (index < 0 || index >= size())
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>");
// Ԫ<><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD>
if (size() == capacity())
extendCapacity();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> index <20>Լ<EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>Ԫ<EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>һλ
for (int j = size() - 1; j >= index; j--) {
arr[j + 1] = arr[j];
}
arr[index] = num;
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
arrSize++;
}
/* ɾ<><C9BE>Ԫ<EFBFBD><D4AA> */
int remove(int index) {
if (index < 0 || index >= size())
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>");
int num = arr[index];
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> index ֮<><D6AE><EFBFBD><EFBFBD>Ԫ<EFBFBD>ض<EFBFBD><D8B6><EFBFBD>ǰ<EFBFBD>ƶ<EFBFBD>һλ
for (int j = index; j < size() - 1; j++) {
arr[j] = arr[j + 1];
}
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
arrSize--;
// <20><><EFBFBD>ر<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
return num;
}
/* <20>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD> */
void extendCapacity() {
// <20>½<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊԭ<CEAA><D4AD><EFBFBD><EFBFBD> extendRatio <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int newCapacity = capacity() * extendRatio;
int *tmp = arr;
arr = new int[newCapacity];
// <20><>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < size(); i++) {
arr[i] = tmp[i];
}
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
delete[] tmp;
arrCapacity = newCapacity;
}
/* <20><><EFBFBD>б<EFBFBD>ת<EFBFBD><D7AA>Ϊ Vector <20><><EFBFBD>ڴ<EFBFBD>ӡ */
vector<int> toVector() {
// <20><>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD>ȷ<EFBFBD>Χ<EFBFBD>ڵ<EFBFBD><DAB5>б<EFBFBD>Ԫ<EFBFBD><D4AA>
vector<int> vec(size());
for (int i = 0; i < size(); i++) {
vec[i] = arr[i];
}
return vec;
}
};
/* Driver Code */
int main() {
/* <20><>ʼ<EFBFBD><CABC><EFBFBD>б<EFBFBD> */
MyList *nums = new MyList();
/* <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
nums->add(1);
nums->add(3);
nums->add(2);
nums->add(5);
nums->add(4);
cout << "<EFBFBD>б<EFBFBD> nums = ";
vector<int> vec = nums->toVector();
printVector(vec);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << nums->capacity() << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << nums->size() << endl;
/* <20><><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
nums->insert(3, 6);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6 <20><><EFBFBD>õ<EFBFBD> nums = ";
vec = nums->toVector();
printVector(vec);
/* ɾ<><C9BE>Ԫ<EFBFBD><D4AA> */
nums->remove(3);
cout << "ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3>õ<EFBFBD> nums = ";
vec = nums->toVector();
printVector(vec);
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
int num = nums->get(1);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3>õ<EFBFBD> num = " << num << endl;
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
nums->set(1, 0);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>Ϊ 0 <20><><EFBFBD>õ<EFBFBD> nums = ";
vec = nums->toVector();
printVector(vec);
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD> */
for (int i = 0; i < 10; i++) {
// <20><> i = 5 ʱ<><CAB1><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD>
nums->add(i);
}
cout << "<EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD> nums = ";
vec = nums->toVector();
printVector(vec);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << nums->capacity() << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << nums->size() << endl;
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
delete nums;
return 0;
}