c语言单链表的增删改查

有问题欢迎讨论,单链表的入门练习,插入可以自动排序。

//
// Created by sq on 2022/4/21.
//
//DEV_MIAN_CPP
//list/mian.cpp
#include<iostream>
#include <string.h>
typedef struct Node{
    char name[20];
    int data;
    Node *next;
}Node;
void InsertNod(Node **p, int data, char *name){
    Node *node = (Node *) malloc(sizeof(Node));
    Node *q = *p;
    node->data = data;
    strcpy(node->name, name);
    //添加在链表头
    if((*p)->next == NULL || node->data <= (*p)->next->data){
        node->next = (*p)->next;
        (*p)->next = node;
        return;
    }
    while (q->next != NULL){
        //添加在链表尾
        if(node->data >= q->next->data && q->next->next == NULL){
            q->next->next = node;
            node->next = NULL;
            break;
        }
        //添加在链表体中
        if(node->data >= q->next->data && node->data < q->next->next->data){
            node->next = q->next->next;
            q->next->next = node;
            break;
        }
        q = q->next;
    }
}
void printfNode(Node **p){
    Node *q = *p;
    while(q->next != NULL){
        printf("用户:%s 数据:%d\n", q->next->name, q->next->data);
        q = q->next;
    }
}
void freeNode(Node **p){
    Node *tmp = NULL, *q;
    if(p == NULL){
        return;
    }
    q = (*p)->next;
    while(q != NULL){
        tmp = q;
        q = q->next;
        free(tmp);
    }
}
void deleteNode(Node **p, int n){
    Node *node = NULL;
    if(p == NULL){
        return;
    }
    Node *q = *p;
    while(q->next != NULL){
        if(q->next->data == n){
            node = q->next;
            q->next = q->next->next;
            printf("删除了结点用户:%s 数据:%d\n", node->name, node->data);
            free(node);
            return;
        }
        q = q->next;
    }
    printf("链表中没有结点数据:%d, 删除失败", n);
}
void selectNode(Node **p, int n){
    if(p == NULL){
        return;
    }
    Node *q = *p;
    while(q->next != NULL){
        if(q->next->data == n){
            printf("查询到用户:%s 数据:%d\n", q->next->name, q->next->data);
            break;
        }
        q = q->next;
    }
}
void updateNode(Node **p, int n, int newNum){
    if(p == NULL){
        printf("未修改任何结点。");
        return;
    }
    Node *q = *p;
    while(q->next != NULL){
        if(q->next->data == n){
            q->next->data = newNum;
            printf("修改用户:%s 数据:%d 新数据为:%d\n", q->next->name, n, q->next->data);
            break;
        }
        q = q->next;
    }
}
int main() {
    Node *head = (Node *) malloc(sizeof(Node));
    head->next = NULL;
    //添加结点
    char sq1[20] = "sq1";
    char sq2[20] = "sq2";
    char sq3[20] = "sq3";
    char sq4[20] = "sq4";
    InsertNod(&head, 1, sq1);
    InsertNod(&head, 3, sq3);
    InsertNod(&head, 2, sq2);
    InsertNod(&head, 4, sq4);
    //删除结点
    deleteNode(&head,2);
    //查询结点
    selectNode(&head, 3);
    //修改结点
    updateNode(&head, 4, 44);
    //打印结点
    printfNode(&head);
    //释放内存
    freeNode(&head);
    return 0;
}

怕什么真理无穷,进一寸有进一寸的欢喜 --胡适

好的,以下是 C 语言单链表增删改查代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct Node { int data; struct Node* next; }; // 定义链表头节点指针 struct Node* head = NULL; // 插入节点函数 void insertNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; if (head == NULL) { head = newNode; } else { struct Node* temp = head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } printf("插入节点成功!\n"); } // 删除节点函数 void deleteNode(int data) { if (head == NULL) { printf("链表为空,无法删除!\n"); return; } struct Node* temp = head; if (temp->data == data) { head = head->next; free(temp); printf("删除节点成功!\n"); return; } while (temp->next != NULL && temp->next->data != data) { temp = temp->next; } if (temp->next == NULL) { printf("未找到要删除的节点!\n"); return; } struct Node* deleteNode = temp->next; temp->next = deleteNode->next; free(deleteNode); printf("删除节点成功!\n"); } // 修改节点函数 void modifyNode(int data, int newData) { if (head == NULL) { printf("链表为空,无法修改!\n"); return; } struct Node* temp = head; while (temp != NULL && temp->data != data) { temp = temp->next; } if (temp == NULL) { printf("未找到要修改的节点!\n"); return; } temp->data = newData; printf("修改节点成功!\n"); } // 查找节点函数 void searchNode(int data) { if (head == NULL) { printf("链表为空,无法查找!\n"); return; } struct Node* temp = head; while (temp != NULL && temp->data != data) { temp = temp->next; } if (temp == NULL) { printf("未找到要查找的节点!\n"); return; } printf("找到要查找的节点,数据为:%d\n", temp->data); } // 打印链表函数 void printList() { if (head == NULL) { printf("链表为空,无法打印!\n"); return; } struct Node* temp = head; printf("链表中的节点数据为:"); while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { insertNode(1); insertNode(2); insertNode(3); printList(); modifyNode(2, 4); printList(); searchNode(3); deleteNode(2); printList(); return 0; } ``` 以上是单链表的基本操作函数,包括插入节点、删除节点、修改节点、查找节点以及打印链表等功能。你可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值