C语言单链表,头指针不为空学习

#include<stdio.h>//标准库
#include<windows.h> //使用系统函数
#include<string.h> //使用字符串函数
#include<malloc.h>//动态申请内存库
#define LEN sizeof(stu)//宏定义结构体类型的大小,方便在之后动态创建节点
typedef struct student{ //定义student结构体,并取别名stu
    char number[11]; //学号
    int scope ;  //成绩
    struct student *next; //下一个人
}stu;

void menu(){ //菜单
    printf("1.添加记录\n");
    printf("2.遍历记录\n");
    printf("3.删除记录\n");
    printf("4.退出程序\n");
}

stu* add(stu* scorestu){ //添加
    stu* pointer = (stu*)malloc(LEN);//来录入临时的值
    printf("输入学号(回车)成绩\n");
    scanf("%s%d",&pointer->number,&pointer->scope);//输入时要带上地址符号
    printf("您刚输的值:%s,%d \n",&pointer->number,pointer->scope);//对于指针字符串,输出时要带上地址符号
    pointer->next = NULL;//将其视为最后一个节点,指向NULL
    if(scorestu==NULL){//判断如果是第一个节点
        scorestu = pointer;//直接将一个数据赋值给首节点,否则执行else,找到尾节点再赋值
    }else {
        stu* temp = scorestu;//临时指针用来寻找最末尾的节点,如果不使用此临时变量,那么最多只能容纳两个节点
        while (temp->next !=NULL){//寻找最后一个节点
            temp=temp->next;//这个地方不可以使用temp++来使其到达下一个节点
        }
        temp->next = pointer;//将最后一个节点指向新节点
    }
    return scorestu;//返回这个链表的首结点
}

int ergodic(stu*scorestu){ //遍历
    int i;//定义循环变量用作计数
    if(scorestu==NULL){
        printf("该链表为空");
        return 0;
    }
    for(i=0;scorestu!=NULL;i++){//判断节点是否为空
        printf("第%d个同学  学号:%s,成绩:%d\n",(i+1),&scorestu->number,scorestu->scope);
        if(scorestu->next!=NULL)//判断是否存在下一个节点,存在则继续深入,否则就退出
            scorestu = scorestu->next;
        else
            break;
    }
    printf("遍历完成\n");
    return i;
}

stu* delect(stu*scorestu){ //删除
    int select = 0;//存放要删除的节点序号
    system("cls");//清屏,防止干扰
    int count = ergodic(scorestu);//先遍历获取节点总数,并给用户判断删除的节点
    printf("总共元素%d\n",count+1);
    do{
        printf("请输入要删除的节点\n");
        scanf("%d",&select);
    }while (select>(count+1) && select<=0);//这里加一是因为接收的返回值是从0开始的,while与do...while条件要反着写
    for(int i=0;scorestu!=NULL;i++){//判断节点是否为空
        if((i+1)==(select)){//表示要删除头节点
            scorestu = scorestu->next;//将当前结构体赋值成下一个
            printf("操作完成\n");
            break;
        }
         if((i+2)==(select)){//找到要被替换的前一个,select是根据返回总数+1来算的,所以这里的i要加2
            scorestu->next=scorestu->next->next;//将前一个指针直接指向下一个的下一个
            printf("操作完成\n");
            break;
         }
        if(scorestu->next!=NULL){//判断是否存在下一个节点,存在则继续深入,否则就退出
            scorestu = scorestu->next;   
        }else{
            printf("没有找到\n");
            break;
        }
    }
    return scorestu;
}

int main(){ //主函数
    int select = 0;
    stu* scorestu = NULL;//使用的链表
    system("chcp 65001");//设置cmd的字符集为utf-8,更好的显示中文
    system("cls");//使用之前都要清屏
    while (1)
    {
        menu();//显示菜单
        scanf("%d",&select);//输入菜单选项
        if (select>4||select<1)
        {
            system("cls");//清屏
            printf("请重新输入:\n");
        }else{
             switch (select)
            {
                case 1:scorestu=add(scorestu);break;
                case 2:ergodic(scorestu);break;
                case 3:scorestu=delect(scorestu);break;
                case 4:return 0;break;
            }
        }
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值