竞赛选手,重写这三个比较常用的STL是因为今天遇到卡STL空间的某道题目。重写过程也算锻炼工程能力了。
使用方法同STL,顺便加了print函数输出。具体越界情况没怎么处理(都是熟手了这点就算了吧,再说加了这些复杂度就上去了不是吗?)
有空会继续更的(然而这只是一句自欺欺人的话罢了)
template <typename t> class List
{
public:
int n = 0;
struct Node
{
t data;
Node* next = nullptr;
};
Node* head = new Node, * tail = head;
t *begin()
{
return &(head->next->data);
}
t* end()
{
return &(tail->next->data);
}
int size()
{
return n;
}
void push(t num)
{
Node* node = new Node; n++;
node->data = num; tail->next = node; tail = node;
}
void insert(int ad, t num)
{
Node* ptr = head; int cnt = 0;
if (ad <= n)
while (ptr->next != nullptr)
{
ptr = ptr->next; cnt++;
if (ad == cnt)
{
Node* node1 = new Node;
node1->data = num; node1->next = ptr->next; ptr->next = node1; n++;
return;
}
}
else push(num);
}
void erase(int ad)
{
Node* ptr = head, * ptr1; int cnt = 0;
if (ad <= n)
while (ptr->next != nullptr)
{
ptr1 = ptr; ptr = ptr->next; cnt++;
if (ad == cnt)
{
ptr1->next = ptr->next;
delete(ptr); n--;
return;
}
}
}
void change(int ad, t num)
{
Node* ptr = head; int cnt = 0;
if (ad <= n)
while (ptr->next != nullptr)
{
ptr = ptr->next; cnt++;
if (ad == cnt)
{
ptr->data = num;
return;
}
}
}
void check(int ad)
{
Node* ptr = head; int cnt = 0;
if (ad <= n)
while (ptr->next != nullptr)
{
ptr = ptr->next; cnt++;
if (ad == cnt)
{
if (typeid(ptr->data) == typeid(int)) { printf("%d\n",ptr->data); }
else if (typeid(ptr->data) == typeid(long long)) { printf("%lld\n",ptr->data); }
else { printf("%lf\n",ptr->data); }
return;
}
}
}
void print()
{
Node* ptr = head;
while (ptr->next != nullptr)
{
ptr = ptr->next;
if (typeid(ptr->data) == typeid(int)) { printf("%d\n",ptr->data); }
else if (typeid(ptr->data) == typeid(long long)) { printf("%lld\n",ptr->data); }
else { printf("%lf\n",ptr->data); }
}
puts("");
}
};
template <typename t> class Quene
{
public:
int n = 0;
struct Node
{
t data;
Node* next = nullptr;
};
Node* head = new Node, * tail = head;
t* begin()
{
return &(head->next->data);
}
t* end()
{
return &(tail->next->data);
}
int size()
{
return n;
}
void push(t num)
{
Node* ptr = new Node; n++;
ptr->data = num; tail->next = ptr; tail = ptr;
}
t front()
{
return head->next->data;
}
void pop()
{
Node* ptr = head->next; n--;
delete head; head = ptr;
}
void print()
{
Node* ptr = head;
while (ptr->next != nullptr)
{
ptr = ptr->next;
if (typeid(ptr->data) == typeid(int)) { printf("%d\n",ptr->data); }
else if (typeid(ptr->data) == typeid(long long)) { printf("%lld\n",ptr->data); }
else { printf("%lf\n",ptr->data); }
}
puts("");
}
};
};
template <typename t> class Stack
{
public:
int n = 0;
struct Node
{
t data;
Node* next = nullptr;
};
Node* head = new Node, * tail = head;
t* begin()
{
return &(head->next->data);
}
t* end()
{
return &(tail->next->data);
}
int size()
{
return n;
}
void push(int num)
{
Node* ptr = new Node; n++;
ptr->data = num, ptr->next = head->next; head->next = ptr;
if (n == 1) tail = ptr;
}
int top()
{
return head->next->data;
}
void pop()
{
Node* ptr = head->next; n--;
delete head; head = ptr;
}
void print()
{
Node* ptr = head;
while (ptr->next != nullptr)
{
ptr = ptr->next;
if (typeid(ptr->data) == typeid(int)) { printf("%d\n",ptr->data); }
else if (typeid(ptr->data) == typeid(long long)) { printf("%lld\n",ptr->data); }
else { printf("%lf\n",ptr->data); }
}
puts("");
}
};
};
后记:测试(Test1为STL的list,Test2为改写的)
选了list存入1e6的数据,差距还是十分明显的
int main()
{
clock_t start1 = clock();
list <int> l1;
for (int i = 0; i < 1e6; i++) l1.push_back(i);
clock_t end1 = clock();
cout <<"Test1:" <<end1 - start1 << " ms" << endl;
clock_t start2 = clock();
List <int> l2;
for (int i = 0; i < 1e6; i++) l2.push(i);
clock_t end2 = clock();
cout << "Test2:" << end2 - start2 << " ms" << endl;
return 0;
}