Java中的LinkedList 是采用双向循环列表实现的。
利用LinkedList 可以实现栈(stack)、队列(queue)
下面写两个例子
学生类:
int stuId;
publicintgetStuId(){returnstuId;}publicvoidsetStuId(intstuId){this.stuId = stuId;}publicStringgetStuName(){returnstuName;}publicvoidsetStuName(String stuName){this.stuName = stuName;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age = age;}String stuName;intage;publicStudent(){}publicStudent(intstuId,String stuName,intage){this.age = age;this.stuId = stuId;this.stuName = stuName;}publicStringtoString(){return("学生姓名:"+this.stuName+"学生年龄"+this.age+"学生编号"+this.stuId);}
队列
自定义方法实现LInkedList
LinkedList lists =new LinkedList();
publicvoidpush(Objectobject){ lists.addLast(object);}publicObjectpop(){returnlists.removeFirst();}
实现方法
System.out.println("在队列中添加对象");
MyQuene quene = new MyQuene();
quene.push(new Student(1,"学生1",20));
quene.push(new Student(2,"学生2",21));
quene.push(new Student(3,"学生3",23));
for (Object object : quene.lists) {
Student student = (Student)object;
System.out.println(student.toString());
}
System.out.println("在队列中删除对象");
quene.pop();
for (Object object : quene.lists) {
Student student = (Student)object;
System.out.println(student.toString());
}
堆栈:
自定义方法,并实现
LinkedList lists =new LinkedList();
publicvoidpush(Objectobject){ lists.addFirst(object);}publicObjectpop(){returnlists.removeLast();}
System.out.println("在队列中添加对象");
MyStack Stack = new MyStack();
Stack.push(new Student(1,"学生1",20));
Stack.push(new Student(2,"学生2",21));
Stack.push(new Student(3,"学生3",23));
for (Object object : Stack.lists) {
Student student = (Student)object;
System.out.println(student.toString());
}
System.out.println("在队列中删除对象");
Stack.pop();
for (Object object : Stack.lists) {
Student student = (Student)object;
System.out.println(student.toString());
}
感兴趣的可以加群:854393687