单链表

//删除单链表中最大的值   时间复杂度O(n) 
void delmaxnode(LinkedList *&L){
    LinkedList *pre = L,*p = L->next,*maxpre = pre,*maxp = p;
    while(p!=NULL)
    {
        if(maxp->data<p->data){
            maxp = p;
            maxpre = pre;
        }
        pre = p;
        p = p->next;
    }
    maxpre->next = maxp->next;
    free(maxp); 
}
//单链表排序 时间复杂度O(n^2)
//将链表分为有序区和无序区 
void sort(LinkedList *&L){
    LinkedList *pre,*p,*q;
    p = L->next->next;
    L->next->next = NULL; //一开始有序区只有一个结点 
    while(p!=NULL){
        q = p->next;
        pre = L;
        //将p结点插入有序区 
        while(pre->next!=NULL&&pre->next->data<p->data){
            pre = pre->next;
        }
        p->next = pre->next;
        pre->next = p;
        
        p = q;
    }
}
//将单链表倒转过来 头插法建表的办法 
void reverse(LinkedList *&L){
    LinkedList *p = L->next;
    L->next = NULL; 
    while(p!=NULL){
        q = p->next;
        p->next = L->next;
        L->next = p;
        p = q;
        }    
}
// 将链表分成两个链表 a1->b1->a2->b2
// a1->a2->a3    b(n)->b(n-1)->b(n-2) 
// L1采用尾插法建表,L2采用头插法建表 
void split(LinkedList *&L,LinkedList *&L1,LinkedList *&L2){
    LinkedList *p = L->next,*q,*r1;
    L1 = L;
    r1 = L1;
    L2 = (LinkedList*)malloc(sizeof(LinkedList));
    L2->next = NULL;
    while(p!=NULL){
        r1->next = p;
        r1 = p;
        p = p->next;
        q = p->next;
        p->next = L2->next;
        L2->next = p;
        
        p = q;
    }
    r1->next = NULL; 
} 

 

posted @ 2019-06-05 21:49  飞蝎儿  阅读(165)  评论(0编辑  收藏  举报