算法练习(47): 队列、栈的复制(1.3.41-1.3.42)

本系列博客习题来自《算法(第四版)》,算是本人的读书笔记,如果有人在读这本书的,欢迎大家多多交流。为了方便讨论,本人新建了一个微信群(算法交流),想要加入的,请添加我的微信号:zhujinhui207407 谢谢。另外,本人的个人博客 http://www.kyson.cn 也在不停的更新中,欢迎一起讨论

算法(第4版)

知识点

  • 队列、栈的复制

题目

1.3.41 复制队列。编写一个新的构造函数,使以下代码:
Queue r = new Queue(q);
得到的 r 指向队列 q 的一个新的独立的副本。可以对 q 或 r 进行任意入列或出列操作但它们不会相互影响。


1.3.41 Copy a queue. Create a new constructor so that
Queue<Item> r = new Queue<Item>(q);
makes r a reference to a new and independent copy of the queue q. You should be able to push and pop from either q or r without influencing the other. Hint : Delete all of the elements from q and add these elements to both q and r.

分析

本人所有简书的算法文章详细分析已经移入小专栏:算法四习题详解,欢迎大家订阅

答案

public static class Queue<Item> {
    private int N;

    public Queue(Queue<Item> q) {
        int size = q.size();
        for (int i = 0; i < size; i++) {
            this.enqueue(q.dequeue());
        }
    }

    private class Node {
        Item item;
        Node next;
    }

    private Node first;
    private Node last;

    public Queue() {

    }

    public boolean isEmpty() {
        if (first == null)
            return true;
        return false;
    }

    public int size() {
        return N;
    }

    public void enqueue(Item item) {
        Node oldLast = last;
        last = new Node();
        last.item = item;
        last.next = null;
        if (this.isEmpty()) {
            first = last;
        } else {
            oldLast.next = last;
        }
        N++;
    }

    public Item dequeue() {
        Item item = first.item;
        first = first.next;
        if (this.isEmpty()) {
            last = null;
        }
        N--;
        return item;
    }

    public static void main(String[] args) {
        Queue queue = new Queue();
        queue.enqueue("I");
        queue.enqueue("am");
        queue.enqueue("Kyson");

        Queue queue1 = new Queue(queue);
        int size = queue1.size();
        for (int i = 0; i < size; i++) {
            System.out.println(queue1.dequeue());
        }
    }
}

题目

复制栈。为基于链表实现的栈编写一个新的构造函数,使以下代码

Stack t = new Stack(s);

得到的 t 指向栈 s 的一个新的独立的副本。


1.3.42 Copy a stack. Create a new constructor for the linked-list implementation of Stack so that
Stack<Item> t = new Stack<Item>(s);
makes t a reference to a new and independent copy of the stack s.

答案

public class  Stack<Item> implements Iterable<Item> {
    private Item[] a;
    private int N;

    public Stack(int cap) {
        a =(Item[]) new Object[cap];
    }

    public Stack(Stack<Item> s) {
        Stack<Item> xx = new Stack<Item>(1);
        a =(Item[]) new Object[1];
        Stack<Item>.StackIterator xit = s.iterator();
        while (xit.hasNext()){
            xx.push(xit.next());
        }

        Stack<Item>.StackIterator xxit = xx.iterator();
        while (xxit.hasNext()) {
            this.push(xxit.next());
        }
    }

    public void push(Item item) {
        if (a.length ==  N) {
            resize(2 * N);
        }
        a[N++] = item;
    }

    public void resize(int max)
    {
        Item[] temp = (Item[]) new Object[max];
        for (int i = 0 ; i < N ; ++i) {
            temp[i] = a[i];
        }
        a = temp;
    }

    public boolean isEmpty() {
        return N == 0;
    }

    public Item pop() {
        Item item = a[--N];
        a[N] = null;
        if (N > 0 && a.length == N / 4) {
            resize(a.length / 2);
        }
        return item;
    }

    public StackIterator iterator()
    {
        return new StackIterator();
    }

    private class StackIterator implements Iterator<Item>
    {
        private int i = N;

        @Override
        public boolean hasNext() {
            return i > 0;
        }

        @Override
        public Item next() {
            return a[--i];
        }
    }
}

测试用例

public static void main(String[] args)
{
    Stack<String> a = new Stack<>(10);
    a.push("1");
    a.push("2");
    a.push("3");
    a.push("4");
    a.push("5");
    a.push("6");
    a.pop();
    a.pop();

    Stack<String> b= new Stack<>(a);

}

广告

我的首款个人开发的APP壁纸宝贝上线了,欢迎大家下载。

壁纸宝贝

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 昨晚看完七月八号上映的《大鱼海棠》,来看一看其中的缺憾。 《大鱼海棠》这部影片,本质上是在描述中国神话世界中...
    绅士猫俏皮狗阅读 4,606评论 3 1
  • 你会为自己选择怎样的生活? 当我问出这个问题的时候,却又开始反思好像不应该这么问,因为绝大多数人并不能完全随自己的...
    奶茶熙阅读 5,786评论 3 19
  • 【0421今日话题】 你有没有收到过“清粉”信息?你是如何看待“清粉”这件事呢? 我是个玩票的微商,在这个圈子,收...
    一珈之主阅读 1,254评论 0 0
  • 单调的鼾声打破黑暗的沉默 窗外灯光点点 谁在梦里招惹逝去的繁华 破旧的日记掀开遗失的童话 迷失的世界没有笑声与鲜花...
    墨染月华阅读 1,950评论 0 5
  • 一 “也许当时忙着微笑和哭泣,忙着追逐天空中的流星……与你相遇,好幸运,可我已失去为你泪流满面的权利。” 最近我总...
    木鱼and咸鱼阅读 4,850评论 11 16