AcWing单链表数据结构做法及AC做法

题目

826. 单链表

数据结构做法

#include <cstdio>
#include <malloc.h>

typedef struct LNode* PtrLNode;
struct LNode
{
    int x;
    int k;
    PtrLNode next;
}LNode;

int main()
{
    int n;
    scanf("%d", &n);
    PtrLNode head = (PtrLNode)malloc(sizeof(LNode));
    head -> k = 0;
    char c;
    int k, x, cnt = 1;
    for (int i = 1; i <= n; i ++ )
    {
        getchar();
        scanf("%c", &c);
        if (c == 'H')
        {
            scanf("%d", &x);
            PtrLNode node = (PtrLNode)malloc(sizeof(LNode));
            node -> x = x;
            node -> k = cnt;
            node -> next = head -> next;
            head -> next = node;
            cnt ++ ;
        }
        else if (c == 'I')
        {
            scanf("%d %d", &k, &x);
            PtrLNode node = head;
            while (node != NULL)
            {
                if (node -> k == k)
                {
                    PtrLNode newNode = (PtrLNode)malloc(sizeof(LNode));
                    newNode -> x = x;
                    newNode -> k = cnt;
                    newNode -> next = node -> next;
                    node -> next = newNode;
                    break;
                }
                node = node -> next;
            }
            cnt ++ ;
        }
        else if (c == 'D')
        {
            scanf("%d", &k);
            PtrLNode node = head;
            while (node != NULL)
            {
                if (node -> k == k)
                {
                    node -> next = node -> next -> next;
                    break;
                }
                node = node -> next;
            }
        }
    }
    
    PtrLNode node = head -> next;
    while (node != NULL)
    {
        printf("%d ", node -> x);
        node = node -> next;
    }
    return 0;
}

这个方法会超时。

AC做法

#include <cstdio>

const int N = 100010;

int idx, e[N], ne[N], head;

void init()
{
    head = -1;
    idx = 1;
}

void add_to_head(int x)
{
    e[idx] = x;
    ne[idx] = head;
    head = idx ++ ;
}

void add(int k, int x)
{
    e[idx] = x;
    ne[idx] = ne[k];
    ne[k] = idx ++ ;
}

void remove(int k)
{
    ne[k] = ne[ne[k]];
}

int main()
{
    int n;
    scanf("%d", &n);
    char c;
    int k, x;
    init();
    for (int i = 1; i <= n; i ++ )
    {
        getchar();
        scanf("%c", &c);
        if (c == 'H')
        {
            scanf("%d", &x);
            add_to_head(x);
        }
        else if (c == 'I')
        {
            scanf("%d %d", &k, &x);
            add(k, x);
        }
        else if (c == 'D')
        {
            scanf("%d", &k);
            if (k == 0)
                head = ne[head];
            else
                remove(k);
        }
    }
    
    for (int i = head; ~ i; i = ne[i])
        printf("%d ", e[i]);
    return 0;
}

其实就是用数组模拟了链表。

ACwing是一个在线的程序设计竞赛训练平台,提供了丰富的算法题目和解题思路。在ACwing上,数据结构是其中一个重要的学习内容。根据引用内容,我们可以得出以下观点。 首先,数据结构在解决问题时起到了至关重要的作用。STL(Standard Template Library,标准模板库)是一种常用的C++库,其中包含了各种数据结构和算法。然而,STL不一定能够满足所有的需求,有些问题可能需要使用数组来解决。因此,学习数组的方法对于实现各种数据结构是非常重要的。 其次,使用结构体和指针来创建数据结构节点时,每次创建一个新节点都需要执行new Node()操作,这个操作可能会比较慢。特别是在处理大量数据的情况下,频繁的new操作可能导致运行时间超时。因此,在设计数据结构时需要考虑到运行效率的问题。 最后,在比赛中,通常没有进行O2优化。在这种情况下,纯STL可能会比数组模拟的数据结构稍慢一些。这是因为STL包含了更多的细节和抽象,而数组模拟的数据结构更为直接和简单。然而,在实际应用中,选择使用哪种数据结构还是要根据具体问题的需求和性能要求来决定。 综上所述,ACwing数据结构包含了使用STL和数组等不同的方法来实现各种数据结构。根据具体问题的需求和性能要求,选择合适的数据结构方法是很重要的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【AcWing 算法基础课】 2、数据结构 笔记](https://blog.csdn.net/qq_40915831/article/details/124761243)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值