栈:后进先出。
队列:先进先出。
利用这个核心思想,结合集合的add和remove方法做的栈和队列如下代码所示:
主类(t1()和t2()分别演栈和队列):
-
package cn.hncu.mySet2;
-
-
import java.util.Iterator;
-
-
import cn.hncu.set.Person;
-
-
public class TestDemo {
-
public static void main(String[] args) {
-
t1();
-
// t2();
-
-
-
}
-
-
private static void t1() {
-
Person p1=new Person("01", "张三", 20);
-
Person p2=new Person("02", "李四", 21);
-
Person p3=new Person("03", "王五", 22);
-
Person p4=new Person("04", "mis", 17);
-
-
MyStack stack=new MyStack();
-
stack.in(p1);
-
stack.in(p2);
-
stack.in(p3);
-
-
System.out.println(stack.out());
-
System.out.println(stack.out());
-
System.out.println(stack.out());
-
System.out.println(stack.out());
-
System.out.println(stack.out());
-
}
-
private static void t2() {
-
Person p1=new Person("01", "张三", 20);
-
Person p2=new Person("02", "李四", 21);
-
Person p3=new Person("03", "王五", 22);
-
Person p4=new Person("04", "mis", 17);
-
MyQueue queue=new MyQueue();
-
queue.in(p1);
-
queue.in(p2);
-
queue.in(p3);
-
queue.in(p4);
-
System.out.println(queue.out());
-
System.out.println(queue.out());
-
System.out.println(queue.out());
-
System.out.println(queue.out());
-
System.out.println(queue.out());
-
-
}
-
-
}
-
package cn.hncu.mySet2;
-
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.HashSet;
-
import java.util.LinkedList;
-
import java.util.List;
-
import java.util.Set;
-
import java.util.Stack;
-
-
public class MyStack {
-
private LinkedList mystack=new LinkedList();
-
public void in(Object obj){
-
mystack.add(obj);
-
}
-
public Object out(){
-
if(mystack.isEmpty()){
-
return null;
-
}
-
return mystack.removeLast();
-
}
-
-
public boolean isEmpty(){
-
return mystack.isEmpty()?true:false;
-
}
-
-
}
队列类:
-
package cn.hncu.mySet2;
-
-
import java.util.LinkedList;
-
-
public class MyQueue {
-
private LinkedList list=new LinkedList();
-
public void in(Object obj){
-
list.add(obj);
-
}
-
-
public Object out(){
-
if(list.isEmpty()){
-
return null;
-
}
-
return list.remove(0);
-
}
-
public boolean isEmpty(){
-
return list.isEmpty()?true:false;
-
}
-
}
-
package cn.hncu.mySet2;
-
-
public class Person {
-
private String Sno;
-
private String name;
-
private int age;
-
public Person(String sno, String name, int age) {
-
super();
-
Sno = sno;
-
this.name = name;
-
this.age = age;
-
}
-
public String getSno() {
-
return Sno;
-
}
-
public void setSno(String sno) {
-
Sno = sno;
-
}
-
public String getName() {
-
return name;
-
}
-
public void setName(String name) {
-
this.name = name;
-
}
-
public int getAge() {
-
return age;
-
}
-
public void setAge(int age) {
-
this.age = age;
-
}
-
-
public String toString() {
-
return "Person [Sno=" + Sno + ", name=" + name + ", age=" + age + "]";
-
}
-
-
public int hashCode() {
-
final int prime = 31;
-
int result = 1;
-
result = prime * result + ((Sno == null) ? 0 : Sno.hashCode());
-
result = prime * result + age;
-
result = prime * result + ((name == null) ? 0 : name.hashCode());
-
return result;
-
}
-
-
public boolean equals(Object obj) {
-
if (this == obj)
-
return true;
-
if (obj == null)
-
return false;
-
if (getClass() != obj.getClass())
-
return false;
-
Person other = (Person) obj;
-
if (Sno == null) {
-
if (other.Sno != null)
-
return false;
-
} else if (!Sno.equals(other.Sno))
-
return false;
-
if (age != other.age)
-
return false;
-
if (name == null) {
-
if (other.name != null)
-
return false;
-
} else if (!name.equals(other.name))
-
return false;
-
return true;
-
}
-
-
}