#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
struct node
{
int data;
node *pre;
node *next;
};
void outputList(node *);
void findData(int, node*);
node * insertData(int , node *);
node *deleteData(int , node*);
int main()
{
int n;
int num;
node *listHead=NULL;
node *listTail=NULL;
cin>>n;
for(int i=0; i < n; i++)
{
cin >> num;
node *newNode=(node*)malloc(sizeof(node));
if(i==0)
{
listHead=newNode;
listHead->data=num;
listHead->pre=NULL;
listHead->next=NULL;
listTail=listHead;
}
else
{
newNode->data=num;
newNode->next=NULL;
newNode->pre=listTail;
listTail->next=newNode;
listTail=newNode;
}
}
outputList(listHead);
cout<<"输入的数字";
cin>>num;
// findData(num,listHead);
// listHead=deleteData(num,listHead);
listHead=insertData(num,listHead);
outputList(listHead);
return 0;
}
void outputList(node *head)
{
node*curNode=head;
while(curNode)
{
cout<<curNode->data<<" ";
curNode=curNode->next;
}
}
void findData(int n, node* head)
{
node* curNode=head;
while(curNode)
{
if(curNode->data==n)
cout<<"find it!";
curNode=curNode->next;
}
}
node * insertData(int n, node *head)
{
node* curNode=head;
node* preNode=NULL;
node* newNode=NULL;
while((curNode != NULL) && (curNode->data<n))
{
preNode=curNode;
curNode=curNode->next;
}
newNode = new node;
newNode->data=n;
if(preNode==NULL)
{
newNode->next=curNode;
newNode->pre=NULL;
if(curNode != NULL)
curNode->pre = newNode;
return newNode;
}
if(curNode==NULL)
{
newNode->pre=preNode;
preNode->next=newNode;
newNode->next=NULL;
return head;
}
else
{
preNode->next=newNode;
newNode->pre=preNode;
curNode->pre=newNode;
newNode->next=curNode;
return head;
}
}
node *deleteData(int n, node* head)
{
node* curNode = head;
while(curNode)
{
if(curNode->data==n)
{
if(curNode->pre == NULL)
{
head=head->next;
head->pre=NULL;
}
else
{
curNode->pre->next=curNode->next;
if(curNode->next != NULL)
curNode->next->pre=curNode->pre;
}
//cout<<"delete"<<n;
return head;
}
curNode=curNode->next;
}
return head;
}