C语言链表-学习笔记-图+代码方便理解

这段代码实现了一个简单的链表数据结构,包括创建链表、在指定位置插入节点、删除节点、改变节点数据、删除头尾节点、头插法、查找指定数据和打印链表特定位置数据等功能。用户可以交互选择操作,适用于学习链表的基本操作。
摘要由CSDN通过智能技术生成

链表创建示意图在这里插入图片描述
删除节点
在这里插入图片描述

代码加注释如下,可以直接复制

#include"stdio.h"
#include<stdio.h>
#include<malloc.h>
#define true 1
void changenode(struct stu* head, int n);//改变链表指定位置的数据
struct stu* create(int n);//创建一个长度为N的链表
void print(struct stu* head);//打印全部数据
void insertnode(struct stu* head, int n);//在指定位置插入数据
void delete(struct stu* head, int n);//删除指定节点数据
void delete1(struct stu* head);//尾删节点
void delete2(struct stu* head);//头删节点
struct stu* create1(struct stu* head);//头插节点
struct stu* create2(struct stu* head);//尾插节点
int find(struct stu* head, int n);//查找指定数据的位置
void print1(struct stu* head);//打印链表指定位置的数据
struct stu
{
int id;
char name[50];
struct stu* next;
};
int main()
{

int n, x, y,a,b;
struct stu* head = NULL;
printf("请输入你要创建的节点个数:\n");
scanf("%d", &n);
head = create(n);
print(head);
while (true)
{
    printf("请选择要进行的操作:1.删除节点 2.插入节点 3.改变节点数据 4.删除尾节点 5. 删除头节点 6.头插节点 7.查找指定数据 8.打印指定位置数据\n");
    scanf("%d", &x);
   // printf("请输入数据\n");
    if (x == 1)
    {
        printf("请输入要删除的节点位置\n");
        scanf("%d", &y);
        delete(head, y);
        print(head);
    }
    if (x == 2)
    {
        printf("请输入要插入的节点位置\n");
        scanf("%d", &y);
        insertnode(head, y);
        print(head);
    }
    if (x == 3)
    {
        printf("请输入要改变的节点位置\n");
        scanf("%d", &y);
        changenode(head, y);
        print(head);
    }
    if (x == 4)
    {
        printf("已经删除尾节点\n");
        delete1(head);
        print(head);
    }
    if (x == 5)
    {
        printf("已经删除头节点\n");
        delete2(head);
        print(head);
    }
    if (x == 6)
    {
        printf("请输入数据\n");
        create1(head);
        print(head);
    }
    if (x == 7)
    {
        printf("请输入要查找的数据\n");
        scanf("%d", &a);
        printf("要查找的数据在节点%d\n", find(head, a));
        print(head);
    }
    if (x == 8)
    {
        print1(head);
    }

}

}

struct stu* create(int n)
{
struct stu* head, * node, * end; //定义头节点,普通节点,尾节点
head = (struct stu*)malloc(sizeof(struct stu)); //给头节点申请内存
end = head; //如果是空链表,让头尾节点地址一致
for (int i = 0; i < n; i++)
{

    node = (struct stu*)malloc(sizeof(struct stu));//给普通节点申请内存空间
    scanf("%d %s", &node->id, &node->name);//给链表添加数据
    end->next = node;//让链表尾节点指向新的普通节点
    end = node;//让尾节点跟新的普通节点地址一致,方便让这个普通节点的指针指向下一个新创建的节点

}
end->next = NULL; //让尾节点指向空

//node->next = head;//头插法
//head = node;

return head;//返回链表的执政。

}
struct stu* create1(struct stu* head)//头插节点
{
struct stu* node,pr; //定义头节点,普通节点,尾节点
node = (struct stu
)malloc(sizeof(struct stu));//给普通节点申请内存空间
scanf(“%d %s”, &node->id, &node->name);//给链表添加数据
pr = head->next;
head->next = node;//让链表尾节点指向新的普通节点
node->next = pr;
return head;//返回链表的执政。
}
int find(struct stu* head,int n)//查找指定数据
{
struct stu *p=head; //定义头节点,普通节点,尾节点
int a=0;
while (p->next!=NULL)
{

    p = p->next;
    if (p->id == n)
    {
        break;
    }
    a++;
}

return a;//返回链表的执政。

}

struct stu* create2(struct stu* head)//头插节点
{
struct stu* node, * pr; //定义头节点,普通节点,尾节点
node = (struct stu*)malloc(sizeof(struct stu));//给普通节点申请内存空间
scanf(“%d %s”, &node->id, &node->name);//给链表添加数据
//while()
pr = head->next;
head->next = node;//让链表尾节点指向新的普通节点
node->next = pr;
return head;//返回链表的执政。
}

void print(struct stu* head)//打印全部链表
{
struct stu* p = head;
int j = 0;
p = p->next;//不打印头节点数据;
while (p != NULL)
{
printf(“%d %d %s\n”, j, p->id, p->name);//打印数据
p = p->next;//指针逐个往下
j++;//次数加一
}

}

void print1(struct stu* head)//打印链表指定位置的数据
{
struct stu* p = head;
int j = 0;
p = p->next;//不打印头节点数据;
printf(“请输入要打印的节点\n”);
scanf(“%d”, &j);
for (int i = 0; i < j; i++)
{
p = p->next;//指针逐个往下
}
printf(“%d %s”, p->id, p->name);

}
void insertnode(struct stu* head, int n)
{
struct stu* p = head, * pr; //定义一个指针指向头节点,一个新指针准备插入链表;
int i = 0; //
pr = (struct stu*)malloc(sizeof(struct stu));//给准备插入的节点申请内存
printf(“please intput:”);
scanf(“%d %s”, pr->id, pr->name);//给准备插入的节点赋值

while (i < n && p->next != NULL)//让指针指向准备插入的节点,并且保证该节点不为空
{
    p = p->next;//移动指针
    i++;
}
if (p != NULL)//确保指针不为空
{
    pr->next = p->next;//让新建的节点指针域指向将要插入的节点后一个节点地址
    p->next = pr;//让将要插入的节点的前一个节点指针域指向新建的节点地址
}
else
{
    printf("节点不存在");
}

}

void delete(struct stu* head, int n)
{
struct stu* pr = head, * p = head;//定义两个指针
int i = 0;
while (i < n && p->next != NULL)
{
pr = p;//让两个指针一前一后同步向前移动
p = p->next;
i++;
}
if (p->next != NULL)
{
pr->next = p->next;
free§;
}
else
{
printf(“这是一个空节点”);
}
}

void delete1(struct stu* head)//尾删节点
{
struct stu* pr = head, * p = head;//定义两个指针
int i = 0;
if (head == NULL)
{
return;
}
else if (head->next == NULL)
{
free(head);
head = NULL;
}
else
{
while (p->next != NULL)
{
pr = p;
p = p->next;
}
pr->next = NULL;
free§;
}
}

void delete2(struct stu* head)//头删节点
{
struct stu* pr = head, * p = head;//定义两个指针
int i = 0;
if (head == NULL)
{
return;
}
else
{
p = head->next;
head->next = p->next;
free§;
}

}

void changenode(struct stu* head, int n)
{
struct stu* p = head;
int i = 0;
while (i < n && p->next != NULL)
{
p = p->next;
}
if (p != NULL)
{

    printf("please input");
    scanf("%d", &p->id);
}
else
{
    printf("该节点为空");
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泰7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值