点击该链接有更全的内容并有基本思路
链表,头插法,尾插法,删除,打印,注释超详细,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;
}