#include <iostream>
using namespace std;
struct List{
int value;
struct List* next;
struct List* last;
};//定义一个链表结构
int main()
{
int n;
cin>>n;
List* head = new List();//定义链表的头部
head->value=0;
head->next=NULL;
head->last=NULL;
List* t=head;//定义临时节点t
for(int i=0;i<n;i++)
{
List* p = new List;
cin>>p->value;
p->next=NULL;
p->last=t;
t->next=p;
t=p;
}//每次循环创造一个新的节点,并获取value。
List* end=new List;//定义尾部节点
end->last=t;
end->next=NULL;
end->value=0;
t->last=end;
t=head->next;
while(t->next!=NULL)//遍历链表中的value
{
List* temp = new List;//定义一个节点,使其指向t节点的下一个
temp = t->next;
while(temp->next!=NULL)//遍历temp节点,使t节点右边的value全大于t节点的value
{
if(temp->value<t->value)//如果temp节点的value小于于t节点的value,则删除temp节点
{
temp->next->last=temp->last;
temp->last->next=temp->next;
}
temp =temp->next;
}
t = t->next;
}
t=head->next;
while(t!=NULL)//遍历输出链表,检验程序至此的正确性
{
cout<<t->value<<" ";
t=t->next;
}
t=end;
while(t->last!=NULL)//再来一边,使t节点左边的值全为小于t节点value的值
{
List* temp = new List;
temp = t->last;//定义temp节点,使其指向t节点的上一个(last)
while(temp->last!=NULL)
{
if(temp->value>t->value)//若temp的value大于t的value,则删除temp节点
{
temp->last->next=temp->next;
temp->next->last=temp->last;
}
temp =temp->last;
}
t = t->last;
}
t=head;
while(t->next!=NULL)//遍历输出链表
{
cout<<t->value;
t=t->next;
}
}
感觉应该是下标有问题,请指教