#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *create(int n)
{
node *head=NULL;
node *p=NULL;
head=new node();
p=head;
cin>>p->data;
node *q;
while(--n)
{
q=new node();
cin>>q->data;
p->next=q;
p=q;
}
p->next=NULL;
return head;
}
void print(node *head)
{
node *p=head;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
node* insert(node *head,int weizhi,int n)
{
node *p=NULL;
if(weizhi<0)
{
return 0;
}
if(weizhi==0)
{
p=new node();
p->data=n;
p->next=head;
head=p;
}
if(weizhi>0)
{
p=head;
int num=weizhi;
while(--num)
{
p=p->next;
}
node *t=p->next;
node *q=new node();
q->data=n;
p->next=q;
q->next=t;
}
return head;
}
node *delete_node(node *head,int n)
{
if (head==NULL)
{
return NULL;
}
else if(head->data==n)
{
node *p=head;
head=head->next;
delete p;
return head;
}
else
{
node *p,*q;
p=head;
q=p->next;
while(q!=NULL && q->data!=n)
{
p=p->next;
q=q->next;
}
if(q==NULL)
{
return head;
}
else
{
p->next=q->next;
delete q;
return head;
}
}
}
node *reverse(node *head)
{
node *p=NULL;
node *r=head;
node *q=NULL;
while(r!=NULL)
{
q=r->next;
r->next=p;
p=r;
r=q;
}
head=p;
return head;
}
int main()
{
node *head=create(5);
print(head);
head=insert(head,2,100);
print(head);
head=insert(head,0,1000);
print(head);
head=delete_node(head,3);
print(head);
head=delete_node(head,5);
print(head);
head=delete_node(head,1000);
print(head);
head=reverse(head);
print(head);
return 0;
}