数据结构--链表+list

 

一、list 定义和初始化

             只需要简单的 list<TYPE> my_list; 就可以完成对一个 list 的定义了。不需要 new。

             初始化的话就要用到 list 的构造函数。
             一个简单的例子是:

  int myints[] = {75,23,65,42,13};
  list<int> mylist (myints, myints+5);

二、list 函数介绍

迭代器

函数名作用
begin将迭代器返回到开头(Return iterator to beginning)
end将迭代器返回到最后(Return iterator to end)
rbeginReturn reverse iterator to reverse beginning
rendReturn reverse iterator to reverse end

容量

函数名作用
empty检查容器是否为空
size返回当前容器内元素个数
max_size返回当前容器能容纳的最大元素数量

元素访问

函数名作用
front访问第一个元素
back访问最后一个元素

更改 list

函数名作用
assignAssign new content to container
push_front将元素插入到开头
pop_front删除第一个元素
push_back将元素插入到最后
pop_back删除最后一个元素
insert插入元素
erase删除元素
swap交换两个 list 内容
resize改变容器大小
clear删除容器所有内容

操作

函数名作用
splice合并两个 list
remove根据值删除元素
remove_if删除满足条件的元素
unique删除重复的值
merge合并排好序的 list
sort对容器内的元素排序
reverse将元素反序

三、函数举例

assign()

#include <iostream>
#include <list>
using namespace std;

template <class T>
void print_list(list<T> my_list)
{
	for (typename list<T>::iterator it = my_list.begin(); it != my_list.end(); ++it)
		cout << ' ' << *it;

	cout << '\n';
} 

int main ()
{
	list<int> first;
	list<int> second;

	first.assign (7, 100);                      // 7 ints with value 100
	print_list(first);
	// 100 100 100 100 100 100 100

	second.assign (first.begin(),first.end()); // a copy of first
	print_list(second);
	// 100 100 100 100 100 100 100

	int myints[]= {1776, 7, 4};
	first.assign (myints, myints+3);            // assigning from array
	print_list(first);
	// 1776 7 4

	cout << "Size of first: " << int (first.size()) << '\n';
	cout << "Size of second: " << int (second.size()) << '\n';
	// Size of first: 3
	// Size of second: 7
	
	return 0
}

begin() —— 对 list 进行顺序遍历(同end())

#include <iostream>
#include <list>
using namespace std;

int main ()
{
	int myints[] = {75,23,65,42,13};
	list<int> mylist (myints,myints+5);

	cout << "mylist contains:";
	for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
		std::cout << ' ' << *it;

	cout << '\n';
	// mylist contains: 75 23 65 42 13
	return 0;
}

erase()——动态删除

#include <iostream>
#include <list>
using namespace std;

int main ()
{
	list<int> mylist;
	list<int>::iterator it1,it2;

	// set some values:
	for (int i=1; i<10; ++i) mylist.push_back(i*10);

								// 10 20 30 40 50 60 70 80 90
	it1 = it2 = mylist.begin(); // ^^
	advance (it2,6);            // ^                 ^
	++it1;                      //    ^              ^

	it1 = mylist.erase (it1);   // 10 30 40 50 60 70 80 90
								//    ^           ^

	it2 = mylist.erase (it2);   // 10 30 40 50 60 80 90
								//    ^           ^

	++it1;                      //       ^        ^
	--it2;                      //       ^     ^

	mylist.erase (it1,it2);     // 10 30 60 80 90
								//        ^

	cout << "mylist contains:";
	for (it1=mylist.begin(); it1!=mylist.end(); ++it1)
		cout << ' ' << *it1;
	cout << '\n';

	return 0;
}

insert()——插入元素

#include <iostream>
#include <list>
#include <vector>
using namespace std;

int main ()
{
	list<int> mylist;
	list<int>::iterator it;

	// set some initial values:
	for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5

	it = mylist.begin();
	++it;       // it points now to number 2           ^

	mylist.insert (it,10);                        // 1 10 2 3 4 5

	// "it" still points to number 2                      ^
	mylist.insert (it,2,20);                      // 1 10 20 20 2 3 4 5

	--it;       // it points now to the second 20            ^

	vector<int> myvector (2,30);
	mylist.insert (it,myvector.begin(),myvector.end());
	// 1 10 20 30 30 20 2 3 4 5
	//               ^
	cout << "mylist contains:";
	for (it=mylist.begin(); it!=mylist.end(); ++it)
		cout << ' ' << *it;
	cout << '\n';
	// mylist contains: 1 10 20 30 30 20 2 3 4 5
	
	return 0;
}

四、例题

https://www.luogu.com.cn/problem/P1160

#include <cstdio>
#include <list>

using namespace std;

using Iter = list<int>::iterator;

const int maxN = 1e5 + 10;
Iter pos[maxN];
list<int> queList;
bool erased[maxN];
int N;

void buildQueue()
{
  queList.push_front(1);
  pos[1] = queList.begin();

  for (int i = 2; i <= N; i++)
  {
      int k, p;
      scanf("%d%d", &k, &p);
      if (p == 0)
      {
          pos[i] = queList.insert(pos[k], i); //left
      }
      else
      {
          auto nextIter = next(pos[k]);
          pos[i] = queList.insert(nextIter, i); //right
      }
  }
  int M;
  scanf("%d", &M);
  for (int x, i = 1; i <= M; i++)
  {
      scanf("%d", &x);
      if (!erased[x])
      {
          queList.erase(pos[x]);
      }
      erased[x] = true;
  }
}

int main()
{
  scanf("%d", &N);
  buildQueue();
  bool first = true;
  for (int x: queList)
  {
      if (!first)
          putchar(' ');
      first = false;
      printf("%d", x);
  }
  putchar('\n');
  return 0;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
    int L, R;
}a[100003];
int n, m;
inline void addRight(int x, int pos) { //插入右边 
    a[x].L = pos;
    a[a[pos].R].L = x;
    a[x].R = a[pos].R;
    a[pos].R = x;
}
inline void addLeft(int x, int pos) { //插入左边
    a[x].R = pos;
    a[a[pos].L].R = x;
    a[x].L = a[pos].L;
    a[pos].L = x;
}
inline void del(int x) {
    if(a[x].L == -1) return;
    a[a[x].L].R = a[x].R;
    a[a[x].R].L = a[x].L;
    a[x].L = -1;
    a[x].R = -1; 
}
inline void go() {
    int x = a[0].R;
    while(1) {
        cout<<x<<" ";
        if(a[x].R == -1) break;
        x = a[x].R;
    }
}
inline void init() {
    for(int i = 1; i <= n; ++i) a[i].L = a[i].R = -1;
    a[1].R = -1; a[1].L = 0; a[0].R = 1;
}
int main() {
    scanf("%d", &n);
    int cmd1, cmd2;
    init();
    for(int i = 2; i <= n; ++i) {
        scanf("%d %d", &cmd1, &cmd2);
        if(!cmd2) addLeft(i, cmd1);
        else addRight(i, cmd1);
    }
    scanf("%d", &m);
    for(int i = 1; i <= m; ++i) {
        scanf("%d", &cmd1);
        del(cmd1);
    }
    go();
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值