#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
typedef struct Node
{
int data;
Node *next;
}LNode,*Linkedlist;
Linkedlist LinkedlistInit()
{
Linkedlist L;
L=(LNode*)malloc(sizeof(LNode));
if(L==NULL)
{
cout<<"没有足够的空间"<<endl;
}
L->next=NULL;
return L;
}
Linkedlist Create()
{
Linkedlist L,p;
int x,i,j;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
cin>>i;
for(j=0;j<i;j++)
{
cin>>x;
p=(LNode*)malloc(sizeof(LNode));
p->data=x;
p->next=L->next;
L->next=p;
}
return L;
}
Linkedlist TailCreate()
{
Linkedlist L,r,p;
int x,i,j;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;r=L;
cin>>i;
for(j=0;j<i;j++)
{
cin>>x;
p=(LNode*)malloc(sizeof(LNode));
p->data=x;
r->next=p;
r=p;
}
r->next=NULL;
return L;
}
Linkedlist rerverse(Linkedlist L)
{
Linkedlist p,q;
p=L->next;
L->next=NULL;
while(p)
{
q=p->next;
p->next=L->next;
L->next=p;
p=q;
}
return L;
}
Linkedlist delsame(Linkedlist L)
{
Linkedlist p,q,temp;
p=L->next;
q=p->next;
while(q!=NULL)
{
if(p->data==q->data)
{
temp=q;
q=q->next;
free(temp);
}
else
{
p->next=q;
p=q;
q=q->next;
}
// p->next=q;
}
return L;
}
Linkedlist merge(Linkedlist L1,Linkedlist L2)
{
Linkedlist L,p,q,r;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;r=L;
p=L1->next;q=L2->next;
while(p && q)
{
if(p->data<=q->data)
{
r->next=p;
r=p;
p=p->next;
}
else if(p->data>q->data)
{
r->next=q;
r=q;
q=q->next;
}
}
if(p)
{
r->next=p;
}
else
r->next=q;
return L;
}
void listsort(Linkedlist L)//sort delete 前驱
{
Linkedlist p,q,temp;
while(L->next)
{
p=L;
q=p->next;
// r=q->next;
while(q->next)
{
if(q->next->data<p->next->data)
{
p=q;
q=q->next;
}
else
{
q=q->next;
}
}
cout<<p->next->data<<" ";
temp=p->next;
p->next=temp->next;
free(temp);
}
free(L);
}
void output(Linkedlist L)
{
LNode* p;
p=L->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
Linkedlist L1,L2,LR;
L1=LinkedlistInit();
// L2=LinkedlistInit();
L1=TailCreate();
// L2=TailCreate();
// LR=rerverse(L);
// LR=delsame(L);
// LR=merge(L1,L2);
listsort(L1);
// output(LR);
system("pause");
return 0;
}