1、数组基础实现:
#include<iostream>
using namespace std;
class Array
{
public:
Array(size_t capacity = 10) :
_size(0),
_capacity(capacity),
_array(new int[capacity])
{}
~Array()
{
if (_array)
{
delete[] _array;
}
_array = nullptr;
}
void push_back(int val)
{
if (_size == _capacity)
{
expand(_capacity);
}
_array[_size++] = val;
}
int pop_back()
{
if (_size == 0)
{
return -1;
}
return _array[--_size];
}
void insert(size_t pos, int val)
{
if (pos > _size)
{
return;
}
if (_size == _capacity)
{
expand(_capacity);
}
for (size_t i = _size; i > pos; i--)
{
_array[i] = _array[i - 1];
}
_array[pos] = val;
_size++;
}
void erase(size_t pos)
{
if (pos >= _size) return;
for (int i = pos; i < _size - 1; i++)
{
_array[i] = _array[i + 1];
}
_size--;
}
int find(int val)
{
for (int i = 0; i < _size; i++)
{
if (_array[i] == val)
{
return i;
}
}
return -1;
}
void expand(size_t capaccity)
{
_capacity = (_capacity == 0) ? 2 : _capacity * 2;
int* tmp = new int[_capacity * 2];
memcpy(tmp, _array, sizeof(int) * _size);
delete[]_array;
_array = tmp;
}
void print()
{
for (int i = 0; i < _size; i++)
{
cout << _array[i] << " ";
}
cout << endl;
}
private:
int* _array;
size_t _size;
size_t _capacity;
};
int main()
{
Array a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);
a.pop_back();
cout << a.find(3) << endl;
a.erase(1);
a.print();
return 0;
}
2、单链表基础实现
#include<iostream>
using namespace std;
struct ListNode
{
int _val;
ListNode* _next;
ListNode(int val = -1) :_val(val), _next(nullptr)
{}
};
class List
{
public:
List()
{
_head = new ListNode();
}
~List()
{
ListNode* p = _head;
while (p != nullptr)
{
_head = _head->_next;
delete p;
p = _head;
}
}
void InsertTail(int val)
{
ListNode* p = _head;
while (p->_next != nullptr)
{
p = p->_next;
}
p->_next = new ListNode(val);
}
void InsertHead(int val)
{
ListNode* s = new ListNode(val);
s->_next = _head->_next;
_head->_next = s;
}
void Remove(int val)
{
ListNode* p = _head;
ListNode* q = _head->_next;
while (p)
{
if (q->_val == val)
{
p->_next = q->_next;
delete q;
return;
}
else
{
p = q;
q = q->_next;
}
}
}
void RemoveAll(int val)
{
ListNode* p = _head;
ListNode* q = _head->_next;
while (p != nullptr)
{
if (q != nullptr && q->_val == val)
{
p->_next = q->_next;
delete q;
if (!p->_next) return;
q = p->_next;
}
else
{
p = q;
if (q->_next)q = q->_next;
}
}
}
bool find(int val)
{
ListNode* p = _head->_next;
while (p)
{
if (p->_val == val)
{
return true;
}
p = p->_next;
}
return false;
}
void Print()
{
ListNode* p = _head->_next;
while (p != nullptr)
{
cout << p->_val << " ";
p = p->_next;
}
cout << endl;
}
//单链表逆序
void ReverseList()
{
ListNode* p = _head->_next;
ListNode* last = nullptr;
if (p->_next == nullptr)
{
return;
}
while (p != nullptr)
{
ListNode* q = p->_next;
p->_next = last;
last = p;
p = q;
}
_head->_next = last;
}
//求倒数第k个节点
int GetKNode(int n)
{
ListNode* p = _head->_next;
while (--n > 0)
{
p = p->_next;
}
if (!p)return -1;
ListNode* q = _head->_next;
while (p->_next)
{
p = p->_next;
q = q->_next;
}
return q->_val;
}
private:
ListNode* _head;
};
int main()
{
List a;
a.InsertTail(1);
a.InsertTail(2);
a.InsertTail(3);
a.InsertTail(4);
a.Print();
a.InsertHead(10);
a.Print();
a.InsertTail(2);
a.InsertTail(2);
a.InsertTail(2);
a.Remove(2);
a.Print();
a.RemoveAll(2);
a.Print();
cout << a.find(3) << endl;
a.ReverseList();
a.Print();
cout << a.GetKNode(5) << endl;
return 0;
}