title: 0603-0605读书报告
date: 2019-06-24
tags:
- 队列
- 链表
- luogu
- 《啊哈!算法!》
categories: 算法学习
欢迎访问我的博客,以获得更好的阅读体验Hunter’s Blog
学习内容:队列与链表
队列:
通过数组进行队列操作,会费时间较多如图所示
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VyjJCAAI-1580477428200)(https://i.loli.net/2019/06/07/5cfa181397ae265980.png)]
每次删除会交换位置,时间较长
于是用指针(我喜欢用int)来记录队列首位末尾便可以快速完成
不多说了
链表
顾名思义就是一条链子,有单向、双向、环形;
一般我会这样用
struct queueDat
{
int data;
queueDat *front = NULL, *back = NULL;
}que[100001];
就是一个结构体,然后除了数据以外有两个指针
于是做了道题目
P1160
就是一道模拟+链表
#include <bits/stdc++.h>
using namespace std;
struct queueDat
{
int ID;
queueDat *front = NULL, *back = NULL;
} que[100001];
queueDat *head = &que[1];
void _cut(int ID)
{
queueDat *cut = &que[ID];
if (cut->ID == head->ID)
head = cut->back;
cut = cut->front;
cut->back = cut->back->back;
cut = cut->back;
cut->front = cut->front->front;
}
void _add(int num, int ID, bool back)
{
queueDat *find = &que[ID], *add = &que[num];
if (back)
{
add->front = find;
add->back = find->back;
find->back = add;
find = find->back->back;
find->front = add;
return;
}
else
{
add->back = find;
add->front = find->front;
find = find->front;
find->back = add;
find = find->back->back;
find->front = add;
if (ID == head->ID)
head = add;
}
}
int main()
{
bool inQueue[100001];
memset(inQueue, false, sizeof(inQueue));
inQueue[1] = true;
for (int i = 1; i < 100001; i++)
que[i].ID = i;
que[1].back = &que[1];
que[1].front = &que[1];
int totStudents, a, b;
cin >> totStudents;
for (int i = 2; i <= totStudents; i++)
{
inQueue[i] = true;
cin >> a >> b;
_add(i, a, ((b == 0) ? false : true));
}
cin >> totStudents;
for (int i = 0; i < totStudents; i++)
{
cin >> a;
if (inQueue[a] == true)
{
inQueue[a] = false;
_cut(a);
}
}
b = head->ID;
do
{
cout << head->ID << " ";
head = head->back;
}
while (b != head->ID);
return 0;
}
指针的数据操作一般是->
/’.’
呼~~