20165233 Make up after class for the tenth week

20165233 Make up after class for the tenth week

Summary of relevant knowledge points & supplementary code and results

Lesson 2

  • In data structures and algorithms, there are two important sorting methods:

  • 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 sorting, create a new class, implement the Comparator interface and call Collection.sort(List, Compatator)

  • Create an empty linked list

    List<Student> list = new LinkedList<Student>();
  • Add a new node to the linked list

    list.add(new Student(5233,"张雨昕","female",20));
  • Code

  • Results screenshot

Lesson 3

  • For the creation of a singly linked list and the addition of new nodes , see the summary of knowledge points in the previous exercise.

  • Connect the nodes into a singly linked list without a head node: the nodes in the linked list are automatically linked together, and we do not need to link, that is, there is no need to operate the next or previous stored in the arrangement node A reference to a node.

  • Traverse the linked list, using iterators

    Iterator<String> iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
  • Sort by student number in ascending order: Using the public static sort(List<E>list)method, you can sort the elements in the list in ascending order.

  • delete node

    list.remove("20165233");
  • Code

    import java.util.*;
    public class MyList {
    public static void main(String [] args) {
        List<String> list=new LinkedList<String>();
        list.add("20165231");
        list.add("20165232");
        list.add("20165234");
        list.add("20165235");
        System.out.println("打印初始链表");
        //把上面四个节点连成一个没有头结点的单链表
        Iterator<String> iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍历单链表,打印每个结点的
        list.add("20165233");
        //把你自己插入到合适的位置(学号升序)
        System.out.println("插入我的学号后排序,打印链表");
        Collections.sort(list);
        iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍历单链表,打印每个结点的
        list.remove("20165233");
        //从链表中删除自己
        System.out.println("删除我的学号后打印链表");
        iter=list.iterator();
        while(iter.hasNext()){
            String te=iter.next();
            System.out.println(te);
        }
        //遍历单链表,打印每个结点的
    }
    }
  • Screenshot of running result

Chapter 15 After-School Programming Questions

Question 1:

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

  • Code

    import java.util.*;
    public class Ep15_1 {
    public static void main(String[] args) {
        Stack<Integer> stack=new Stack<Integer>();
        stack.push(3);
        stack.push(8);
        int k=1;
        while (k<=10) {
            for (int i=1;i<=2;i++) {
                Integer F1=stack.pop();
                int f1=F1.intValue();
                Integer F2=stack.pop();
                int f2=F2.intValue();
                Integer temp= 2 * f1 + 2 * f2;
                System.out.println(""+temp.toString());
                stack.push(temp);
                stack.push(F2);
                k++;
            }
        }
    }
    }
  • Screenshot of running result

Question 2:

Store the students' English transcripts in the linked list into a tree set, so that they can be automatically sorted by grades and output the sorting results

  • Code

    import java.util.*;
    class CollegeStu implements Comparable {
    int english=0;
    String name;
    CollegeStu(int english,String name) {
        this.name=name;
        this.english=english;
    }
    @Override
    public int compareTo(Object b) {
        CollegeStu stu=(CollegeStu)b;
        return (this.english-stu.english);
    }
    }
    public class Ep15_2 {
    public static void main(String[] args) {
        List<CollegeStu> list=new LinkedList<CollegeStu>();
        int score []={67, 66, 90, 56, 80};
        String name []={"王杨鸿永","何彦达","张雨昕","刘津甫","祁瑛"};
        for (int i=0;i<score.length;i++) {
            list.add(new CollegeStu(score[i],name[i]));
        }
        Iterator<CollegeStu> iter=list.iterator();
        TreeSet<CollegeStu> mytree=new TreeSet<CollegeStu>();
        while (iter.hasNext()) {
            CollegeStu stu=iter.next();
            mytree.add(stu);
        }
        Iterator<CollegeStu> te=mytree.iterator();
        while (te.hasNext()) {
            CollegeStu stu=te.next();
            System.out.println(""+stu.name+" "+stu.english);
        }
    }
    }
  • Screenshot of running result

Question 3:

There are 10 U disks with two important attributes: price and capacity, write an application, use TreeMap<K,V>classes, and output the detailed information of 10 U disks sorted by price and capacity respectively.

  • Code

    import java.util.*;
    class UDiscKey implements Comparable {
    double key = 0;
    
    UDiscKey(double d) {
        key = d;
    }
    
    @Override
    public int compareTo(Object b) {
        UDiscKey disc = (UDiscKey) b;
        if ((this.key - disc.key) == 0) {
            return -1;
        } else {
            return (int) ((this.key - disc.key) * 1000);
        }
    }
    }
    class UDisc {
        int amount;
        double price;
        UDisc(int m,double e) {
            amount=m;
            price=e;
        }
    }
    public class Ep15_3 {
    public static void main(String[] args) {
        TreeMap<UDiscKey,UDisc> treeMap=new TreeMap<UDiscKey,UDisc>();
        int amount[]={1,2,4,8,16};
        double price[]={867,266,390,556};
        UDisc UDisc[]=new UDisc[4];
        for (int k=0;k<UDisc.length;k++) {
            UDisc[k]=new UDisc(amount[k],price[k]);
        }
        UDiscKey key[]=new UDiscKey[4];
        for (int k=0;k<key.length;k++) {
            key[k]=new UDiscKey(UDisc[k].amount);
        }
        for (int k=0;k<UDisc.length;k++) {
            treeMap.put(key[k],UDisc[k]);
        }
        int number=treeMap.size();
        Collection<UDisc> collection=treeMap.values();
        Iterator<UDisc> iter=collection.iterator();
        while (iter.hasNext()) {
            UDisc disc=iter.next();
            System.out.println(""+disc.amount+"G "+disc.price+"元");
        }
        treeMap.clear();
        for (int k=0;k<key.length;k++) {
            key[k]=new UDiscKey(UDisc[k].price);
        }
        for (int k=0;k<UDisc.length;k++) {
            treeMap.put(key[k],UDisc[k]);
        }
        number=treeMap.size();
        collection=treeMap.values();
        iter=collection.iterator();
        while (iter.hasNext()) {
            UDisc disc=iter.next();
            System.out.println(""+disc.amount+"G "+disc.price+"元");
        }
    }
    }
  • Screenshot of running result

Guess you like

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