这里是11.4随笔。
今日代码留档:实现单链表插入、删除结点等基本算法。语言:c++
include<stdio.h>
include<stdlib.h>
struct node {
int data;
nodenext;
};
int main()
{
int n,m, c, a, b;
scanf("%d", &n);
node head, * pre, * p;
head = new node;
head->next = NULL;
pre = head;
for(int i=0;i<n;i++)
{
p = new node;
scanf("%d", &p->data);
p->next = NULL;
pre->next = p;
pre = p;
}
scanf("%d", &m);
for(int i=0;i<m;i++)
{
scanf("%d", &c);
if (c == 0)
{
scanf("%d%d", &a, &b);
if (a == 0) {
node* l;
l = new node;
l->data = b;
l->next = head->next;
head->next = l; n++;
}
else if(a<=n)
{
node* pre= head,p;
for (int j = 0; j < a;j++)
pre = pre->next;
p = new node;
p->data = b;
p->next = pre->next;
pre->next = p; n++;
}
}
if (c == 1)
{
scanf("%d", &a);
if (a != 0 && a <= n)
{
node pre;
pre = head;
for(int k=1;k<a;k++)
pre = pre->next;
pre->next = pre->next->next;
}
}
}
node* l = head; l = l->next;
while (l != NULL)
{
printf("%d ", l->data);
l = l->next;
}
return 0;
}