如何减少代码的运行时间

#include
using namespace std;
typedef struct node
{
int data;
struct node* next;
}LISTNODE;
LISTNODE* creatlist(int n)
{
LISTNODE* head = new LISTNODE;
LISTNODE* pre = head;
for (int i = 0; i < n; i++)
{
LISTNODE* p = new LISTNODE;
cin >> p->data;
pre->next = p;
pre = p;
}
pre->next = NULL;
return head;
}
int main(void)
{
unsigned n = 0, m = 0, i = 0, k = 0;
int d = 0;
cin >> n;
if (n > 100000) return 0;
LISTNODE* head = creatlist(n);
cin >> m;
if (m > 100000) return 0;
for (int a = 0; a < m; a++)
{
cin >> i >> k;
if (i == 0)
{
cin >> d;
if (kn)
continue;
LISTNODE* pre = head;
for (int i = 0; i < k; i++)
pre = pre->next;
LISTNODE* newnode = new LISTNODE;
newnode->data = d;
newnode->next = pre->next;
pre->next = newnode;
n++;
}
if (i == 1)
{
if (n == 0 or k <= 0 or k > n)
continue;
LISTNODE* p = head;
for (int i = 1; i < k; i++)
p = p->next;
p->next = p->next->next;
n--;
}
}
LISTNODE* p = head->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
return 0;
}


这段代码不会太慢,想快点发布release版。如果还想更快点,可以加入内存池,减少节点内存申请的时间耗费。

https://blog.csdn.net/weixin_44910057/article/details/105256011