错误的链表插入函数代码

2018-03-26  本文已影响0人  crabor
#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;
}
上一篇下一篇

猜你喜欢

热点阅读