搜索
有爱,有技术,有你^_^)y
╱人◕‿‿◕人╲订下契约(注册新用户)

合作站点账号登陆

QQ登录

只需一步,快速开始

快捷导航
查看: 4740|回复: 38
收起左侧

[普通教程] Coursera普林斯顿大学公开课Algorithm I总结(一):栈和队列

[复制链接]

该用户从未签到

258

主题

314

好友

3万

积分

第二章

积分
35715
发表于 2012-10-7 22:11:10 | 显示全部楼层 |阅读模式

╱人◕‿‿◕人╲定下契约

您需要 登录 才可以下载或查看,没有账号?╱人◕‿‿◕人╲订下契约(注册新用户)

x
本帖最后由 轻舟过 于 2012-10-7 22:13 编辑

8月中旬的时候我注册了Coursera上斯坦福大学的Algorithm I的课程,然后一直积极地学习这门课,直到课程结束。虽然注册的时候只是抱着复习一下本科学过的知识的心态,但是在学习的过程中还是学到了不少新的知识。比如本科阶段学习的算法课使用的语言是C语言,是面向过程的,而本课程使用的语言则是Java,于是了解了一些面向对象语言实现数据结构的方法。
栈和队列
栈和队列应该是最简单的数据结构了,栈是LIFO的数据结构,而队列是FIFO的数据结构。这两种数据结构都有两种实现方式,一种是使用链表的实现,而另外一种则是使用数组的实现。
用链表实现的栈每次插入、删除、查看栈顶元素处理的都是头部的元素,而队列插入是插在链表的尾部,查看以及删除元素处理的都是头部元素,基本上维护好指针就OK了。
栈和队列的链表实现不需要事先给定栈和队列的大小,插入元素只需要分配一个节点的空间并将其链到链表上就可以了,删除也是类似,处理好指针,gc会自动回收节点的空间。而数组实现不一样,数组是需要指定长度的,这样数组实现的构造函数与链表实现的构造函数的api就不一样了。解决的方法就是采用一种策略在合适的时候重新分配空间并将原始数组的元素复制到新数组,从而实现数组自动增长和缩短。
当然我们不能在每次插入或删除时调整数组的长度,一个可行的策略是:
  • 在数组满的时候将分配一个两倍于原先数组长度的空间,并复制元素。
  • 在数组1/4满的时候分配一个原先数组1/2长度的空间,并复制元素。
这样N次插入,N次删除所需的时间是O(N),也就是说均摊下来每次操作的时间是O(1)。
泛型
当然栈和队列需要泛型编程实现,以支持各种类,需要注意的是Java并不支持创建泛型的数组,也就是这样的代码是会报错的:
[mw_shl_code=java,true]s = new Item[capacity];[/mw_shl_code]正确的写法应该是这样:
[mw_shl_code=java,true]s = (Item[]) new Object[capacity];[/mw_shl_code]
迭代器
栈和队列可以实现迭代器接口Iterable,以方便地遍历元素
[mw_shl_code=java,true]public interface Iterable<Item>
{
    Iterator<Item> iterator();
}[/mw_shl_code]Iterator也是一个接口:
[mw_shl_code=java,true]public interface Iterator<Item>
{
   boolean hasNext();
   Item next();
   void remove();
}[/mw_shl_code]接口的实现如下:
[mw_shl_code=java,true]public Iterator<Item> iterator() { return new ListIterator(); }
private class ListIterator implements Iterator<Item>
{
    private Node current = first;
    public boolean hasNext() {  return current != null;  }
    public void remove()     {  /* not supported */      }      
    public Item next()
    {
        Item item = current.item;
        current   = current.next;
        return item;
    }
}[/mw_shl_code]实现了接口之后可以用如下的代码来遍历容器:
[mw_shl_code=java,true]for (String s : stack)
   StdOut.println(s);[/mw_shl_code]
