单链表(学生信息增删改查)

刚学会单链表,于是就来写一个巩固一下哈哈哈
之前因为动态内存分配没有理解到位所以卡了很久
这里的data只有姓名和学号(因为懒)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student{
    char name[10];
    int num;
    struct student *next;
};

struct student *createList(){
    struct student *headnode = (struct student *)malloc(sizeof(struct student));
    strcpy(headnode->name," ");
    headnode->next = NULL;
    headnode->num = 0;
    return headnode;
}

struct student *createnode(char name[10],int num){
    struct student *newnode = (struct student *)malloc(sizeof(struct student));
    strcpy(newnode->name, name);
    newnode->num = num;
    newnode->next = NULL;
    return newnode;
}

void insertnode(struct student *headnode,char name[10],int num){
    struct student *node = createnode(name,num);
    while(headnode->next){
        headnode = headnode->next;
    }
    headnode->next = node;
    node->next = NULL;
}

void printlist(struct student *headnode){
    struct student *p = headnode->next;
    if(p==NULL){
        printf("the list is empty,please choose the option 2 to add something\n");
    }
    while (p)
    {
        printf("num: %d name: %s\n", p->num,p->name);
        p = p->next;
    }
}

void delete(struct student *headnode,int n){
    struct student *p = headnode->next;
    struct student *fp = headnode;
    while(p&&p->num!=n){
        p = p->next;
        fp = fp->next;
    }
    if(p==NULL){
        printf("the number is invalid\n");
    }else{
        fp->next = fp->next->next;
        free(p);
        printf("you have just delete the data!\n");
    }
}

void searchdata(struct student *headnode,int search){
    struct student *p = headnode->next;
    while(p){
        if(p->num==search){
            printf("num: %d name: %s\n", p->num,p->name);
            break;
        }else{
            p = p->next;
        }
    }
    if(p==NULL){
        printf("the number is invalid\n");
    }
}

void changename(struct student *headnode,int num,char new_name[10]){
    struct student *p = headnode->next;
    while(p){
        if(p->num==num){
            strcpy(p->name, new_name);
            break;
        }else{
            p = p->next;
        }
    }
    if(p==NULL){
        printf("the number is invalid\n");
    }
}

int main(){
    char name[10],new_name[10];
    int course, num, n,search;
    struct student *list = createList();
    printf("\n\n\t\tWELCOMT TO THE LINKEDLIST OPERATION\n");
    do{
        printf("\nplease choose your operation: 1.search list 2.insert data 3.printf list 4.delete 5.change 6.quit\n (input the number): ");
        scanf("%d", &course);
        switch (course)
        {
        case 1:
            printf("please input the num that you want to search: ");
            scanf("%d", &search);
            searchdata(list, search);
            break;
        case 2:
            printf("please input the num: ");
            scanf("%d", &num);
            printf("please input the name: ");
            scanf("%s", name);
            insertnode(list, name,num);
            break;
        case 3:
            printlist(list);
            break;
        case 4:
            printf("please input the number which you want to delete:");
            scanf("%d", &n);
            delete (list, n);
            break;
        case 5:
            printf("please input the number which you want to change the name:");
            scanf("%d", &num);
            printf("please input the newname: ");
            scanf("%s", new_name);
            changename(list, num, new_name);
            break;
        case 6:
            printf("you have quit the program!");
            return 0;
        }
    } while (1);
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值