Deprecated: Creation of dynamic property wpdb::$categories is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$post2cat is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$link2cat is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 544

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 960

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 980

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 992

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1003

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 199

Deprecated: Creation of dynamic property Automattic\Jetpack\Connection\Manager::$error_handler is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-connection/src/class-manager.php on line 91

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Instagram_Gallery::$is_wpcom is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-instagram-gallery.php on line 23

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Mailchimp::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php on line 17

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Tweetstorm_Gather::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php on line 21

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Tweetstorm_Gather::$is_wpcom is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php on line 22

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Gutenberg_Available_Extensions::$wpcom_is_site_specific_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/gutenberg-available-extensions.php on line 20

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Memberships::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php on line 23

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Memberships::$wpcom_is_site_specific_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php on line 24

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Subscribers::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php on line 15

Deprecated: Creation of dynamic property Automattic\Jetpack\Sync\Queue::$random_int is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-queue.php on line 38

Deprecated: Creation of dynamic property Automattic\Jetpack\Sync\Queue::$random_int is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-queue.php on line 38

Deprecated: Creation of dynamic property WPCOM_Widget_Goodreads::$shelves is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/modules/widgets/goodreads.php on line 33

Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /var/www/twodee/blog/wp-includes/script-loader.php on line 348

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_classic_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 347

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 349

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block_side is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 350

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$panels_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 351

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$used_block_buttons is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 354

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198
» CS 245 Lecture 21 – Linked List, Stack, and Queue

teaching machines

CS 245 Lecture 21 – Linked List, Stack, and Queue

April 15, 2014 by . Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs245, lectures, spring 2014.

Agenda

Program This

Write one of the following for LinkedList:

Program This

You’ve got an expression in “postfix” form:

Write pseudocode to parse and evaluate such expressions.

Code

HashSet.java

package lecture21;

import java.util.ArrayList;

public class HashSet<T> {
  private ArrayList<T>[] items;
  
  public HashSet() {
    items = new ArrayList[5];
  }
  
  public void add(T item) {
    int i = Math.abs(item.hashCode()) % items.length;
    if (items[i] == null) {
      items[i] = new ArrayList<T>();
    }
    if (!items[i].contains(item)) {
      items[i].add(item);
    }
  }
  
  public void remove(T item) {
    int i = Math.abs(item.hashCode()) % items.length;
    if (items[i] != null) {
      items[i].remove(item);
    }
  }
  
  public boolean contains(T item) {
    int i = Math.abs(item.hashCode()) % items.length;
    return items[i] != null && items[i].contains(item);
  }
  
  public Iterator getIterator() {
    return new Iterator();
  }
  
  public class Iterator {
    private int hashIndexOfItemToRetrieve;
    private int listIndexOfItemToRetrieve;
    
    public Iterator() {
      hashIndexOfItemToRetrieve = 0;
      listIndexOfItemToRetrieve = -1;
      advanceToNextItemToRetrieve();
    }
    
    public boolean hasNext() {
      return hashIndexOfItemToRetrieve < items.length;
    }
    
    public T next() {
      T item = items[hashIndexOfItemToRetrieve].get(listIndexOfItemToRetrieve);
      advanceToNextItemToRetrieve();
      return item;
    }
    
    private void advanceToNextItemToRetrieve() {
      ++listIndexOfItemToRetrieve;
      
      while (hashIndexOfItemToRetrieve < items.length &&
             (items[hashIndexOfItemToRetrieve] == null ||
              listIndexOfItemToRetrieve >= items[hashIndexOfItemToRetrieve].size())) {
        listIndexOfItemToRetrieve = 0;
        ++hashIndexOfItemToRetrieve;
      }
    }
  }
  
  public static void main(String[] args) {
    HashSet<String> set = new HashSet<String>();
    set.add("a");
    System.out.println(set.contains("a"));
    set.remove("a");
    System.out.println(set.contains("a"));
    
    set.add("a");
    set.add("b");
    set.add("c");
    set.add("d");
    set.add("e");
    set.add("f");
    set.add("g");
    set.add("z");
    set.add("z");
    set.add("ifrim");
    set.add("racecar");
    
    System.out.println(set.contains("racecar"));
    System.out.println(set.contains("tacocat"));
    
    HashSet<String>.Iterator i = set.getIterator();
    while (i.hasNext()) {
      System.out.println(i.next());
    }
  }
}

LinkedList.java

package lecture21;

import java.util.NoSuchElementException;

public class LinkedList<T> {
  private Node<T> head;
  private Node<T> tail;

  public LinkedList() {
    head = new Node<T>(null, null, null);
    tail = new Node<T>(null, null, null);
    head.next = tail;
    tail.prev = head;
  }

  /**
   * Adds the new item at the end of this list.
   * 
   * @param newItem
   * The item to add.
   */
  public void prepend(T newItem) {
    Node<T> newNode = new Node<T>(newItem, head.next, head);
    head.next = newNode;
    newNode.next.prev = newNode;
  }

  public void append(T newItem) {
    Node<T> newNode = new Node<T>(newItem, tail, tail.prev);
    tail.prev = newNode;
    newNode.prev.next = newNode;
  }

  public boolean isEmpty() {
    return head.next == tail;
  }

  public int size() {
    int count = 0;
    Node<T> thisNode = head;
    while (thisNode.next != tail) {
      ++count;
      thisNode = thisNode.next;
    }
    return count;
  }

  public T getFirst() {
    if (isEmpty()) {
      throw new NoSuchElementException();
    }
    return head.next.value;
  }