代码
附Queue.java和Stack.java的实现,代码来自课程的algs4.jar。
[mw_shl_code=java,true]/*************************************************************************
*  Compilation:  javac Queue.java
*  Execution:    java Queue < input.txt
*  Data files:   http://algs4.cs.princeton.edu/13stacks/tobe.txt  
*
*  A generic queue, implemented using a linked list.
*
*  % java Queue < tobe.txt
*  to be or not to be (2 left on queue)
*
*************************************************************************/

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
*  The <tt>Queue</tt> class represents a first-in-first-out (FIFO)
*  queue of generic items.
*  It supports the usual <em>enqueue</em> and <em>dequeue</em>
*  operations, along with methods for peeking at the top item,
*  testing if the queue is empty, and iterating through
*  the items in FIFO order.
*  <p>
*  All queue operations except iteration are constant time.
*  <p>
*  For additional documentation, see <a href="http://algs4.cs.princeton.edu/13stacks">Section 1.3</a> of
*  <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*/
public class Queue<Item> implements Iterable<Item> {
    private int N;         // number of elements on queue
    private Node first;    // beginning of queue
    private Node last;     // end of queue

    // helper linked list class
    private class Node {
        private Item item;
        private Node next;
    }

   /**
     * Create an empty queue.
     */
    public Queue() {
        first = null;
        last  = null;
        N = 0;
        assert check();
    }

   /**
     * Is the queue empty?
     */
    public boolean isEmpty() {
        return first == null;
    }

   /**
     * Return the number of items in the queue.
     */
    public int size() {
        return N;     
    }

   /**
     * Return the item least recently added to the queue.
     * @throws java.util.NoSuchElementException if queue is empty.
     */
    public Item peek() {
        if (isEmpty()) throw new NoSuchElementException("Queue underflow");
        return first.item;
    }

   /**
     * Add the item to the queue.
     */
    public void enqueue(Item item) {
        Node oldlast = last;
        last = new Node();
        last.item = item;
        last.next = null;
        if (isEmpty()) first = last;
        else           oldlast.next = last;
        N++;
        assert check();
    }

   /**
     * Remove and return the item on the queue least recently added.
     * @throws java.util.NoSuchElementException if queue is empty.
     */
    public Item dequeue() {
        if (isEmpty()) throw new NoSuchElementException("Queue underflow");
        Item item = first.item;
        first = first.next;
        N--;
        if (isEmpty()) last = null;   // to avoid loitering
        assert check();
        return item;
    }

   /**
     * Return string representation.
     */
    public String toString() {
        StringBuilder s = new StringBuilder();
        for (Item item : this)
            s.append(item + " ");
        return s.toString();
    }

    // check internal invariants
    private boolean check() {
        if (N == 0) {
            if (first != null) return false;
            if (last  != null) return false;
        }
        else if (N == 1) {
            if (first == null || last == null) return false;
            if (first != last)                 return false;
            if (first.next != null)            return false;
        }
        else {
            if (first == last)      return false;
            if (first.next == null) return false;
            if (last.next  != null) return false;

            // check internal consistency of instance variable N
            int numberOfNodes = 0;
            for (Node x = first; x != null; x = x.next) {
               numberOfNodes++;
            }
            if (numberOfNodes != N) return false;

            // check internal consistency of instance variable last
            Node lastNode = first;
            while (lastNode.next != null) {
               lastNode = lastNode.next;
            }
            if (last != lastNode) return false;
        }

        return true;
    }


   /**
     * Return an iterator that iterates over the items on the queue in FIFO order.
     */
    public Iterator<Item> iterator()  {
        return new ListIterator();  
    }

    // an iterator, doesn't implement remove() since it's optional
    private class ListIterator implements Iterator<Item> {
        private Node current = first;

        public boolean hasNext()  { return current != null;                     }
        public void remove()      { throw new UnsupportedOperationException();  }

        public Item next() {
            if (!hasNext()) throw new NoSuchElementException();
            Item item = current.item;
            current = current.next;
            return item;
        }
    }


