zoukankan      html  css  js  c++  java
  • 算法学习之基础(背包 列队 栈) 习题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

  • 相关阅读:
    纯CSS气泡对话框
    使用JAVASCRIPT实现的 单行文字 逐个文字变色,可以循环控制
    数字转换成中文汉字
    jquery如何在对表格添加删除后再对其前面加上顺序的序列号
    情路
    【缅怀妈妈系列诗歌】之二十四:一份永不忘却的思念
    我等你,千年之后
    赢道在手,前程无忧
    为你感动
    你为什么对我如此冷漠
  • 原文地址:https://www.cnblogs.com/ganhang-acm/p/5405930.html
Copyright © 2011-2022 走看看