蓝桥杯备赛题 <小王子单链表>

题目描述

小王子有一天迷上了排队的游戏,桌子上有标号为 1-101−10 的 1010 个玩具,现在小王子将他们排成一列,可小王子还是太小了,他不确定他到底想把那个玩具摆在哪里,直到最后才能排成一条直线,求玩具的编号。已知他排了 MM 次,每次都是选取标号为 XX 个放到最前面,求每次排完后玩具的编号序列。

要求一:采用单链表解决

输入描述

第一行是一个整数 M,表示小王子排玩具的次数。

随后 M 行每行包含一个整数 X,表示小王子要把编号为 X 的玩具放在最前面。

输出描述

共 M 行,第 i 行输出小王子第 ii 次排完序后玩具的编号序列。

输入输出样例

示例 1

输入

5
3
2
3
4
2

输出

3 1 2 4 5 6 7 8 9 10
2 3 1 4 5 6 7 8 9 10
3 2 1 4 5 6 7 8 9 10
4 3 2 1 5 6 7 8 9 10
2 4 3 1 5 6 7 8 9 10

 很基础的一道题,用c写可锻炼下链表的底层操作:创建、插入、删除等等

这里我直接用c++写,使用容器list,很方便

关键点是记住一些拼写 还有在push时记得要是l1.push    在写的时候忘记写前面的l1导致报错

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

void listinit()
{
    for(int i=1;i<=10;i++)
    {
          l1.push_back(i);
    }
}

void show( list<int> &l1)
{
      for(list<int>::iterator it=l1.begin();it !=l1.end();it++)
    {
        cout<<(*it)<<" ";
    }
  cout<<endl;

}

int main()
{
  listinit();
  int M=0;
  int X=0;
  cin >> M;
  for(int j=1;j<=M;j++)
  {
    cin >> X;         //先找到这个编号,然后删了,使用earse
    for(list<int>::iterator it=l1.begin();it != l1.end() ; it++)
    {
      if(*it==X){
        l1.erase(it);
        break;
      }
    }
    l1.push_front(X);   //删了后头插,顺便打印输出
    show(l1);
  }
  // 请在此输入您的代码
  return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
小王子单链表是一种基于Python语言实现的数据结构,它是一种线性表的存储结构,由一系列节点组成,每个节点包含两部分:数据域和指针域。数据域用于存储具体的数据,指针域用于指向下一个节点。 在小王子单链表中,每个节点只能指向下一个节点,最后一个节点的指针域为空。这样的设计使得在插入、删除等操作时,只需要修改相邻节点的指针域,而不需要移动其他节点,提高了操作效率。 下面是一个简单的小王子单链表的Python实现示例: ```python class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def is_empty(self): return self.head is None def append(self, data): new_node = Node(data) if self.is_empty(): self.head = new_node else: current = self.head while current.next: current = current.next current.next = new_node def insert(self, data, position): if position <= 0: new_node = Node(data) new_node.next = self.head self.head = new_node else: current = self.head count = 0 while current and count < position - 1: current = current.next count += 1 if current: new_node = Node(data) new_node.next = current.next current.next = new_node def delete(self, data): if self.is_empty(): return if self.head.data == data: self.head = self.head.next else: current = self.head while current.next and current.next.data != data: current = current.next if current.next: current.next = current.next.next def display(self): current = self.head while current: print(current.data, end=" ") current = current.next print() # 示例代码 linked_list = LinkedList() linked_list.append(1) linked_list.append(2) linked_list.append(3) linked_list.insert(4, 1) linked_list.delete(2) linked_list.display() ``` 以上是一个简单的小王子单链表的Python实现示例,包括了常用的插入、删除和显示操作。你可以根据自己的需求进行扩展和修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mc10141222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值