• 单链表的基本操作


    原文 http://c.biancheng.net/view/3338.html

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct Link {
        int  elem;
        struct Link *next;
    }link;
    link * initLink();
    //链表插入的函数,p是链表,elem是插入的结点的数据域,add是插入的位置
    link * insertElem(link * p, int elem, int add);
    //删除结点的函数,p代表操作链表,add代表删除节点的位置
    link * delElem(link * p, int add);
    //查找结点的函数,elem为目标结点的数据域的值
    int selectElem(link * p, int elem);
    //更新结点的函数,newElem为新的数据域的值
    link *amendElem(link * p, int add, int newElem);
    void display(link *p);
    int main() {
        //初始化链表(1,2,3,4)
        printf("初始化链表为:
    ");
        link *p = initLink();
        display(p);
        printf("在第4的位置插入元素5:
    ");
        p = insertElem(p, 5, 4);
        display(p);
        printf("删除元素3:
    ");
        p = delElem(p, 3);
        display(p);
        printf("查找元素2的位置为:
    ");
        int address = selectElem(p, 2);
        if (address == -1) {
            printf("没有该元素");
        }
        else {
            printf("元素2的位置为:%d
    ", address);
        }
        printf("更改第3的位置上的数据为7:
    ");
        p = amendElem(p, 3, 7);
        display(p);
        return 0;
    }
    link * initLink() {
        link * p = (link*)malloc(sizeof(link));//创建一个头结点
        link * temp = p;//声明一个指针指向头结点,用于遍历链表
        //生成链表
        for (int i = 1; i < 5; i++) {
            link *a = (link*)malloc(sizeof(link));
            a->elem = i;
            a->next = NULL;
            temp->next = a;
            temp = temp->next;
        }
        return p;
    }
    link * insertElem(link * p, int elem, int add) {
        link * temp = p;//创建临时结点temp
        //首先找到要插入位置的上一个结点
        for (int i = 1; i < add; i++) {
            temp = temp->next;
            if (temp == NULL) {
                printf("插入位置无效
    ");
                return p;
            }
        }
        //创建插入结点c
        link * c = (link*)malloc(sizeof(link));
        c->elem = elem;
        //向链表中插入结点
        c->next = temp->next;
        temp->next = c;
        return  p;
    }
    link * delElem(link * p, int add) {
        link * temp = p;
        //遍历到被删除结点的上一个结点
        for (int i = 1; i < add; i++) {
            temp = temp->next;
            if (temp->next == NULL) {
                printf("没有该结点
    ");
                return p;
            }
        }
        link * del = temp->next;//单独设置一个指针指向被删除结点,以防丢失
        temp->next = temp->next->next;//删除某个结点的方法就是更改前一个结点的指针域
        free(del);//手动释放该结点,防止内存泄漏
        return p;
    }
    int selectElem(link * p, int elem) {
        link * t = p;
        int i = 1;
        while (t->next) {
            t = t->next;
            if (t->elem == elem) {
                return i;
            }
            i++;
        }
        return -1;
    }
    link *amendElem(link * p, int add, int newElem) {
        link * temp = p;
        temp = temp->next;//tamp指向首元结点
        //temp指向被更改结点
        for (int i = 1; i < add; i++) {
            temp = temp->next;
        }
        temp->elem = newElem;
        return p;
    }
    void display(link *p) {
        link* temp = p;//将temp指针重新指向头结点
        //只要temp指针指向的结点的next不是Null,就执行输出语句。
        while (temp->next) {
            temp = temp->next;
            printf("%d ", temp->elem);
        }
        printf("
    ");
    }
  • 相关阅读:
    Kubernetes实战指南(三十三):都0202了,你还在手写k8s的yaml文件?
    Hadoop学习笔记
    Anaconda、Pycharm的安装与运行和Python环境的搭建
    常用编程软件文件配置(下载安装教程)
    error C2678: 二进制“<”: 没有找到接受“const _Ty”类型的左操作数的运算符
    Java 移位运算、符号位扩展
    c++ 集合操作
    c++ 输入与缓冲区
    python 装饰器
    python global 与 nonlocal
  • 原文地址:https://www.cnblogs.com/nanqiang/p/11528790.html
Copyright © 2020-2023  润新知