PS: goto还是很好玩的。
#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* list);
void deletel(List* list,int number);
void freel(List* list);

int main(int argc, char *argv[]) {
    Node *head = NULL;
    List list;
    int number=0;
    list.head=NULL;
    for(;;){
    printf("\n这是一个链表,现有如下功能  1.添加节点  2.遍历链表  3.查找并删除节点  4.释放链表 5.退出程序 \n请输入你想要进行的操作的选项:\n" );
    out:
    scanf("%d",&number);
    switch(number){
        case 1:
            printf("你选择了添加节点\n请输入添加节点的值,输入-1结束添加\n");
            do{
                scanf("%d",&number);
                if(number != -1){
                    //添加一个新的节点挂载。 
                    add(&list,number);
                }
            }while(number != -1);
            printf("添加完毕");
            break;
        case 2:
            printf("你选择了遍历链表\n");
            //打印链表 
            print(&list);
            break;
        case 3:
            printf("你选择了查找并删除节点,请输入节点数据\n");
            scanf("%d",&number);
            //删除一个节点 
            deletel(&list,number);
            break;
        case 4:
            printf("你选择了释放链表");
            //释放链表 
            freel(&list);
            printf("释放完毕");
            break;
        case 5:
            goto end;
        default :
            printf("输入错误,请重新输入\n");
            goto out;
        }
    }
    end:
    printf("拜拜");
    return 0;
}
void add(List* plist,int number){
    Node *p =(Node*)malloc(sizeof(Node));
    p->value =number;
    p->next =NULL;
    Node *last =plist->head;
    if(last){
        while(last->next){
            last = last->next;
        }
        last->next=p;
    }else{
        plist->head = p;
    }
}
    //遍历链表 
void print(List* list){
    Node *p;
    for(p=list->head;p;p=p->next){
        printf("%d\t",p->value);
    }
    printf("\n");
}
    //查找并删除节点
void deletel(List* list,int number){
    Node* p;
    int isFound =0;
    for(p=list->head;p;p=p->next){
        if(p->value==number){
            printf("找到了\n");
            isFound = 1;
            break ;
        }
    }
        if(!isFound){
        printf("没找到");
        }
        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);
            printf("已经删除");
            break ;
        }
    }
}
    //释放链表 
void freel(List* list){
    Node *q;
    Node* p;
    for(p=list->head;p;p=q){
    q=p->next;
    free(p);
    }
}

读书和健身总有一个在路上

02-13 03:14