   /**
     * A test client.
     */
    public static void main(String[] args) {
        Queue<String> q = new Queue<String>();
        while (!StdIn.isEmpty()) {
            String item = StdIn.readString();
            if (!item.equals("-")) q.enqueue(item);
            else if (!q.isEmpty()) StdOut.print(q.dequeue() + " ");
        }
        StdOut.println("(" + q.size() + " left on queue)");
    }
}
[/mw_shl_code][mw_shl_code=java,true]/*************************************************************************
*  Compilation:  javac Stack.java
*  Execution:    java Stack < input.txt
*
*  A generic stack, implemented using a linked list. Each stack
*  element is of type Item.
*  
*  % more tobe.txt
*  to be or not to - be - - that - - - is
*
*  % java Stack < tobe.txt
*  to be not that or be (2 left on stack)
*
*************************************************************************/

import java.util.Iterator;
import java.util.NoSuchElementException;


/**
*  The <tt>Stack</tt> class represents a last-in-first-out (LIFO) stack of generic items.
*  It supports the usual <em>push</em> and <em>pop</em> operations, along with methods
*  for peeking at the top item, testing if the stack is empty, and iterating through
*  the items in LIFO order.
*  <p>
*  All stack operations except iteration are constant time.
*  <p>
*  For additional documentation, see <a href="/algs4/13stacks">Section 1.3</a> of
*  <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*/
public class Stack<Item> implements Iterable<Item> {
    private int N;          // size of the stack
    private Node first;     // top of stack

    // helper linked list class
    private class Node {
        private Item item;
        private Node next;
    }

   /**
     * Create an empty stack.
     */
    public Stack() {
        first = null;
        N = 0;
        assert check();
    }

   /**
     * Is the stack empty?
     */
    public boolean isEmpty() {
        return first == null;
    }

   /**
     * Return the number of items in the stack.
     */
    public int size() {
        return N;
    }

   /**
     * Add the item to the stack.
     */
    public void push(Item item) {
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        N++;
        assert check();
    }

   /**
     * Delete and return the item most recently added to the stack.
     * @throws java.util.NoSuchElementException if stack is empty.
     */
    public Item pop() {
        if (isEmpty()) throw new NoSuchElementException("Stack underflow");
        Item item = first.item;        // save item to return
        first = first.next;            // delete first node
        N--;
        assert check();
        return item;                   // return the saved item
    }


   /**
     * Return the item most recently added to the stack.
     * @throws java.util.NoSuchElementException if stack is empty.
     */
    public Item peek() {
        if (isEmpty()) throw new NoSuchElementException("Stack underflow");
        return first.item;
    }

   /**
     * Return string representation.
     */
    public String toString() {
        StringBuilder s = new StringBuilder();
        for (Item item : this)
            s.append(item + " ");
        return s.toString();
    }


    // check internal invariants
    private boolean check() {
        if (N == 0) {
            if (first != null) return false;
        }
        else if (N == 1) {
            if (first == null)      return false;
            if (first.next != null) return false;
        }
        else {
            if (first.next == null) return false;
        }

        // check internal consistency of instance variable N
        int numberOfNodes = 0;
        for (Node x = first; x != null; x = x.next) {
            numberOfNodes++;
        }
        if (numberOfNodes != N) return false;

        return true;
    }


   /**
     * Return an iterator to the stack that iterates through the items in LIFO order.
     */
    public Iterator<Item> iterator()  { return new ListIterator();  }

    // an iterator, doesn't implement remove() since it's optional
    private class ListIterator implements Iterator<Item> {
        private Node current = first;
        public boolean hasNext()  { return current != null;                     }
        public void remove()      { throw new UnsupportedOperationException();  }

        public Item next() {
            if (!hasNext()) throw new NoSuchElementException();
            Item item = current.item;
            current = current.next;
            return item;
        }
    }


