算法Sedgewick第四版-第1章基础-015一stack只保留last指针

  1 /*************************************************************************
  2  *
  3  *  A generic queue, implemented using a *circular* linked list.
  4  *  (Exercise 1.3.29)
  5  *
  6  *  % java Ex_1_3_29 < tobe.txt
  7  *  to be or not to be (2 left on queue)
  8  *
  9  *************************************************************************/
 10
 11 import java.util.Iterator;
 12 import java.util.NoSuchElementException;
 13
 14 public class Ex_1_3_29<Item> implements Iterable<Item> {
 15     private int N;
 16     private Node last;
 17
 18     private class Node {
 19         private Item item;
 20         private Node next;
 21     }
 22
 23    /**
 24      * Create an empty queue.
 25      */
 26     public Ex_1_3_29() {
 27         last  = null;
 28     }
 29
 30    /**
 31      * Is the queue empty?
 32      */
 33     public boolean isEmpty() {
 34         return last == null;
 35     }
 36
 37    /**
 38      * Return the number of items in the queue.
 39      */
 40     public int size() {
 41         return N;
 42     }
 43
 44    /**
 45      * Return the item least recently added to the queue.
 46      * Throw an exception if the queue is empty.
 47      */
 48     public Item peek() {
 49         if (isEmpty()) throw new RuntimeException("Queue underflow");
 50         return last.next.item;
 51     }
 52
 53    /**
 54      * Add the item to the queue.
 55      */
 56     public void enqueue(Item item) {
 57         Node x = new Node();
 58         x.item = item;
 59         if (isEmpty())
 60             x.next = x;
 61         else
 62         {
 63             x.next = last.next;
 64             last.next = x;
 65         }
 66         last = x;
 67         N++;
 68     }
 69
 70    /**
 71      * Remove and return the item on the queue least recently added.
 72      * Throw an exception if the queue is empty.
 73      */
 74     public Item dequeue() {
 75         if (isEmpty()) throw new RuntimeException("Queue underflow");
 76         Item item = last.next.item;
 77         if (last.next == last)
 78             last = null;
 79         else
 80             last.next = last.next.next;
 81         N--;
 82         return item;
 83     }
 84
 85    /**
 86      * Return string representation.
 87      */
 88     public String toString() {
 89         StringBuilder s = new StringBuilder();
 90         for (Item item : this)
 91             s.append(item + " ");
 92         return s.toString();
 93     }
 94
 95
 96    /**
 97      * Return an iterator that iterates over the items on the queue in FIFO order.
 98      */
 99     public Iterator<Item> iterator()  {
100         return new ListIterator();
101     }
102
103     private class ListIterator implements Iterator<Item> {
104         private int n = N;
105         private Node current = last;
106
107         public boolean hasNext()  { return n > 0; }
108         public void remove()      { throw new UnsupportedOperationException();  }
109
110         public Item next() {
111             if (!hasNext()) throw new NoSuchElementException();
112             Item item = current.next.item;
113             current = current.next;
114             n--;
115             return item;
116         }
117     }
118
119
120     public static void main(String[] args) {
121         Ex_1_3_29<String> q = new Ex_1_3_29<String>();
122         while (!StdIn.isEmpty()) {
123             String item = StdIn.readString();
124             if (!item.equals("-")) q.enqueue(item);
125             else if (!q.isEmpty()) StdOut.print(q.dequeue() + " ");
126         }
127         StdOut.println("(" + q.size() + " left on queue: [ " + q + "])");
128     }
129 }
时间: 2024-10-29 19:09:38

算法Sedgewick第四版-第1章基础-015一stack只保留last指针的相关文章

算法Sedgewick第四版-第1章基础-001递归

一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归,因为递归代码比相应的非递归代码更加简洁优雅.易懂.下面这种实现中的注释就言简意赅地说明了代码的作用.我们可以用数学归纳法证明这段注释所解释的算法的正确性.我们会在 3.1 节中展开这个话题并为二分查找提供一个这样的证明.编写递归代码时最重要的有以下三点.? 递归总有一个最简单的情况——方法的第一条语

算法Sedgewick第四版-第1章基础-008一用数组实现固定长度的栈

1 package algorithms.ADT; 2 3 /****************************************************************************** 4 * Compilation: javac FixedCapacityStackOfStrings.java 5 * Execution: java FixedCapacityStackOfStrings 6 * Dependencies: StdIn.java StdOut.

算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

1. 1 package algorithms.analysis14; 2 3 import algorithms.util.StdOut; 4 import algorithms.util.StdRandom; 5 6 /****************************************************************************** 7 * Compilation: javac DoublingTest.java 8 * Execution: jav

算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 1 package algorithms.elementary21; 2 3 /****************************************************************************** 4 * Compilation: javac Insertion.java 5 * Execution: java I

算法Sedgewick第四版-第1章基础-018一解决不能声明泛型数组的两咱方法(强转或反射)

1. 1 /****************************************************************************** 2 * Compilation: javac ResizingArrayStackWithReflection.java 3 * Execution: java ResizingArrayStackWithReflection < input.txt 4 * Dependencies: StdIn.java StdOut.jav

算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

一. 1. 2. 3. 二.代码 1 package algorithms.mergesort22; 2 3 import algorithms.util.StdIn; 4 import algorithms.util.StdOut; 5 6 /****************************************************************************** 7 * Compilation: javac MergeBU.java 8 * Executio

算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的

1. package algorithms.stacks13; /****************************************************************************** * Compilation: javac ResizingArrayBag.java * Execution: java ResizingArrayBag * Dependencies: StdIn.java StdOut.java * * Bag implementatio

算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)

一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursively), and then merge the results. As you will see, one of mergesort’s most attractive properties is that it guarantees to sort any array of N items in t

算法Sedgewick第四版-第1章基础-003一封装交易对象

1. 1 package ADT; 2 3 /****************************************************************************** 4 * Compilation: javac Transaction.java 5 * Execution: java Transaction 6 * Dependencies: StdOut.java 7 * 8 * Data type for commercial transactions.