洛谷 P1160 【队列安排】

本文介绍了一种手写链表模拟算法,用于解决特定数据结构问题。通过定义链表节点结构,实现节点间的左右连接,以及节点删除操作。文章详细展示了如何构建链表,并在一系列操作后找到链表头部,适用于需要模拟链表行为的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很久以前看到,今天才做。苦于没有满足该性质的数据结构,于是——手工模拟(看了别人的题解,原来这算链表?)

#include<bits/stdc++.h>
using namespace std;
int n,m;//由题
struct node{
	int r;//左边
	int l;//右边
}a[100007];
void dcout(int k){//不断找右边的人
	if(!a[k].r)return;//右边没人了
	cout<<a[k].r<<' ';//输出当前右边的人
	dcout(a[k].r);//找右边的人的右边
}//其实您也可以用递归的方法先找队尾,压栈输出他的左边
int main(){
	cin>>n;
	for(int i=2;i<=n;i++){//注意是第2~n个人
		int k,p;
		cin>>k>>p;//最好按照题目里来
		if(p==0){//在左边
			a[i].r=k;i的右边是k
			a[i].l=a[k].l;i的左边是原来k的左边
			a[a[k].l].r=i;原来k的左边的人,现在的右边是i
			a[k].l=i;k的左边是i
		}
		else {//在右边
			a[i].l=k;//i的左边是k
			a[i].r=a[k].r;//i的右边是原来k的右边
			a[a[k].r].l=i;//原来k的右边的人,现在的左边是i
			a[k].r=i;k的右边是i
		}
	}
	cin>>m;
	for(int i=1;i<=m;i++){
		int x;
		cin>>x;//由题
		a[a[x].l].r=a[x].r;//x的左边的人,现在的右边是原来x的右边
		a[a[x].r].l=a[x].l;//x的右边的人,现在的左边是原来x的左边
		a[x].l=a[x].r=0;//把x的左右两边都删除,不然下面输出不好找最左边的人
	}
	for(int i=1;i<=n;i++)
		if(!a[i].l&&a[i].r){//左边没人了,并且右边还有人,说明这是队头
			cout<<i<<' ';//先把队头输出
			dcout(i);//找他的右边
			break;
		}
	return 0;
}
### P1160 队列安排问题的 Java 实现 对于给定的一系列插入操作,可以构建一个双向链表来模拟学生队列的变化过程。通过维护节点之间的前后指针关系,能够高效地完成插入操作。 #### 双向链表结构定义 为了方便处理边界情况,在初始化时创建虚拟头结点 `dummyHead` 和尾部哨兵节点 `tailSentinel`,使得所有实际的学生节点都位于这两个特殊节点之间: ```java class Node { int id; Node prev, next; public Node(int id) { this.id = id; this.prev = null; this.next = null; } } ``` #### 插入逻辑实现 根据题目描述中的输入格式,针对每一对 `(k, p)` 参数执行相应的左插或右插动作: ```java import java.util.*; public class QueueArrangement { static class Node { int id; Node prev, next; public Node(int id) { this.id = id; this.prev = null; this.next = null; } } private static void insert(Node target, int position, List<Node> nodes) { Node newNode = new Node(position); if (target == null || target.next == null){ // Insert at the end of list when k is invalid or reaching tail sentinel. Node lastNode = nodes.get(nodes.size() - 2); // Exclude dummyTail lastNode.next = newNode; newNode.prev = lastNode; } else { if (position == 0) { // Special case for inserting before first element target.next = newNode; newNode.prev = target; } else { if ((newNode.id > target.id && position == 1) || (newNode.id < target.id && position == 0)){ // Normal insertion based on direction specified by 'p' Node tempNext = target.next; target.next = newNode; newNode.prev = target; newNode.next = tempNext; if(tempNext != null) tempNext.prev = newNode; } } } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine().trim()); List<Node> nodeList = new ArrayList<>(); Node dummyHead = new Node(-2); nodeList.add(dummyHead); nodeList.add(tailSentinel); dummyHead.next = tailSentinel; tailSentinel.prev = dummyHead; while (--n >= 0) { String line = scanner.nextLine(); String[] parts = line.split(" "); int studentIndex = Integer.parseInt(parts[0]); int direction = Integer.parseInt(parts[1]); Node currentTarget = findNodeById(studentIndex, nodeList); insert(currentTarget, direction, nodeList); System.out.println(getQueueOrder(nodeList)); } } private static Node findNodeById(int id, List<Node> nodeList) { return nodeList.stream() .filter(node -> node.id == id) .findFirst() .orElse(null); } private static String getQueueOrder(List<Node> nodeList) { StringBuilder sb = new StringBuilder(); for (int i = 1; i < nodeList.size()-1 ;i++) { sb.append(nodeList.get(i).id).append(' '); } return sb.toString().trim(); } } ``` 此代码片段实现了基于命令序列调整队列顺序的功能,并且每次修改后输出当前队伍成员编号列表[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值