题解 | #KiKi学结构体和指针#

KiKi学结构体和指针

https://www.nowcoder.com/practice/0ab593ca56b1476eb05b1ff848fd7fcc

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

// 声明插入、删除、销毁链表(内存回收)
void insert(int);
void delete (int);
void destroy(void);

// 仿构造器
struct Node* Node(int, struct Node*);
struct LinkedList* LinkedList(void);

// 链表节点
struct Node {
    int data;
    struct Node* next;
}* node;

// 链表
struct LinkedList {
    int size; // 长度
    struct Node* head; // 头结点
    struct Node* tail; // 尾结点
    void (*insert)(int); // 插入
    void (*delete)(int); // 删除
    void (*destroy)(void); // 销毁链表
}* linkedList;


int main() {
    int n;
    scanf("%d", &n);
    // 链表
    struct LinkedList* linkedList =  LinkedList();
    int data;
    for (int i = 0; i < n; i++) {
        scanf("%d", &data);
        // 插入节点
        linkedList->insert(data);
    }
    // 要删除的节点
    int del;
    scanf("%d", &del);
    linkedList->delete (del);
    // 链表长度
    printf("%d\n", linkedList->size);

    // 遍历链表
    struct Node* tmp = linkedList->head;
    while (tmp != NULL) {
        printf("%d ", tmp->data);
        tmp = tmp->next;
    }

    return 0;
}

// 类似于构造器
struct Node* Node(int data, struct Node* next) {
    node = (struct Node*)malloc(sizeof(struct Node));
    if (node == NULL) {
        perror("Node()");
        return NULL;
    }
    node->data = data;
    node->next = next;
    return node;
}

// 类似于构造器
struct LinkedList* LinkedList(void) {
    linkedList = (struct LinkedList*)malloc(sizeof(struct LinkedList)); 
    if (linkedList == NULL) {
        perror("LinkedList()");
        return NULL;
    }
    linkedList->size = 0;
    linkedList->head = NULL;
    linkedList->tail = NULL;
    linkedList->insert = insert;
    linkedList->delete = delete;
    linkedList->destroy = destroy;
    return linkedList;
}

void insert(int data) {
    struct Node* pNode = Node(data, NULL);
    if (pNode == NULL) {
        perror("insert(int)");
        return;
    }

    if (linkedList->head == NULL) {
        linkedList->head = pNode;
        linkedList->tail = linkedList->head;
    } else {
        linkedList->tail->next = pNode;
        linkedList->tail = pNode;
    }
    linkedList->size++;
}

void delete (int data) {
    struct Node* cur = linkedList->head;
    struct Node* prev = NULL;
    struct Node* del = NULL;
    while (cur) {
        if (cur->data == data) {
            del = cur;
            if (cur == linkedList->head) {
                linkedList->head = cur->next;
                cur = linkedList->head;
            } else {
                prev->next = cur->next;
                cur = cur->next;
            }
            free(del);
            linkedList->size--;
        } else {
            prev = cur;
            cur = cur->next;
        }
    }
}

void destroy(void) {
    struct Node* delNode = NULL;
    struct Node* tmp = linkedList->head;
    while (tmp != NULL) {
        delNode = tmp;
        tmp = tmp->next;
        free(delNode);
    }
    free(linkedList);
    linkedList = NULL;
}

全部评论

相关推荐

投递阿里巴巴控股集团等公司10个岗位 >
点赞 评论 收藏
转发
头像
点赞 评论 收藏
转发
科大讯飞消费者bg二级研究院 语音算法岗 24k*14
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务