• 算法学习之基础(背包 列队 栈) 习题1.3.33泛型双向队列


    前几天写的。。

      1 package gh;
      2 
      3 import java.util.Iterator;
      4 
      5 /**
      6  * 泛型双向队列(双向链表实现)
      7  * @author ganhang
      8  *
      9  */
     10 public class Deque<T> implements Iterable<T> {
     11     private Node first;
     12     private Node last;
     13     private int n=0;
     14     public Deque(){
     15         
     16     }
     17     //从左边加入
     18     public void pushLeft(T item) {
     19         if (first == null) {
     20             first=new Node(item);    
     21             last=first;
     22         }else{
     23             Node oldfirst = first;
     24             first=new Node(item);
     25             first.next=oldfirst;
     26             oldfirst.pre=first;
     27         }
     28         n++;
     29     }
     30     //从右边加入
     31     public void pushRight(T item){
     32         if(last==null){
     33             last=new Node(item);
     34             first=last;
     35         }else{
     36             Node oldlast=last;
     37             last=new Node(item);
     38             oldlast.next=last;
     39             last.pre=oldlast;
     40         }
     41         n++;
     42     }
     43     //从左边弹出一个元素
     44     public T popLeft() {
     45         if (first != null) {
     46             T item = first.item;
     47             first = first.next;
     48             n--;
     49             return item;
     50         }
     51         return null;
     52     }
     53     //从右边弹出一个元素
     54     public T popRight(){
     55         if(last!=null){
     56             T item=last.item;
     57             last=last.pre;
     58             n--;
     59             return item;
     60         }
     61         return null;
     62     }
     63     public int size(){
     64         return n;
     65     }
     66     public boolean isEmpty(){
     67         return n==0;
     68     }
     69     private class Node{
     70         private T item;
     71         private Node pre;
     72         private Node next;
     73         public Node(T item){
     74             this.item=item;
     75         }
     76     }
     77     @Override
     78     public Iterator<T> iterator() {
     79         // TODO Auto-generated method stub
     80         return new DequeIterator();
     81     }
     82     private class DequeIterator implements Iterator<T>{
     83         private Node current=first;
     84         @Override
     85         public boolean hasNext() {
     86             // TODO Auto-generated method stub
     87             return current!=null;
     88         }
     89 
     90         @Override
     91         public T next() {
     92             // TODO Auto-generated method stub
     93             T item=current.item;
     94             current=current.next;
     95             return item;
     96         }
     97 
     98         @Override
     99         public void remove() {
    100             // TODO Auto-generated method stub
    101             
    102         }
    103         
    104     }
    105 
    106 }

    测试

    Deque<String> d=new Deque<String>();
            d.pushLeft("a");
            d.pushLeft("b");
            d.pushLeft("c");
            d.pushLeft("d");
            d.pushRight("1");
            d.pushRight("2");
            d.pushRight("3");
            d.pushRight("4");
            while(!d.isEmpty()){
                System.out.println(d.popRight());
            }
            System.out.println("-----------");
            for(String s:d)
            System.out.println(s);

    输出

    4
    3
    2
    1
    a
    b
    c
    d
    -----------
    d
    c
    b
    a
    1
    2
    3
    4

  • 相关阅读:
    多线程22:线程池
    多线程21:信号灯法
    多线程20:管程法
    多线程19:生产者消费者模式
    多线程18:Lock锁
    多线程17:死锁
    多线程16:CopyOnWriteArrayList
    多线程15:线程同步
    多线程14:三大不安全案例
    业余草 maven异常:Updating Maven Project 的统一解决方案
  • 原文地址:https://www.cnblogs.com/ganhang-acm/p/5405930.html
Copyright © 2020-2023  润新知