链表修改数据(附完整代码)

该文章提供了一段C语言代码,用于创建一个链表,初始化节点值,然后修改链表中指定位置的数据,并遍历输出链表以验证结果。示例中展示了使用for和while循环两种方式来修改链表节点值。
摘要由CSDN通过智能技术生成

(完整代码在文章 "最…下方")

  1. 创建链表并初始化值

  1. 修改链表中指定位置数据

  1. 遍历输出链表查看结果

(下方为头部代码)

//定义结构体
typedef struct Node{
    int data;             //数据域
    struct Node *next;    //指针域
}Node;

void create(Node *head);    //创建
void alter(Node *head);     //修改
void print(Node *head);     //输出

int main(){
    
    Node *head = (Node *)malloc(sizeof(Node));
    
    create(head);        //创建链表
    
    alter(head);        //修改链表指定位置的值     
    
    print(head);        //遍历链表

}

1、创建链表

void create(Node *head){
    
    if(!head){
        return;
    }
    
    Node *q = head;
    
    for(int i=0;i<5;i++){
        Node *p = (Node *)malloc(sizeof(Node));
        p->data = i+1;
        
        q->next = p;
        q = q->next;
    }
    
    q->next = NULL;
    
}

2、修改数据

这里将for循环 与 while循环 两种方式均展示出来。

void alter(Node *head){
    
    if(!head){
        return;
    }
    
    Node *q = head;
    
    // for循环修改值
    for(int i=0; q->next; i++, q=q->next ){
        if(i==3){
            printf("请输入修改后的值:");
            scanf("%d",&q->data);
        }
    }

    // while循环修改值
    int count = 0;        //定义计数器,用来统计循环次数 即结点位置
    while(q->next){
        count++;
        if(count ==3){
            printf("请输入修改后的值:");
            scanf("%d",&q->data);
        }
    }
    
}

3、遍历链表

void print(Node *head){
    
    if(!head) {
        return;
    }
    
    Node *q = head->next;
    
    while(q){
        printf("%d ",q->data);
        q = q->next;
    }
    
}

  • 完整代码板

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

typedef struct Node{
    int data;
    struct Node *next;
}Node;

void create(Node *head);
void alter(Node *head);
void print(Node *head);

int main(){
    
    Node *head = (Node *)malloc(sizeof(Node)) ;
    
    create(head);
    
    alter(head);  
    
    print(head);

}

void create(Node *head){
    
    if(!head){
        return;
    }
    
    Node *q = head;
    
    for(int i=0;i<5;i++){
        Node *p = (Node *)malloc(sizeof(Node));
        p->data = i+1;
        
        q->next = p;
        q = q->next;
    }
    
    q->next = NULL;
    
}

void alter(Node *head){
    
    if(!head){
        return;
    }
    
    Node *q = head;
    
    for(int i=0; q->next; i++, q=q->next ){
        if(i==3){
            printf("请输入修改后的值:");
            scanf("%d",&q->data);
        }
    }
    
}

void print(Node *head){
    
    if(!head) {
        return;
    }
    
    Node *q = head->next;
    
    while(q){
        printf("%d ",q->data);
        q = q->next;
    }
    
}
  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
下面是一个简单的 C 语言链表修改原有数据的代码示例: ```c #include <stdio.h> #include <stdlib.h> typedef struct ListNode { int data; struct ListNode* next; } ListNode; void modifyList(ListNode* head, int target, int newValue) { ListNode* current = head; while (current) { if (current->data == target) { current->data = newValue; return; } current = current->next; } } int main() { // 创建链表 ListNode* head = (ListNode*)malloc(sizeof(ListNode)); head->data = 1; head->next = NULL; ListNode* second = (ListNode*)malloc(sizeof(ListNode)); second->data = 2; second->next = NULL; head->next = second; ListNode* third = (ListNode*)malloc(sizeof(ListNode)); third->data = 3; third->next = NULL; second->next = third; // 修改链表数据 modifyList(head, 2, 4); // 输出链表数据 ListNode* current = head; while (current) { printf("%d\n", current->data); current = current->next; } // 释放链表所占用的内存 current = head; while (current) { ListNode* temp = current; current = current->next; free(temp); } return 0; } ``` 在上面的代码,`modifyList` 函数可以接收一个链表的头结点指针、需要修改的数值、以及新的数值作为参数。函数会遍历整个链表,找到第一个值为 `target` 的节点,并将其值修改为 `newValue`。如果链表不存在值为 `target` 的节点,则不做任何修改。 在 `main` 函数,我们创建了一个简单的链表,然后调用 `modifyList` 函数将链表值为 2 的节点修改为 4。最后,我们输出修改后的链表,并释放链表所占用的内存。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Discord_lim

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

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

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

打赏作者

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

抵扣说明:

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

余额充值