  public T getLast() {
    if (isEmpty()) {
      throw new NoSuchElementException();
    }
    return tail.prev.value;
  }

  public T removeFirst() {
    if (isEmpty()) {
      throw new NoSuchElementException();
    }
    T value = head.next.value;
    head.next = head.next.next;
    head.next.prev = head;
    return value;
  }

  public T removeLast() {
    if (isEmpty()) {
      throw new NoSuchElementException();
    }
    T value = tail.prev.value;
    tail.prev = tail.prev.prev;
    tail.prev.next = tail;
    return value;
  }

  public Iterator getIterator() {
    return new Iterator();
  }

  public class Iterator {
    private Node<T> current;

    public Iterator() {
      current = head;
    }

    public boolean hasNext() {
      return current.next != tail;
    }

    public T next() {
      current = current.next;
      return current.value;
    }
  }

  public static class Node<T> {
    private T value;
    private Node<T> next;
    private Node<T> prev;

    public Node(T value,
                Node<T> next,
                Node<T> prev) {
      this.value = value;
      this.next = next;
      this.prev = prev;
    }
  }
}

SnakesOnAFrame.java

package lecture21;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import lecture21.LinkedList.Iterator;

public class SnakesOnAFrame extends JFrame implements KeyListener {
  private static final int TILE_WIDTH = 25;
  private static final int TILE_HEIGHT = 25;
  private int heightInTiles;
  private int widthInTiles;
  private SnakesPanel panel;
  private LinkedList<Point> snake;

  private HashSet<Point> eggs;

  public SnakesOnAFrame() {
    eggs = new HashSet<Point>();

    panel = new SnakesPanel();
    add(panel);

    setFocusable(true);
    requestFocusInWindow();
    addKeyListener(this);

    setSize(1024, 600);

    heightInTiles = (int) (getSize().getHeight() / TILE_HEIGHT);
    widthInTiles = (int) (getSize().getWidth() / TILE_WIDTH);

    generateEggs();
    snake = new LinkedList<Point>();
    snake.append(new Point(6, 9));

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
  }

  private void generateEggs() {
    Random g = new Random();
    for (int i = 0; i < 20; ++i) {
      int y = g.nextInt(heightInTiles);
      int x = g.nextInt(widthInTiles);
      eggs.add(new Point(x, y));
    }
  }

  class SnakesPanel extends JPanel {
    public void paintComponent(Graphics g) {
      super.paintComponent(g);

      // draw eggs
      g.setColor(Color.ORANGE);
      HashSet<Point>.Iterator i = eggs.getIterator();
      while (i.hasNext()) {
        Point egg = i.next();
        g.fillOval(egg.x * TILE_WIDTH, egg.y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
      }

      g.setColor(Color.GREEN);
      LinkedList<Point>.Iterator iSnake = snake.getIterator();
      while (iSnake.hasNext()) {
        Point segment = iSnake.next();
        g.fillRoundRect(segment.x * TILE_WIDTH, segment.y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, 20, 20);
      }
    }
  }

  public static void main(String[] args) {
    new SnakesOnAFrame();
  }

  @Override
  public void keyPressed(KeyEvent arg0) {
  }

  @Override
  public void keyReleased(KeyEvent event) {
    Point head = new Point(snake.getFirst());
    
    switch (event.getKeyCode()) {
      case KeyEvent.VK_UP:
        --head.y;
        break;
      case KeyEvent.VK_DOWN:
        ++head.y;
        break;
      case KeyEvent.VK_LEFT:
        --head.x;
        break;
      case KeyEvent.VK_RIGHT:
        ++head.x;
        break;
    }

    // Insert the new head.
    snake.prepend(head);

    // Is there an egg under the head?
    if (!eggs.contains(head)) {
      snake.removeLast();
    } else {
      eggs.remove(head);
    }

    panel.repaint();
  }

  @Override
  public void keyTyped(KeyEvent arg0) {
  }
}

Stack.java

package lecture21;

public class Stack<T> {
  private LinkedList<T> items;
  
  public Stack() {
    items = new LinkedList<T>();
  }
  
  public boolean isEmpty() {
    return items.isEmpty();
  }
  
  public void push(T item) {
    items.append(item);
  }
  
  public T pop() {
    return items.removeLast();
  }
  
  public T peek() {
    return items.getLast();
  }
}

Valhalla.java

package lecture21;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Valhalla {
  public static void main(String[] args) throws FileNotFoundException {
    Scanner in = new Scanner(new File("/Users/johnch/Desktop/foo.html"));
    in.useDelimiter("(\\A|>)[^<]*(\\Z|<)");

    Stack<String> opens = new Stack<String>();

    while (in.hasNext()) {
      String element = in.next();
      System.out.println(element);
      // if it's an opener
      // push it
      // else
      // if there's no top
      // stack underflow -- invalid
      // elsif it corresponds to the top
      // pop -- valid
      // else bad correspondence

      if (!element.startsWith("/")) {
        opens.push(element);
      } else if (opens.isEmpty()) {
        System.out.println("stack underflow");
        return;
      } else {
        if (opens.peek().startsWith(element.substring(1))) {
          opens.pop();
        } else {
          System.out.println("got a " + element + " but waiting for a " + opens.peek());
          return;
        }
      }
    }

    if (opens.isEmpty()) {
      System.out.println("wee!");
    } else {
      System.out.println("unclosed " + opens.peek());
    }
  }
}

Haiku

Jenga is a queue
I map my name to Boardwalk
Discard piles are stacks