单链表基本操作c语实现

整理了下以前的代码,放到这里来,防止又被我格了(上次很多被不小心格了,哭啊!) 。

单链表的一些操作:

 

// :linkList.c

#include 
< stdio.h >
#include 
< malloc.h >
#include 
< conio.h >

// define a node
typedef struct node {
    
int val;
    struct node 
*next;
}
 node;

// create a link list by tail inserting
void  tail_insert(node  ** h, int  i) {
    
if (*== 0){
        
*= (node*)malloc(sizeof(node));
        (
*h)->val = i;
        (
*h)->next = 0;
        
return;
    }

    node 
*= (node*)malloc(sizeof(node)),*pl = *h;
    p
->val = i;
    p
->next = 0;    
    
while (pl->next)
        pl 
= pl->next;
    pl
->next = p;
    
return;
}


// create a link list by head inserting
void  head_insert(node  ** h, int  i) {
    
if (*== 0){
        
*= (node*)malloc(sizeof(node));
        (
*h)->next = 0;
        (
*h)->val = i;
        
return;
    }

    node 
*= (node*)malloc(sizeof(node));
    p
->val = i;
    p
->next = *h;
    
*= p;
    
return;
}


// reverse a link list by iteration
node *  reverse_iteration(node  * h) {
    node 
*nh = h,*= 0;
    h 
= h->next;
    nh
->next = 0;
    
while (h){
        p 
= h;
        h 
= h->next;
        p
->next = nh;
        nh 
= p;
    }

    
return nh;
}


// reverse a link list by recursion
node  *  reverse_recursion(node  * h) {
    
if (!|| !h->next)
        
return h;
    node 
*= h->next,*= 0;
    r 
= reverse_recursion(p);
    p
->next = h;
    h
->next = 0;
    
return r;
}


// order link list by using typical algorithm
// insertion sort
node *  insertionSortL(node  * h) {
    node 
*sorted = h,*unsorted = sorted->next,
         
*p1 = 0,*p2 = 0;
    
while (unsorted){
        
if (unsorted->val < h->val){
            sorted
->next = unsorted->next;
            unsorted
->next = h;
            h 
= unsorted;
        }

        
else{
            p1 
= h;
            p2 
= p1->next;
            
while (unsorted->val > p2->val){
                p1 
= p2;
                p2 
= p2->next;
            }

            
if (p2 != unsorted){
                sorted
->next = unsorted->next;
                p1
->next = unsorted;
                unsorted
->next = p2;
            }

            
else
                sorted 
= unsorted;
        }

        unsorted 
= sorted->next;
    }

    
return h;
}
            

// quick sort
node *  quickSortL(node  * h,node  * t) {
    
if (h == t)
        
return h;
    node 
*pl = h,*pr = t,*p1 = h->next,*p2 = 0;
    
while (p1 != t){
        p2 
= p1;
        p1 
= p1->next;
        
if (p2->val < h->val){
            p2
->next = pl;
            pl 
= p2;
        }

        
else{
            p2
->next = pr;
            pr 
= p2;
        }

    }

    h
->next = quickSortL(pr,t);
    
return quickSortL(pl,h);    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值