#include <iostream>
using namespace std;
struct node{
int val;
node * next;
};
void print(node * head){
if(head==NULL ||head->next==NULL){
cout<<"链表中已经无元素";
return;
}
cout<<"打印列表:";
node * p=head->next;
while(p){
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}
node * insert(node * head,int n){
node * cur=new node;
cur->val=n;
node *pre=head;
if(head->next==NULL){
head->next=cur;
cur->next=NULL;
// print(head);
return head;
}
node *p=head->next;
while(p->val<n){
pre=p;
p=p->next;
if(p==NULL) break;
}
pre->next=cur;
cur->next=p;
return head;
}
void printop(){
cout<<"请输入操作符和数字,i表示插入,d表示删除,后面接着插入或者删除的数字,用空格分隔"<<endl;
}
node * deletenode(node * head,int n){
node * p=head;
node * q=head->next;
while(q->val!=n){
p=q;
q=q->next;
if(q==NULL) return NULL;
}
p->next=q->next;
return head;
}
int main(){
int n;
char op;
node * head=new node;
head->next=NULL;
printop();
while(cin>>op>>n){
if(op=='i'){
head=insert(head,n);
}
else if(op='d'){
if(head==NULL || head->next==NULL){
cout<<"链表已经没有数字可以删除了";
return 0;
}
else{
node * res=deletenode(head,n);
if(res==NULL) {
cout<<"没有找到数据";
printop();
continue;
}
else head=res;
}
}
else{
cout<<"输入错误";
}
print(head);
printop();
}
return 0;
}