链表

作者: crabor | 来源:发表于2018-02-23 17:04 被阅读0次

    主函数用于排序

    #include <stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    typedef struct NODE {
      struct NODE *next;
      int value;
    } Node;
    
    typedef enum { ERROR = 0, OK = 1 } Status;
    
    Status Insert(Node **ppLink,int newValue){//有current版本
        Node *new, *current;
    
        new = (Node *)malloc(sizeof(Node));
        if(new==NULL){
            return ERROR;
        }
        new->value = newValue;
    
        while(current=*ppLink,current!=NULL&&current->value<newValue){
            ppLink = &current->next;
        }
    
        *ppLink = new;
        new->next = current;
    
        return OK;
    }
    
    // Status Insert(Node **ppLink,int newValue){//无*ppLink版本
    //     Node *new;
    
    //     new = (Node *)malloc(sizeof(Node));
    //     if(new==NULL){
    //         return ERROR;
    //     }
    //     new->value = newValue;
    
    //     while(*ppLink!=NULL&&(*ppLink)->value<newValue){
    //         ppLink = &(*ppLink)->next;
    //     }
    
    //     new->next = *ppLink;
    //     *ppLink = new;
    
    //     return OK;
    // }
    
    int main(int argc,char *argv[]){
        Node *linkedList=NULL;
        int temp;
        srand((unsigned)time(NULL));
        for (int i = 0; i < 10;i++){
            temp = rand() % 101;
            printf("%d ", temp);
            Insert(&linkedList,temp);
        }
        printf("\n");
        for (Node *p = linkedList; p!= NULL;p=p->next){
            printf("%d ", p->value);
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:链表

          本文链接:https://www.haomeiwen.com/subject/fybxxftx.html