链表的创建,插入 ,删除(源码VS2019)

点击该链接有更全的内容并有基本思路

链表,头插法,尾插法,删除,打印,注释超详细,Ctrl C就可以直接用;_K3V2的博客-CSDN博客_尾插法

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

//定义结构体
struct Student {
    int data;//可用结构体替换
    struct Student* next;
};

struct Student* creathead()//创建表头
{
    struct Student* head = (struct Student*)malloc(sizeof(struct Student));//申请动态内存
    head->next = NULL;
    return head;
}

struct Student* creatstudent(int data)//数据域传入数据,创建节点
{
    struct Student* newstudent = (struct Student*)malloc(sizeof(struct Student));
    newstudent->data = data;
    newstudent->next = NULL;
    return newstudent;
}

void printlist(struct Student *head)//打印链表
{
    struct Student* Pmove = head->next;
    while(Pmove!=NULL)
    {
        printf("%d\n", Pmove->data);
        Pmove = Pmove->next;
    }
}

void insetbyhead(struct Student* head, int data)//头插法
{
    struct Student* newstudent = creatstudent(data);
    newstudent->next = head->next;
    head->next = newstudent;

}

void deletlist(struct Student* head, int ddata)//删除节点
{
    struct Student* posstudent = head->next;
    struct Student* studentfront = head;
    if(posstudent==NULL)
    {
        printf("error\n");
    }
    else
    {
        while (posstudent->data != ddata)
        {  
            studentfront = posstudent;
            posstudent = posstudent->next;
            if(posstudent == NULL)
            {
                 printf("error\n");
                 break;
                      }
        }
        
        studentfront->next = posstudent->next;
        free(posstudent);
        
    }

}
int main()//setbuf(stdin,NULL);清空缓冲区函数
{
    
    struct Student* list = creathead();
    insetbyhead(list, 1);
    insetbyhead(list, 2);
    insetbyhead(list, 3);
    insetbyhead(list, 4);
    printlist(list);
    deletlist(list, 3);
    printlist(list);


    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值