Week 10 homework (make-up class test)
1. Summary of knowledge points
1. Singly linked list
- Create a singly linked list
- Insertion of data in linked list
list.add("**");
- Sorting of data in linked list
Collections.sort();
- Deletion of data in linked list
lsit.remove("");
2. Sort
- tree set concept
- tree map
TreeMap<K,V>**适合用于数据的排序**
- Sort by keyword
TreeMap<StudentKey,Student> treemap= new TreeMap<StudentKey,Student>();
2. Supplementary content and screenshots
1. Singly linked list
Create linked list
create node
Insert your own student number and sort
Delete your student ID and print
overall code
import java.util.*; public class MyList { public static void main(String [] args) { List<String> mylist=new LinkedList<String>(); //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点 mylist.add("20165224"); mylist.add("20165225"); mylist.add("20165227"); mylist.add("20165228"); //把上面四个节点连成一个没有头结点的单链表 System.out.println("打印初始链表"); //遍历单链表,打印每个结点的 Iterator<String> iter=mylist.iterator(); while (iter.hasNext()){ String num=iter.next(); System.out.println(num); } //把你自己插入到合适的位置(学号升序) mylist.add("20165226"); Collections.sort(mylist); //遍历单链表,打印每个结点的 System.out.println("插入我的学号在排序之后,链表中的数据:"); iter =mylist.iterator(); while(iter.hasNext()){ String num=iter.next(); System.out.println(num); } //从链表中删除自己 mylist.remove("20165226"); System.out.println("删除我的学号之后打印链表:"); //遍历单链表,打印每个结点的 iter=mylist.iterator(); while(iter.hasNext()){ String num=iter.next(); System.out.println(num); } } }
operation result
2. Sort
Create
Student
object of class
Use TreeMap sorting method
总成绩
Sort by keyword
学号
Sort by keyword
operation result
3. Make up the programming topic of 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
import java.util.*;
public class E {
public static void main(String args[]) {
Stack<Integer> stack=new Stack<Integer();
stack.push(new Integer(3));
stack.push(new Integer(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=new Integer(2*f1+2*f2);
System.out.println(""+temp.toString());
stack.push(temp);
stack.push(F2);
k++;
}
}
}
}
2. Write a program to 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
import java.util.*;
class Student implements Comparable {
int english=0;
String name;
Student(int english,String name) {
this.name=name;
this.english=english;
}
public int compareTo(Object b) {
Student st=(Student)b;
return (this.english-st.english);
}
}
public class E {
public static void main(String args[]) {
List<Student> list=new LinkedList<Student>();
int score []={65,76,45,99,77,88,100,79};
String name[]={"张三","李四","旺季","加戈","为哈","周和","赵李","将集"};
for(int i=0;i<score.length;i++){
list.add(new Student(score[i],name[i]));
}
Iterator<Student> iter=list.iterator();
TreeSet<Student> mytree=new TreeSet<Student>();
while(iter.hasNext()){
Student stu=iter.next();
mytree.add(stu);
}
Iterator<Student> te=mytree.iterator();
while(te.hasNext()) {
Student stu=te.next();
System.out.println(""+stu.name+" "+stu.english);
}
}
}
3. There are 10 U disks with two important attributes: price and capacity. Write an application that uses TreeMap
import java.util.*;
class UDiscKey implements Comparable {
double key=0;
UDiscKey(double d) {
key=d;
}
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 E {
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+"元");
}
}
}