【每日算法】AB9 链表

简介: 【每日算法】AB9 链表

题目

描述

请你实现一个链表。
操作:
insert x y:将yy加入链表,插入在第一个值为xx的结点之前。若链表中不存在值为xx的结点,则插入在链表末尾。保证xx,yy为int型整数。
delete x:删除链表中第一个值为xx的结点。若不存在值为xx的结点,则不删除。

输入描述:

第一行输入一个整数n (1≤n≤10^4),表示操作次数。
接下来的n行,每行一个字符串,表示一个操作。保证操作是题目描述中的一种。

输出描述:

输出一行,将链表中所有结点的值按顺序输出。若链表为空,输出"NULL"(不含引号)。

代码

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

struct node {
    int data;
    struct node* next;
} node;

struct node* init() {
    struct node* n = (struct node*)malloc(sizeof(struct node));
    n->next = NULL;
    n->data = -1;
    return n;
}

void insert(struct node* l, int beforeme, int data) {
    struct node* n = (struct node*)malloc(sizeof(struct node));
    n->data = data;
    struct node* tmp = l;
    while (1) {
        if (tmp->next == NULL) {
            tmp->next = n;
            n->next = NULL;
            break;
        } else {
            if (tmp->next->data == beforeme) {
                n->next = tmp->next;
                tmp->next = n;
                break;
            }
            tmp = tmp->next;
        }
    }
}

void delete (struct node* l, int target) {
    struct node* tmp = l;
    while (1) {
        if (tmp->next == NULL) {
            if (tmp->data == target) {
                free(tmp);
            } else {
                break;
            }
        } else if (tmp->next->data == target) {
            struct node* tmpp = tmp->next;
            tmp->next = tmp->next->next;
            free(tmpp);
            break;
        } else {
            tmp = tmp->next;
        }
    }
}

int main() {
    int n;
    scanf("%d", &n);
    struct node* l = init();
    for (int i = 0; i < n; i ++) {
        char op[7];
        scanf("%s", op);
        if (strcmp(op, "insert") == 0) {
            int afterme, data;
            scanf("%d %d", &afterme, &data);
            insert(l, afterme, data);
        } else if (strcmp(op, "delete") == 0) {
            int target;
            scanf("%d", &target);
            delete (l, target);
        }
    }
    struct node* tmp = l->next;
    if (tmp == NULL) {
        printf("NULL\n");
    } else {
        while (tmp != NULL) {
            printf("%d ", tmp->data);
            tmp = tmp->next;
        }
    }
    return 0;
}

问题

关于调用free函数释放内存的问题,之前是delete函数里是这样释放的

        else if (tmp->next->data == target) {
            tmp->next = tmp->next->next;
            free(tmp->next);
            break;
        }

然而此时tmp的next已经更新了,所以需要创建一个指针指向tmp的next,最后释放该指针指向的空间:

        else if (tmp->next->data == target) {
            struct node* tmpp = tmp->next;
            tmp->next = tmp->next->next;
            free(tmpp);
            break;
        }
目录
相关文章
|
2天前
|
算法
【优选算法专栏】专题九:链表--------两两交换链表中的节点
【优选算法专栏】专题九:链表--------两两交换链表中的节点
18 0
|
2天前
|
存储 算法 前端开发
数据结构与算法:双向链表
朋友们大家好啊,在上节完成单链表的讲解后,我们本篇文章来对带头循环双向链表进行讲解
|
2天前
|
算法
【数据结构与算法】题解 | #反转链表#
【数据结构与算法】题解 | #反转链表#
|
2天前
|
算法 索引
【数据结构与算法】5、循环链表、约瑟夫问题、静态链表
【数据结构与算法】5、循环链表、约瑟夫问题、静态链表
39 0
|
2天前
|
算法
算法系列--递归(一)--与链表有关(上)
算法系列--递归(一)--与链表有关
28 0
|
2天前
|
存储 算法 Java
数据结构与算法 数组和链表
数据结构与算法 数组和链表
11 0
|
2天前
|
算法
算法系列--链表刷题(二)(下)
算法系列--链表刷题(二)(下)
18 0
|
2天前
|
算法
算法系列--链表刷题(二)(上)
算法系列--链表刷题(二)
19 0
|
2天前
|
算法
算法系列--递归(一)--与链表有关(下)
算法系列--递归(一)--与链表有关(下)
21 0
|
2天前
|
存储 算法
【优选算法专栏】专题九:链表--------两数之和
【优选算法专栏】专题九:链表--------两数之和
15 0