寒光博客

[c语言]简洁 完整 单链表 有注释 谭浩强
如有问题 欢迎指点 #include <stdio.h> #include <stdlib.h&...
扫描右侧二维码阅读全文
16
2021/05

[c语言]简洁 完整 单链表 有注释 谭浩强

如有问题 欢迎指点

#include <stdio.h>
#include <stdlib.h>
/**
 * 每次操作 都是操作代理指针 不能操作链表的头指针!!
 * setbuf(stdin,NULL);清空输入缓存
 * @return
 */
struct Node {
    int data;
    struct Node *next;
};

struct Node *creatList();//创建链表 头结点
struct Node *creatNode(int data);//创建节点
void insertNodeInEnd(struct Node *list, int data);//头插法
void insertNodeInHead(struct Node *list, int data);//尾插法
void printList(struct Node *list);//打印链表 最后加个换行
void deleteNode(struct Node *list, int data);//删除 指定数据节点 双指针 前后步进(有头结点的重要性!)



//创建链表 头结点
struct Node *creatList() {
    struct Node *headNode = (struct Node *) malloc(sizeof(struct Node));
    headNode->data = -1;//初始化头data结点为-1
    headNode->next = NULL;
    return headNode;
}

//创建节点
struct Node *creatNode(int data) {
    struct Node *node = (struct Node *) malloc(sizeof(struct Node));
    node->data = data;
    node->next = NULL;
    return node;
}

//尾插 法
void insertNodeInEnd(struct Node *list, int data) {
    //没有第一个元素 就直接把list的next指向新节点
    if (list->next == NULL) {
        list->next = creatNode(data);
        return;
    }
    //每次操作必须使用代理指针!!
    struct Node *p = list->next;
    while (p->next) {//不能判断p 因为要知道上一个有效节点
        p = p->next;
    }
    p->next = creatNode(data);//不应该是p=creatNode() 要知道上一个节点 并指向其
}

void insertNodeInHead(struct Node *list, int data) {
    struct Node *newNode;
    newNode = creatNode(data);
    newNode->next = list->next;
    list->next = newNode;
}
/**
 * 打印链表
 * 判断是否为空
 * @param list
 */
void printList(struct Node *list) {
    if (NULL == list->next) {
        printf("this list is NULL\n");
        return;
    }
    struct Node *p = list->next;//跳过头指针
    while (p) {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

/**
 * 删除指定数据节点
 * 双指针 删除 pre是p的前一个指针
 * 要判断是否为空
 * @param list
 * @param data
 */
void deleteNode(struct Node *list, int data) {
    struct Node *p = list->next;//从头结点开始
    struct Node *pre = list;//p的前一个节点
    if (p == NULL) {
        printf("this List is NULL\n");
        return;
    }
    while (p) {//头结点的下一个节点
        if (p->data == data) {
            pre->next = p->next;
            free(p);
            printf("delete %d successful\n", data);
            return;//若删除多个相同data节点  可以删除该句子和最后的 dont find
        }
        pre = pre->next;
        p = pre->next;//p一定 指向的是pre的下一个(这样可以保证 如果是多个节点的话)
    }
    printf("dont find this data:%d\n", data);
}

int main() {
    struct Node *list;
    list = creatList();

    printList(list);
    insertNodeInEnd(list, 4);
    insertNodeInEnd(list, 5);
    insertNodeInEnd(list, 6);
    insertNodeInHead(list, 3);
    insertNodeInHead(list, 2);
    insertNodeInHead(list, 1);
    //打印插入的所有
    printList(list);
    //删除4
    deleteNode(list, 4);
    printList(list);

    deleteNode(list, 4);
    printList(list);
}
本文作者:Author:     文章标题:[c语言]简洁 完整 单链表 有注释 谭浩强
本文地址:https://dxoca.cn/StudyNotes/386.html       百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
Last modification:May 16th, 2021 at 12:42 am
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment

One comment

  1. 寒光博客 Google Chrome 86.0.4240.198 Windows 10 中国 湖北

    关于尾插法 写的有点复杂了 ,,直接p=head
    然后p->next循环移动指针即可 然后加入新节点 (有头指针就很方便)