Study notes: double linked list (array simulation)

AcWing 827. Double Linked List

Realize a double-linked list. The double-linked list is initially empty and supports 5 operations:

(1) Insert a number on the leftmost side;

(2) Insert a number on the far right;

(3) Delete the k-th inserted number;

(4) Insert a number to the left of the k-th inserted number;

(5) Insert a number to the right of the k-th inserted number

Now we need to perform M operations on the linked list. After all operations are completed, the entire linked list is output from left to right.

Note: The k-th inserted number in the title does not refer to the k-th number in the current linked list. For example, a total of n numbers are inserted during the operation. According to the time sequence of insertion, the n numbers are: the first inserted number, the second inserted number, ... the nth inserted number.

Input format The
first line contains the integer M, which represents the number of operations.

The next M lines, each line contains an operation command, the operation commands may be the following:

(1) "L x" means inserting the number x at the left end of the linked list.

(2) "R x" means that the number x is inserted at the right end of the linked list.

(3) "D k" means deleting the k-th inserted number.

(4) "IL kx" means to insert a number to the left of the k-th inserted number.

(5) "IR kx" means to insert a number to the right of the k-th inserted number.

The output format is
one line, and the entire linked list is output from left to right.

Data range
1≤M≤100000
All operations are guaranteed to be legal.

#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define endl '\n'
#include<algorithm>
using namespace std;

inline ll read()
{
    
    
	ll x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9')	{
    
     if(ch=='-') f=-1; ch=getchar(); }
	while('0'<=ch&&ch<='9')	x=x*10+ch-'0', ch=getchar();
	return f*x;
}

const int N = 1e5+5;

//l左指针, r右指针 
int idx,e[N],l[N],r[N];

void init()
{
    
    
	r[0]=1; l[1]=0; //0为左端点,1为右端点 
	idx=2; //编号从2开始 
}

void insert(int k,int x) //在节点 k的右边插入一个数 x
{
    
    
	e[idx]=x; l[idx]=k; r[idx]=r[k]; // idx的左边指向  k,右边指向 k 向右指向的位置 
	l[r[k]]=idx; // k右边的节点向左指向idx 
	r[k]=idx++; // k向右指向idx 
}

void remove(int k) //删除 k节点 
{
    
    
	l[r[k]]=l[k]; //k右边的节点向左指向 k向左指向的位置 
	r[l[k]]=r[k]; //k左边的节点向右指向 k向右指向的位置 
}

int main()
{
    
    
	init();
	int m;	cin>>m;
	string op; int x,k;
	while(m--)
	{
    
    
		cin>>op;
		if(op=="L")
		{
    
    
			cin>>x;
			insert(0,x);
		}
		else if(op=="R")
		{
    
    
			cin>>x;
			insert(l[1],x);
		}
		else if(op=="D")
		{
    
    
			cin>>k;
			remove(k+1); //因为编号idx是从2开始的 
		}
		else if(op=="IL")
		{
    
    
			cin>>k>>x;
			insert(l[k+1],x); //在 k节点的左侧插入x 相当于 在 k左边节点的右侧插入x 
		}
		else if(op=="IR")
		{
    
    
			cin>>k>>x;
			insert(k+1,x);
		}
	}
	for(int i=r[0];i!=1;i=r[i])	cout<<e[i]<<" ";cout<<endl; // 从左到右输出
//	for(int i=l[1];i!=0;i=l[i])	cout<<e[i]<<" ";cout<<endl; // 从右到左输出
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_50815157/article/details/113485719