#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&¤t->value<newValue){
ppLink = ¤t->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&¤t->value<newValue){
ppLink = ¤t->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;
}
网友评论