#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct DLnode
{
int data;
struct DLnode *left;
struct DLnode *right;
}DLnode;
DLnode* createDLinkList() //构建双向链表,头插法 逆序的
{
int num;
DLnode *head=(DLnode *)malloc(sizeof(DLnode));
head->right=head->left=NULL;
cout<<"您构建的双向链表的节点个数是:";
cin>>num;
cout<<"输入您的节点数据:";
for(int i=0;i<num;i++)
{
DLnode *p=(DLnode *)malloc(sizeof(DLnode));
p->right=head->right; //双联表的插入,4个步骤,先连接,最后断开
p->left=head;
head->right->left=p;
head->right=p;
cin>>p->data;
}
return head;
}
void getAndRemove(DLnode *head)
{
DLnode *p=head->right;
DLnode *r=p->right;
while(r!=NULL)
{
if(r->data>p->data)
{
p=r;
}
r=r->right;
}
int temp=p->data;
p->data=head->right->data;
head->right->data=temp;
}
void printDL(DLnode *h)
{
DLnode *p=NULL;
p=h->right;
while(p!=NULL)
{
cout<<p->data<<"\t";
p=p->right;
}
}
int main()
{
DLnode *head= createDLinkList() ;
cout<<"原来的双向链表:\n";
printDL(head);
cout<<"\n新的双向链表:\n";
getAndRemove(head);
printDL(head);
return 0;
}