AcWing 826 单链表 题解 (单链表模板)

本文详细解析AcWing平台上的第826题,通过实例深入讲解单链表的基本操作和解题思路,提供了一份完整的单链表模板,帮助读者巩固链表算法知识。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

const int N = 1e5 + 10;

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

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

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

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

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

int main()
{
	cin>>m;
	init();
	memset(ne, -1, sizeof(ne));
	while(m -- ){
		char op;
		cin>>op;
		if(op == 'H'){
			int x;
			cin>>x;
			add_top(x);
		}
		else if(op == 'D'){
			int k;
			cin>>k;
			if(k == 0) head = ne[head];
			else remove(k - 1);
		}
		else{
			int k, x;
			cin>>k>>x;
			insert(k - 1, x);
		}
	}
	for(int i = head; i != -1; i = ne[i]){
		cout<<e[i]<<" ";
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值