• 集合第七发练习之利用ArrayList做栈、队列


     

    栈:后进先出。

    队列:先进先出。

    利用这个核心思想,结合集合的add和remove方法做的栈和队列如下代码所示:

    主类(t1()和t2()分别演栈和队列):

    1.  
      package cn.hncu.mySet2;
    2.  
       
    3.  
      import java.util.Iterator;
    4.  
       
    5.  
      import cn.hncu.set.Person;
    6.  
       
    7.  
      public class TestDemo {
    8.  
      public static void main(String[] args) {
    9.  
      t1();
    10.  
      // t2();
    11.  
       
    12.  
       
    13.  
      }
    14.  
       
    15.  
      private static void t1() {
    16.  
      Person p1=new Person("01", "张三", 20);
    17.  
      Person p2=new Person("02", "李四", 21);
    18.  
      Person p3=new Person("03", "王五", 22);
    19.  
      Person p4=new Person("04", "mis", 17);
    20.  
       
    21.  
      MyStack stack=new MyStack();
    22.  
      stack.in(p1);
    23.  
      stack.in(p2);
    24.  
      stack.in(p3);
    25.  
       
    26.  
      System.out.println(stack.out());
    27.  
      System.out.println(stack.out());
    28.  
      System.out.println(stack.out());
    29.  
      System.out.println(stack.out());
    30.  
      System.out.println(stack.out());
    31.  
      }
    32.  
      private static void t2() {
    33.  
      Person p1=new Person("01", "张三", 20);
    34.  
      Person p2=new Person("02", "李四", 21);
    35.  
      Person p3=new Person("03", "王五", 22);
    36.  
      Person p4=new Person("04", "mis", 17);
    37.  
      MyQueue queue=new MyQueue();
    38.  
      queue.in(p1);
    39.  
      queue.in(p2);
    40.  
      queue.in(p3);
    41.  
      queue.in(p4);
    42.  
      System.out.println(queue.out());
    43.  
      System.out.println(queue.out());
    44.  
      System.out.println(queue.out());
    45.  
      System.out.println(queue.out());
    46.  
      System.out.println(queue.out());
    47.  
       
    48.  
      }
    49.  
       
    50.  
      }
    栈类:
    1.  
      package cn.hncu.mySet2;
    2.  
       
    3.  
      import java.util.ArrayList;
    4.  
      import java.util.HashMap;
    5.  
      import java.util.HashSet;
    6.  
      import java.util.LinkedList;
    7.  
      import java.util.List;
    8.  
      import java.util.Set;
    9.  
      import java.util.Stack;
    10.  
       
    11.  
      public class MyStack {
    12.  
      private LinkedList mystack=new LinkedList();
    13.  
      public void in(Object obj){
    14.  
      mystack.add(obj);
    15.  
      }
    16.  
      public Object out(){
    17.  
      if(mystack.isEmpty()){
    18.  
      return null;
    19.  
      }
    20.  
      return mystack.removeLast();
    21.  
      }
    22.  
       
    23.  
      public boolean isEmpty(){
    24.  
      return mystack.isEmpty()?true:false;
    25.  
      }
    26.  
       
    27.  
      }

    队列类:
    1.  
      package cn.hncu.mySet2;
    2.  
       
    3.  
      import java.util.LinkedList;
    4.  
       
    5.  
      public class MyQueue {
    6.  
      private LinkedList list=new LinkedList();
    7.  
      public void in(Object obj){
    8.  
      list.add(obj);
    9.  
      }
    10.  
       
    11.  
      public Object out(){
    12.  
      if(list.isEmpty()){
    13.  
      return null;
    14.  
      }
    15.  
      return list.remove(0);
    16.  
      }
    17.  
      public boolean isEmpty(){
    18.  
      return list.isEmpty()?true:false;
    19.  
      }
    20.  
      }
    person类:
    1.  
      package cn.hncu.mySet2;
    2.  
       
    3.  
      public class Person {
    4.  
      private String Sno;
    5.  
      private String name;
    6.  
      private int age;
    7.  
      public Person(String sno, String name, int age) {
    8.  
      super();
    9.  
      Sno = sno;
    10.  
      this.name = name;
    11.  
      this.age = age;
    12.  
      }
    13.  
      public String getSno() {
    14.  
      return Sno;
    15.  
      }
    16.  
      public void setSno(String sno) {
    17.  
      Sno = sno;
    18.  
      }
    19.  
      public String getName() {
    20.  
      return name;
    21.  
      }
    22.  
      public void setName(String name) {
    23.  
      this.name = name;
    24.  
      }
    25.  
      public int getAge() {
    26.  
      return age;
    27.  
      }
    28.  
      public void setAge(int age) {
    29.  
      this.age = age;
    30.  
      }
    31.  
      @Override
    32.  
      public String toString() {
    33.  
      return "Person [Sno=" + Sno + ", name=" + name + ", age=" + age + "]";
    34.  
      }
    35.  
      @Override
    36.  
      public int hashCode() {
    37.  
      final int prime = 31;
    38.  
      int result = 1;
    39.  
      result = prime * result + ((Sno == null) ? 0 : Sno.hashCode());
    40.  
      result = prime * result + age;
    41.  
      result = prime * result + ((name == null) ? 0 : name.hashCode());
    42.  
      return result;
    43.  
      }
    44.  
      @Override
    45.  
      public boolean equals(Object obj) {
    46.  
      if (this == obj)
    47.  
      return true;
    48.  
      if (obj == null)
    49.  
      return false;
    50.  
      if (getClass() != obj.getClass())
    51.  
      return false;
    52.  
      Person other = (Person) obj;
    53.  
      if (Sno == null) {
    54.  
      if (other.Sno != null)
    55.  
      return false;
    56.  
      } else if (!Sno.equals(other.Sno))
    57.  
      return false;
    58.  
      if (age != other.age)
    59.  
      return false;
    60.  
      if (name == null) {
    61.  
      if (other.name != null)
    62.  
      return false;
    63.  
      } else if (!name.equals(other.name))
    64.  
      return false;
    65.  
      return true;
    66.  
      }
    67.  
       
    68.  
      }
  • 相关阅读:
    oracle,sql server count函数 存储过程 判断 行数 注意事项
    js 跨域访问 获取验证码图片 获取header 自定义属性
    开发作中常用,实用工具推荐!
    phpcms
    php基础
    jQuery , js 写选项卡
    js, jquery实现全选,反选
    jQuery选择器
    学习jQuery
    javascript 与 java继承问题
  • 原文地址:https://www.cnblogs.com/williamjie/p/9466647.html
Copyright © 2020-2023  润新知