编辑代码

#include <stdio.h>
#include<stdlib.h>

typedef struct _node{
    int value;
    struct _node *next;
}Node;

typedef struct _list{
    Node* head;
}List;

void add(List* pList,int number);
void print(List *pList);


int main () {
    List list;
    list.head = NULL;
    int number;
    do{
        scanf("%d",&number);
        if(number != -1){
            add(&list, number);
        }
    }while(number != -1);
    print(&list);
    // // search
    // scanf("input a number:%d",&number);
    // Node *p;
    // int isFound = 0;
    // for(p = list.head;p;p = p->next){
    //     if(p->value == number){
    //         printf("Find it!\n");
    //         isFound=1;
    //         break;
    //     }
    // }
    // if(!isFound){
    //     printf("NOT Find it!\n");
    // }
    // delete
    scanf("input a number to be deleted:%d",&number);
    Node *p;
    Node *q;
    for(q = NULL,p = list.head;p;q = p,p = p->next){
        if(p->value == number){
            if(q){
                q->next = p->next;
            }else{
                list.head= p->next;
            }
            free(p);
            break;
        }
    }
    // clear
    for(p =list.head;p;q=q){
        q = p->next;
        free(p);
    }
    return 0;
}  

void add(List* pList,int number){
    // add to linked-list
    Node *p = (Node*)malloc(sizeof(Node));
    p->value = number;
    p->next = NULL;
    // find the last
    Node *last = pList->head;
    if(last != NULL){
        while(last->next){
            last = last->next;
        }
        // attach
        last->next = p;
        printf("add:%i\n",p->value);
    }else{
        pList->head = p;
    }
}

void print(List *pList){
    Node *p;
    for(p = pList->head;p;p=p->next){
        printf("%d\t",p->value);
    }
    printf("\n");
}