链表训练题——小王子单链表

题目描述

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

要求一:采用单链表解决

输入描述

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

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

输出描述

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

输入输出样例

示例 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

运行限制

  • 最大运行时间:1s
  • 最大运行内存: 128M

分析过程:

结点定义:

struct Node
{
    int data;
    Node *next;
}

构建链表

Node *head = new Node(); // 生成头结点

void init() {
	
	head->next = NULL; // 形成空链表,最后一个结点指针为空
	
	for (int i = 10; i >=1 ; i--) {
		Node *temp = new Node;
		temp->data = i;
		
		temp->next = head->next;
		head->next = temp;
	} 
}

编写插入函数

void insert(int x) {
	Node *temp = new Node; // 新建一个结点
	temp->data = x;
	
	temp->next = head->next;
	head->next = temp;
}

编写删除函数

void del(int x) {
	Node *Befor = new Node;
	for (Node *T = head->next; T != NULL; T = T->next) {
		if (T->data == x) {
			Node *temp = T;

			Befor->next = T->next;

			delete temp;

			return;
		}
		Befor = T;
	}
}

编写输出函数

void show(int i) {
	// cout << "这是第" << i << "次操作";
	for ( Node *T = head->next; T != NULL; T = T->next;) {
		cout << T -> data << " ";
	}
	cout << endl;
}

编写主函数

int main()
{

    init();
   // show(0);
    int N;
    cin >> N;
    for (int i = 1; i <= N; i++)
    {
        int x;
        cin >> x;
        del(x);
        insert(x);
        show(i);
    }
}

完整代码

  • C++代码
#include <iostream>
using namespace std;

struct Node {
	int data;
	Node *next;
};

Node *head = new Node(); // 生成头结点

void init() {
	
	head->next = NULL; // 形成空链表,最后一个结点指针为空
	
	for (int i = 10; i >=1 ; i--) {
		Node *temp = new Node;
		temp->data = i;
		
		temp->next = head->next;
		head->next = temp;
	} 
} 

void insert(int x) {
	
	Node *temp = new Node;
	temp->data = x;
	
	temp->next = head->next;
	head->next = temp;
}

void del(int x) {
	Node *Befor = head;
	for (Node *T = head->next; T != NULL; T = T->next) {
		if (T->data == x) {
			Node *temp = T;
			
			Befor->next = T->next;
			
			delete T;
			return;
		}
		Befor = T;
	}
}

void show(int i) {
	//cout << "这是第" << i << "次操作";
	for (Node *T = head->next; T != NULL; T = T->next) {
		cout << T->data << " ";
	}
	cout << endl;
}


int main() {
	init();
	//show(0);
	int N;
	cin >> N;
	for (int i = 1; i <= N; i++) {
		int x;
		cin >> x;
		del(x);
		insert(x);
		show(i);
	}
}
  • java代码
import java.util.Scanner;
public class Main {
    static class Node {
        int data;
        Node next;
        Node(int v) {
            data = v;
        }
    }//成员类,代表节点,类似于C++语言中的结构体
    static Node head = new Node(1);//头节点单列出来
    static void init() {
        Node x = head;
        for (int i = 1; i <= 10; i++) x = (x.next = new Node(i));//建立单向链表
        x.next = null;
    }
    static   void del(int x) {
        Node Befor = head;                               
        for (Node T = head.next; T != null; T = T.next) //链表的遍历常用写法
        {
            if (T.data == x) //找到要的那个数了
            {
                Node temp = T; //先临时保存结点
                Befor.next = T.next; //将节点从链表上摘除
                return; //删除结束后,结束函数。
            }
            Befor = T; //前驱改变
        }
    }
    static void insert(int x) {
        Node temp = new Node(x);
        temp.next = head.next;
        head.next = temp;
    }
    static void show(int i) {
        //  System.out.println("这是第" + i + "次操作");//提交代码时删掉这一行
        for (Node T = head.next; T != null; T = T.next) //链表的遍历常用写法
        {
            System.out.print(T.data + " ");
        }
        System.out.println(" ");
    }
    public static void main(String[] args) {
        int N;
        Scanner in = new Scanner(System.in);
        init();
        N = in.nextInt();
        // show(0);//提交代码时删掉这一行
        for (int i = 0; i < N; i++) {
            int x = in.nextInt();
            del(x);
            insert(x);
            show(i);
        }
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

java小豪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值