20165306 Homework (Week 10)

Homework (Week 10)

Summary of relevant knowledge points

linked list

  • A linked list is a data structure composed of several objects called nodes.
  • Singly linked list: Each node contains a data and a reference to the next node.
  • Doubly linked list: Each node contains a data and contains a reference to the previous node and a reference to the next node.

LinkedList

  • LinkedList in java.util package

LinkedList<String> mylist=new LinkedList<String>();

  • Create an empty doubly linked list. Using LinkedList
  • Nodes are automatically linked together, and no manipulation is required to arrange references to the next or previous node stored in the node.

Common method

  • public void add(int index,E element)Add a new node to the specified position of the linked list, the data in this node is the data specified by the parameter element.

  • public E remove(int index)Deletes the node at the specified position.

  • public E get(int index)Get the data in the node at the specified position in the linked list.

  • public int size()Returns the length of the linked list, that is, the number of nodes.

  • The method of iterator traversing a collection not only finds an object in the collection, but also obtains a reference to the successor object to be traversed.

  • A linked list object can use the iterator() method to obtain an Iterator object, which is an iterator for the current linked list.

Sort and Find

  • public static sort(List<E> list)This method sorts the elements in the list in ascending order.

  • int binarySearch(List<T>list,T key,CompareTo<T>c)Use the halving method to find whether the list contains an element equal to the parameter key. If the key is equal to an element in the linked list, the method returns the index position of the element equal to the key in the linked list (the index position of the linked list starts from 0), otherwise it returns - 1.

  • The sort method in the Collections class provided by Java is designed for the Comparable interface.

  • In data structures and algorithms, sorting is a very important operation. To make a class can be sorted, there are two ways:
    • There is a source code of a class, sort a member variable, let the class implement the Comparable interface, and call Collection.sort(List)
    • If there is no source code of the class, or multiple sorts, create a new class, implement the Comparator interface, and call Collection.sort(List, Compatator)

    the difference:

    1.排序规则实现的方法不同:
    
        Comparable接口的方法:compareTo(Object o)
    
        Comparator接口的方法:compare(To1, To2)
    
    2.类设计前后不同:
    
        Comparable接口用于在类的设计中使用;
    
        Comparator接口用于类设计已经完成,还想排序(Arrays);

Summary: It is simple to use Comparable, as long as the object that implements the Comparable interface becomes a comparable object directly, but the source code needs to be modified; the advantage of using Comparator is that there is no need to modify the source code, but to implement a comparator. When the defined object needs to be compared, the comparator and the object can be passed together to compare the size, and in the Comparator, the user can implement complex and general logic, so that it can match some simpler objects, so that A lot of repetitive work can be saved. ( Reference )

Supplementary course content

(1) Data structure - sorting

Requirements: For the following Student class, use Comparator programming to complete the following functions:

  1. Create a new student list in the test class StudentTest, including two students before and after oneself and student number, a total of 5 students, and give the running results (before sorting, after sorting)

  2. Sort these 5 students in increasing order by student number and total score, and submit the codes of two Comparators

class Student {
    private String id;//表示学号
    private String name;//表示姓名
    private int age;//表示年龄
    private double computer_score;//表示计算机课程的成绩
    private double english_score;//表示英语课的成绩
    private double maths_score;//表示数学课的成绩
    private double total_score;// 表示总成绩
    private double ave_score; //表示平均成绩

    public Student(String id, String name){
        this.id = id;
        this.name = name;
}
    public Student(String id, String name, char sex, int age){
        this(id, name);
        this.sex = sex;
        this.age = age;
}
    public String getId(){
        return id;
}//获得当前对象的学号,
    public double getComputer_score(){
        return computer_score;
}//获得当前对象的计算机课程成绩,
    public double getMaths_score(){
        return maths_score;
}//获得当前对象的数学课程成绩,
    public double getEnglish_score(){
        return english_score;
}//获得当前对象的英语课程成绩,

    public void setId(String id){
        this.id=id;
}// 设置当前对象的id值,
    public void setComputer_score(double computer_score){
        this.computer_score=computer_score;
}//设置当前对象的Computer_score值,
    public void setEnglish_score(double english_score){
        this.english_score=english_score;
}//设置当前对象的English_score值,
    public void setMaths_score(double maths_score){
        this.maths_score=maths_score;
}//设置当前对象的Maths_score值,

    public double getTotalScore(){
        return computer_score+maths_score+english_score;
}// 计算Computer_score, Maths_score 和English_score 三门课的总成绩。
    public double getAveScore(){
        return getTotalScore()/3;
}// 计算Computer_score, Maths_score 和English_score 三门课的平均成绩。

}

class Undergraduate extends Student{
    private String classID;

    public Undergraduate(String id, String name, char sex, int age,String classID){
        super(id,name,sex,age);
        this.classID=classID;
    }
    public String getClassID(){
        return classID;
    }
    public void setClassID(String classID){
        this.classID=classID;
    }
}

code link

(2) Data structure - singly linked list

Requirements: See the attachment, supplement the content of MyList.java, and submit a screenshot of the running result (full screen).

public class MyList {
    public static void main(String [] args) {
        //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
        
        
        //把上面四个节点连成一个没有头结点的单链表
        
        //遍历单链表,打印每个结点的

        //把你自己插入到合适的位置(学号升序)

        //遍历单链表,打印每个结点的

        //从链表中删除自己

        //遍历单链表,打印每个结点的
    }
}


public class Node<T>                             //单链表结点类,T指定结点的元素类型
{
    public T data;                               //数据域,存储数据元素
    public Node<T> next;                         //地址域,引用后继结点

    public Node(T data, Node<T> next)            //构造结点,data指定数据元素,next指定后继结点
    {
        this.data = data;                        //T对象引用赋值
        this.next = next;                        //Node<T>对象引用赋值
    }
    public Node()
    {
        this(null, null);
    }
    public String toString()                     //返回结点数据域的描述字符串
    {
        return this.data.toString();
    }
}

code link

Textbook Chapter 15 Code Analysis

public class Cone<E>{
    double height;
    E bottom;   //用泛型类E声明对象bottom
    public Cone(E b){
        bottom=b;
    }
}
import java.util.*;
public class E153 {
        public static void main(String args[]) {
            LinkedList mylist = new LinkedList();
            mylist.add("你");                 //链表中的第一个节点
                mylist.add("好");                 //链表中的第二个节点
            int number = mylist.size();         //获取链表的长度
            for (int i = 0; i < number; i++) {
                String temp = (String) mylist.get(i); //必须强制转换取出的数据
                System.out.println("第" + i + "节点中的数据:" + temp);
            }
            Iterator iter = mylist.iterator();
            while (iter.hasNext()) {
                String te = (String) iter.next();  //必须强制转换取出的数据
                System.out.println(te);
            }
        }
}
public int compareTo(object b){
    Student st=(Student)b;
    if((this.english-st.English)==0)
        return 1;    //此时允许出现大小相等的两个结点
    else
        return (this.english-st.english);
}

Make up for the programming topics in Chapter 15 of the textbook

1. Use the stack structure to output several items of an, where an=2an-1+2an-2, a1=3, a2=8.

code link

2. Write a program to store the students' English transcripts in the linked list into a tree set, so that the results are automatically sorted and the sorting results are output.

code link

3. There are 10 U disks with two important attributes: price and capacity. Write an application that uses TreeMap

code link

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325511438&siteId=291194637