   /**
     * A test client.
     */
    public static void main(String[] args) {
        Stack<String> s = new Stack<String>();
        while (!StdIn.isEmpty()) {
            String item = StdIn.readString();
            if (!item.equals("-")) s.push(item);
            else if (!s.isEmpty()) StdOut.print(s.pop() + " ");
        }
        StdOut.println("(" + s.size() + " left on stack)");
    }
}[/mw_shl_code]

评分

参与人数 2宅魂 +1 宅币 +23 贡献 +2 收起 理由
风音洛洛 + 20 + 2 o(* ̄▽ ̄*)ブ 发糖
没糖给你了 + 1 + 3 o(* ̄▽ ̄*)ブ 发糖

查看全部评分

博客什么的求人气 http://bimania.org
回复

使用道具 举报

该用户从未签到

20

主题

62

好友

1万

积分

第一章

积分
11375
发表于 2012-10-7 22:29:28 | 显示全部楼层
好吧~~~~~完全不懂的人飘过~~~~~~~~·隔行如隔山~~~~~~~··

不过想请教那个斯坦福的公开课怎样注册?

(标题是普林斯顿?)
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

258

主题

314

好友

3万

积分

第二章

积分
35715
 楼主| 发表于 2012-10-7 22:55:58 | 显示全部楼层

你说的是哪个?
Coursera上有好多学校的公开课的
注册应该不难吧?
博客什么的求人气 http://bimania.org
回复 支持 反对

使用道具 举报

签到天数: 2 天

连续签到: 1 天

[LV.1]初来乍到

30

主题

128

好友

2万

积分

第一章

积分
21456
发表于 2012-10-8 01:10:40 来自手机 | 显示全部楼层
只上网易公开课的飘过。。。
表示渣英文水准听不懂老师在说什么

只好找翻译过的了
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

258

主题

314

好友

3万

积分

第二章

积分
35715
 楼主| 发表于 2012-10-8 01:42:58 | 显示全部楼层
kenneth 发表于 2012-10-8 01:10
只上网易公开课的飘过。。。
表示渣英文水准听不懂老师在说什么

如果网易公开课基本只有视频
coursera是每周会有新的视频,然后还有课后题,作业什么的,比较有代入感
博客什么的求人气 http://bimania.org
回复 支持 反对

使用道具 举报

签到天数: 2 天

连续签到: 1 天

[LV.1]初来乍到

30

主题

128

好友

2万

积分

第一章

积分
21456
发表于 2012-10-8 01:55:20 来自手机 | 显示全部楼层
轻舟过 发表于 2012-10-8 01:42
如果网易公开课基本只有视频
coursera是每周会有新的视频,然后还有课后题,作业什么的,比较有代入感
...


那确实
后来那些课后相关我都是上网找的
幸亏还是找到一些咯
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

258

主题

314

好友

3万

积分

第二章

积分
35715
 楼主| 发表于 2012-10-8 10:43:28 | 显示全部楼层
kenneth 发表于 2012-10-8 01:55

那确实
后来那些课后相关我都是上网找的

以前coursera有些课有中文字幕的,现在上的一些课貌似都只有英文字幕
博客什么的求人气 http://bimania.org
回复 支持 反对

使用道具 举报

签到天数: 2 天

连续签到: 1 天

[LV.1]初来乍到

30

主题

128

好友

2万

积分

第一章

积分
21456
发表于 2012-10-8 10:56:43 来自手机 | 显示全部楼层
轻舟过 发表于 2012-10-8 10:43
以前coursera有些课有中文字幕的,现在上的一些课貌似都只有英文字幕

如果只看英文的话会不会有助于英文水平提高呢

但是如果无字幕我可就真心听不懂了。。

之前有看过哈佛马兰的公开课。

那老师语速真快。后来看了中文的才知道他说了那么多。。。。
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

258

主题

314

好友

3万

积分

第二章

积分
35715
 楼主| 发表于 2012-10-8 11:27:53 | 显示全部楼层
kenneth 发表于 2012-10-8 10:56
如果只看英文的话会不会有助于英文水平提高呢

