单链表的建立,插入排序,去重

ABCADACD  CACAC

#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=100005;

typedef struct Node{
    int data;
    struct Node *next;
}Node,*LinkList;

 void Print(LinkList L){   //打印链表
    while(L->next){
        L=L->next;
        cout<data<<" ";
    }
    cout<next){
        Node *p=cur->next;
        while(p->next&&p->next->data==cur->next->data){
            cur->next->next=p->next->next;
            free(p->next);
        }
        cur=cur->next;
    }
}

void Sort(LinkList & L){     //对于已经排好序的去重
    Node *cur=L;
    while(cur->next){
        Node *midx=cur,*p=cur->next,x;
        //cout<next){
            if(p->next->data<=midx->next->data){
                midx=p;
            }
            p=p->next;
        }
        Node *tmp=midx->next;
        midx->next=midx->next->next;
        tmp->next=cur->next;
        cur->next=tmp;
        cur=cur->next;
    }
}

int main()
{
    //freopen("C:\\Users\\admin\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\admin\\Desktop\\out.txt","w",stdout);

    ios_base::sync_with_stdio(0);//用这个
	cin.tie(0);                    //就能防止cin超时orz

    int x;
    LinkList L=new Node;
    L->data=-1;
    L->next=NULL;
    Node *p=L;

    while(cin>>x){
        //尾插
        /*Node *ne=new Node;
        p->next=ne;
        ne->data=x;
        ne->next=NULL;
        p=p->next;*/

        //头插
        Node *ne=new Node;
        ne->data=x;
        ne->next=L->next;
        L->next=ne;

    }
    Print(L);
    Sort(L);
    Print(L);
    Delete(L);
    Print(L);
}

 

你可能感兴趣的:(单链表的建立,插入排序,去重)