zoukankan      html  css  js  c++  java
  • Java 用链表实现栈和队列

      是一种基于后进先出(LIFO)策略的集合类型。当邮件在桌上放成一叠时,就能用栈来表示。新邮件会放在最上面,当你要看邮件时,会一封一封从上到下阅读。栈的顶部称为栈顶,所有操作都在栈顶完成。

      前面提到的新邮件,就是在栈顶入栈(push),阅读的时候从栈顶取出一封就是出栈(pop)。就像下面这个图一样,你不能从最下面直接拿一封信出来。

     1 package ABAB;
     2 
     3 /**
     4  * 链表实现栈
     5  * @param <Item>
     6  */
     7 public class Stack<Item> {
     8 
     9     private Node first;
    10     private Integer N = 0;
    11 
    12     private class Node {
    13         Item item;
    14         Node next;
    15 
    16         public Node(Item i) {
    17             this.item = i;
    18             this.next = null;
    19         }
    20     }
    21 
    22     public boolean isEmpty() { return N == 0; }
    23 
    24     public int size() { return N; }
    25 
    26     /**
    27      * 进栈
    28      *
    29      * @param i
    30      */
    31     public void push(Item i) {
    32         Node node = new Node(i);
    33         if (first == null) {
    34             first = node;
    35             N++;
    36             return;
    37         }
    38         Node oldFirst = first;
    39         first = node;
    40         first.next = oldFirst;
    41         N++;
    42     }
    43 
    44     /**
    45      * 获取栈顶元素
    46      *
    47      * @return
    48      */
    49     public Item peek() {
    50         if (first == null)
    51             return null;
    52         return first.item;
    53     }
    54 
    55     /**
    56      * 出栈
    57      *
    58      * @return
    59      */
    60     public Item pop() {
    61         if (isEmpty())
    62             return null;
    63         Item item = first.item;
    64         first = first.next;
    65         N--;
    66         return item;
    67     }
    68 
    69 }

    队列

      队列是一种基于先进先出(FIFO)策略的集合类型。例如排队上车,先排队的人可以先上车。

      队列的链表实现中有两个指针,一个指向队头,一个指向队尾。当有新的元素进入时,新元素被放在队尾。当要出队时,从队头弹出元素,并且将队头后移。

     1 package ABAB;
     2 
     3 /**
     4  * 链表实现队列
     5  * @param <Item>
     6  */
     7 public class Queue<Item> {
     8 
     9     private Node first;
    10     private Node last;
    11     private int N = 0;
    12     private class Node{
    13         Item item;
    14         Node next;
    15         public Node(Item i){
    16             this.item = i;
    17             this.next = null;
    18         }
    19     }
    20 
    21     public boolean isEmpty(){return N == 0;};
    22     public int size(){return N;}
    23 
    24     /**
    25      * 入队
    26      * @param item
    27      */
    28     public void enqueue(Item item){
    29         Node node = new Node(item);
    30         Node oldLast = last;
    31         last = node;
    32         if(isEmpty())
    33             first = last;
    34         else
    35             oldLast.next = last;
    36         N++;
    37     }
    38 
    39     /**
    40      * 出队
    41      * @return
    42      */
    43     public Item dequeue(){
    44         Item item;
    45         if(isEmpty())
    46             return null;
    47         item = first.item;
    48         first = first.next;
    49         N--;
    50         return item;
    51     }
    52 
    53     /**
    54      * 获取队首元素
    55      * @return
    56      */
    57     public Item peek(){
    58         if(isEmpty())
    59             return null;
    60         return first.item;
    61     }
    62 
    63 }
  • 相关阅读:
    122. Best Time to Buy and Sell Stock II
    121. Best Time to Buy and Sell Stock
    72. Edit Distance
    583. Delete Operation for Two Strings
    582. Kill Process
    indexDB基本用法
    浏览器的渲染原理
    js实现txt/excel文件下载
    git 常用命令
    nginx进入 配置目录时
  • 原文地址:https://www.cnblogs.com/ELAIRS/p/12131659.html
Copyright © 2011-2022 走看看