#include<iostream>
using namespace std;
typedef struct list
{
int data;
list* prior;
list* next;
}list,*linklist;
void Createlist(linklist& l,int n)//创建(不带头节点)双向链表
{
l = new list;
l->prior = NULL;
l->next = NULL;
linklist p,q;
cin >> l->data;
q = l;
for (int i = 1; i < n; i++)
{
p = new list;
cin >> p->data;
q->next = p;
p->prior = q;
q = p;
}
q->next = l;
l->prior = q;
}
void Insertlist(linklist& l, int n, int e)//在第i个位置插入e
{
linklist p,s;
s = new list;
s->data = e;
p = l;
for (int i = 1; i < n; i++)
{
p = p->next;
}
s->prior = p->prior;
p->prior->next = s;
s->next = p;
p->prior = s;
}
void Deletelist(linklist& l, int n)//删除第i个元素
{
linklist p,q;
p = l;
for (int i = 1; i < n; i++)
{
p = p->next;
}
p->prior->next = p->next;
p->next->prior = p->prior;
}
void Printlist(linklist& l,int &n)//输出两遍链表
{
cout << "循环输出两遍链表" << endl;
linklist p;
p = l;
for (int i = 1; i <= 2*n; i++)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main()
{
linklist l;
cout << "链表长度:" << endl;
int n;
cin >> n;
Createlist(l,n);
Printlist(l,n);
cout << "在第i个元素插入e(分别输入i,e):" << endl;
int i, e;
cin >> i >> e;
Insertlist(l, i, e);
n++;
Printlist(l,n);
cout << "删除第i个元素:" << endl;
cin >> i;
Deletelist(l, i);
n--;
Printlist(l,n);
}