【PTA brush questions】Single linked list

Please add image description
Personal business card:

blogger: Alcoholics ᝰ.
Personal profile: Indulge in wine, and strive for a future with a drink.
column: PTA exercises and analysis
introduce: Recorded the topic of the blogger's practice questions in pta

Please add image description

one,Basic operations of singly linked list

1. Topics

Please write a program to implement basic algorithms such as inserting and deleting nodes in a singly linked list. Given a singly linked list and a sequence of operations for inserting and deleting nodes, output the linked list after the above operations are performed. The value of the singly linked list data field is an integer.

Input format:
input the first line is a positive integer n, which represents the current singly linked list length; the second line is an integer separated by n spaces, which is the data field value of the n elements of the linked list. The third line is a positive integer m, which represents the number of operations applied to the linked list; the next m lines, each line represents an operation, which is 2 or 3 integers in the format of 0 kd or 1 k. 0 kd means inserting a node whose data field value is d after the k-th node of the linked list. If k=0, it means the header is inserted. 1 k means to delete the k-th node in the linked list, and k cannot be 0 at this time. Note: If there are illegal operations in the operation sequence (such as deleting the 8th node, deleting the 0th node, etc. in a linked list of length 5), the operation will be ignored. n and m do not exceed 100000.

Output format:
The output is a row of integers, representing the linked list after implementing the above m operations, with a space after each integer. The input data ensures that the result list is not empty.

Input sample:

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

Sample output:

7 1 2 8 3 5

Code Length Limit 16 KB
Time Limit 5000 ms
Memory Limit 256 MB

2. Code

import java.util.LinkedList;
import java.util.Scanner;


public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        LinkedList<Integer> list = new LinkedList<>();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
    
    
            list.add(sc.nextInt());
        }
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
    
    
            int num = sc.nextInt();
            if (num == 0){
    
    
                int k = sc.nextInt();
                int d = sc.nextInt();
                if (k <= list.size()){
    
    
                    list.add(k, d);
                }
            }else if (num == 1){
    
    
                int k = sc.nextInt();
                if (k <= list.size() && k != 0){
    
    
                    list.remove(k-1);
                }
            }
        }
        for (Integer node : list) {
    
    
            System.out.print(node + " ");
        }
    }
}

two,Basic application of singly linked list – inversion of singly linked list

1. Topics

To implement a simple student information management system by programming, use self-defined functions according to the following steps:
(1) Create a singly linked list according to the input information and output all node information. The information of each student includes name and grade;
(2) Invert the singly linked list created in the previous step and output all node information.

Input format:
Create a singly linked list based on several input student information. Each row represents a student information, ending with a grade of -1 as input.
Output format:
each student information occupies one line, the name and the grade are separated by a space, and the grade is kept to one decimal place.
Create a linked list and output plus prompt information: Original
inversion and output plus prompt information: Reversed

Sample Input:
Here is a set of inputs. E.g:

Wang 87.3
Wang 60.1
Wang 89.5
Li 93.2
Fu 87.5
Wang 78.6
Cheng 89.1
Tang 71.9
Wang 63.1
Fang 81.9
tt -1

Sample output:
The corresponding output is given here. E.g:

Original:
Wang 87.3
Wang 60.1
Wang 89.5
Li 93.2
Fu 87.5
Wang 78.6
Cheng 89.1
Tang 71.9
Wang 63.1
Fang 81.9

Reversed:
Fang 81.9
Wang 63.1
Tang 71.9
Cheng 89.1
Wang 78.6
Fu 87.5
Li 93.2
Wang 89.5
Wang 60.1
Wang 87.3

Code Length Limit 16 KB
Time Limit 400 ms
Memory Limit 64 MB

2. Code

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        List<String> stu = new ArrayList<>();
        List<Double> sco = new ArrayList<>();
        int i = 0;
        double num = 0;
        while(num != -1){
    
    
            stu.add(sc.next());
            sco.add(sc.nextDouble());
            num = sco.get(i);
            i++;
        };
        System.out.println("Original:");
        for (int j = 0; j < stu.size() - 1; j++) {
    
    
            System.out.println(stu.get(j) + " " + sco.get(j));
            if (j == stu.size() - 2){
    
    
                System.out.println();
            }
        }
        System.out.println("Reversed:");
        for (int j = stu.size() - 2; j >= 0; j--) {
    
    
            System.out.println(stu.get(j) + " " + sco.get(j));
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_65144570/article/details/127097662