美文网首页
错误的链表插入函数代码

错误的链表插入函数代码

作者: crabor | 来源:发表于2018-03-26 13:01 被阅读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){
        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 Insert1(Node **ppLink,int newValue){
        Node new, *current;
    
        new.value = newValue;
    
        while(current=*ppLink,current!=NULL&&current->value<newValue){
            ppLink = &current->next;
        }
    
        *ppLink = &new;
        new.next = current;
    
        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);
        }
        printf("\n");
        Insert1(&linkedList, 20);
        for (Node *p = linkedList; p!= NULL;p=p->next){
            printf("%d ", p->value);
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:错误的链表插入函数代码

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