cpp-algo-cases/chapter_array_and_linkedlist/my_list.cpp

190 lines
4.4 KiB
C++
Raw Normal View History

2024-02-21 09:31:34 +08:00
/**
* File: my_list.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
2024-02-22 14:56:29 +08:00
/* <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()
{
2024-02-21 09:31:34 +08:00
arr = new int[arrCapacity];
}
2024-02-22 14:56:29 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
~MyList()
{
2024-02-21 09:31:34 +08:00
delete[] arr;
}
2024-02-22 14:56:29 +08:00
/* <20><>ȡ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>ǰԪ<C7B0><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
int size()
{
2024-02-21 09:31:34 +08:00
return arrSize;
}
2024-02-22 14:56:29 +08:00
/* <20><>ȡ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD> */
int capacity()
{
2024-02-21 09:31:34 +08:00
return arrCapacity;
}
2024-02-22 14:56:29 +08:00
/* <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>ͬ
2024-02-21 09:31:34 +08:00
if (index < 0 || index >= size())
2024-02-22 14:56:29 +08:00
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>"); // out_of_range <20><> C++ <20><>׼<EFBFBD><D7BC><EFBFBD>е<EFBFBD><D0B5><EFBFBD><ECB3A3>
2024-02-21 09:31:34 +08:00
return arr[index];
}
2024-02-22 14:56:29 +08:00
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
void set(int index, int num)
{
2024-02-21 09:31:34 +08:00
if (index < 0 || index >= size())
2024-02-22 14:56:29 +08:00
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>");
2024-02-21 09:31:34 +08:00
arr[index] = num;
}
2024-02-22 14:56:29 +08:00
/* <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>
2024-02-21 09:31:34 +08:00
if (size() == capacity())
extendCapacity();
arr[size()] = num;
2024-02-22 14:56:29 +08:00
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
arrSize++;
}
2024-02-22 14:56:29 +08:00
/* <20><><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
void insert(int index, int num)
{
2024-02-21 09:31:34 +08:00
if (index < 0 || index >= size())
2024-02-22 14:56:29 +08:00
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>
2024-02-21 09:31:34 +08:00
if (size() == capacity())
extendCapacity();
2024-02-22 14:56:29 +08:00
// <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--)
{
2024-02-21 09:31:34 +08:00
arr[j + 1] = arr[j];
}
arr[index] = num;
2024-02-22 14:56:29 +08:00
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
arrSize++;
}
2024-02-22 14:56:29 +08:00
/* ɾ<><C9BE>Ԫ<EFBFBD><D4AA> */
int remove(int index)
{
2024-02-21 09:31:34 +08:00
if (index < 0 || index >= size())
2024-02-22 14:56:29 +08:00
throw out_of_range("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>");
2024-02-21 09:31:34 +08:00
int num = arr[index];
2024-02-22 14:56:29 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> index ֮<><D6AE><EFBFBD><EFBFBD>Ԫ<EFBFBD>ض<EFBFBD><D8B6><EFBFBD>ǰ<EFBFBD>ƶ<EFBFBD>һλ
for (int j = index; j < size() - 1; j++)
{
2024-02-21 09:31:34 +08:00
arr[j] = arr[j + 1];
}
2024-02-22 14:56:29 +08:00
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
arrSize--;
2024-02-22 14:56:29 +08:00
// <20><><EFBFBD>ر<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
return num; // <20><><EFBFBD>ر<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>(<28><>֪<EFBFBD><D6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>ʲô)
2024-02-21 09:31:34 +08:00
}
2024-02-22 14:56:29 +08:00
/* <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; // <20><><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> extendRatio <20><>
int *tmp = arr; // <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
arr = new int[newCapacity]; // <20>½<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ newCapacity <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <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++)
{
2024-02-21 09:31:34 +08:00
arr[i] = tmp[i];
}
2024-02-22 14:56:29 +08:00
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
delete[] tmp; // <20>ͷ<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
arrCapacity = newCapacity; // <20><><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
}
2024-02-22 14:56:29 +08:00
/* <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()); // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ size() <20><> vector
for (int i = 0; i < size(); i++)
{
2024-02-21 09:31:34 +08:00
vec[i] = arr[i];
}
return vec;
}
};
/* Driver Code */
2024-02-22 14:56:29 +08:00
int main()
{
/* <20><>ʼ<EFBFBD><CABC><EFBFBD>б<EFBFBD> */
2024-02-21 09:31:34 +08:00
MyList *nums = new MyList();
2024-02-22 14:56:29 +08:00
/* <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
nums->add(1);
nums->add(3);
nums->add(2);
nums->add(5);
nums->add(4);
2024-02-22 14:56:29 +08:00
cout << "<EFBFBD>б<EFBFBD> nums = ";
2024-02-21 09:31:34 +08:00
vector<int> vec = nums->toVector();
printVector(vec);
2024-02-22 14:56:29 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << nums->capacity() << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << nums->size() << endl;
2024-02-21 09:31:34 +08:00
2024-02-22 14:56:29 +08:00
/* <20><><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
nums->insert(3, 6);
2024-02-22 14:56:29 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6 <20><><EFBFBD>õ<EFBFBD> nums = ";
2024-02-21 09:31:34 +08:00
vec = nums->toVector();
printVector(vec);
2024-02-22 14:56:29 +08:00
/* ɾ<><C9BE>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
nums->remove(3);
2024-02-22 14:56:29 +08:00
cout << "ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3>õ<EFBFBD> nums = ";
2024-02-21 09:31:34 +08:00
vec = nums->toVector();
printVector(vec);
2024-02-22 14:56:29 +08:00
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
int num = nums->get(1);
2024-02-22 14:56:29 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3>õ<EFBFBD> num = " << num << endl;
2024-02-21 09:31:34 +08:00
2024-02-22 14:56:29 +08:00
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
nums->set(1, 0);
2024-02-22 14:56:29 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>Ϊ 0 <20><><EFBFBD>õ<EFBFBD> nums = ";
2024-02-21 09:31:34 +08:00
vec = nums->toVector();
printVector(vec);
2024-02-22 14:56:29 +08:00
/* <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>
2024-02-21 09:31:34 +08:00
nums->add(i);
}
2024-02-22 14:56:29 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD> nums = ";
2024-02-21 09:31:34 +08:00
vec = nums->toVector();
printVector(vec);
2024-02-22 14:56:29 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << nums->capacity() << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = " << nums->size() << endl;
2024-02-21 09:31:34 +08:00
2024-02-22 14:56:29 +08:00
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
2024-02-21 09:31:34 +08:00
delete nums;
return 0;
}