但是如果无字幕我可就真心听不懂了。。

现在基本都有英文字幕的,看英文字幕应该对英语有帮助
博客什么的求人气 http://bimania.org
回复 支持 反对

使用道具 举报

签到天数: 2 天

连续签到: 1 天

[LV.1]初来乍到

30

主题

128

好友

2万

积分

第一章

积分
21456
发表于 2012-10-8 11:43:48 来自手机 | 显示全部楼层
轻舟过 发表于 2012-10-8 11:27
现在基本都有英文字幕的,看英文字幕应该对英语有帮助

。。。。我从疼讯下的就没有。。

话说你是做什么工作的。。

看你博客东西特别高端的样子。
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

258

主题

314

好友

3万

积分

第二章

积分
35715
 楼主| 发表于 2012-10-8 11:46:34 | 显示全部楼层
kenneth 发表于 2012-10-8 11:43
。。。。我从疼讯下的就没有。。

话说你是做什么工作的。。

从腾讯下是什么意思?腾讯也有提供公开课资源下载么

我还没工作呢,计算机专业在读
博客什么的求人气 http://bimania.org
回复 支持 反对

使用道具 举报

签到天数: 2 天

连续签到: 1 天

[LV.1]初来乍到

30

主题

128

好友

2万

积分

第一章

积分
21456
发表于 2012-10-8 11:51:09 来自手机 | 显示全部楼层
轻舟过 发表于 2012-10-8 11:46
从腾讯下是什么意思?腾讯也有提供公开课资源下载么

我还没工作呢,计算机专业在读 ...

喔。。。那一定是学霸之类的哈哈。。

其实我也是计算机在读。。

腾讯的qq旋风里有公开课资源。。

我是会员所以可以离线下载速度快。。

却没有挂字幕所以就果断转战网易公开课。
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

258

主题

314

好友

3万

积分

第二章

积分
35715
 楼主| 发表于 2012-10-8 12:09:34 | 显示全部楼层
kenneth 发表于 2012-10-8 11:51
喔。。。那一定是学霸之类的哈哈。。

其实我也是计算机在读。。

不是学霸啦。

感觉搞个博客谢谢记录下自己学过的东西挺好的,只是我的博客好冷清,已经好久没有人留言了

没字幕的话可以去射手网上搜搜看有没有外挂字幕的,不过基本上看看在线的视频就够了
博客什么的求人气 http://bimania.org
回复 支持 反对

使用道具 举报

签到天数: 2 天

连续签到: 1 天

[LV.1]初来乍到

30

主题

128

好友

2万

积分

第一章

积分
21456
发表于 2012-10-8 12:28:59 来自手机 | 显示全部楼层
轻舟过 发表于 2012-10-8 12:09
不是学霸啦。

感觉搞个博客谢谢记录下自己学过的东西挺好的,只是我的博客好冷清,已经好久没有人留言了 ...

这样
可是又是独立域名又是独立主机不是要钱么
我的博客也是一样啦不过总是疲于更新
像这样的博客很多但是都没有什么读者所以就冷清了

射手网是个好网站
我去找过但是没找到。。。
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

该用户从未签到

258

主题

314

好友

3万

积分

第二章

积分
35715
 楼主| 发表于 2012-10-8 12:55:13 | 显示全部楼层
kenneth 发表于 2012-10-8 12:28
这样
可是又是独立域名又是独立主机不是要钱么
我的博客也是一样啦不过总是疲于更新

其实有免费资源啦,注册.tk域名是免费的,主机的话应该也有免费主机
其实我也懒得更新,一个月也只更新了一点
博客什么的求人气 http://bimania.org
回复 支持 反对

使用道具 举报

本版积分规则

小黑屋|手机版|技术宅(Z站|基宅) ( 粤ICP备18082987号-1 )

GMT+8, 2025-5-2 10:30 , Processed in 0.087148 second(s), 18 queries , Redis On.

Copyright © 2018 技术宅社区

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表