Skip to content

Latest commit

 

History

History
55 lines (40 loc) · 905 Bytes

Ex_1_3_21.md

File metadata and controls

55 lines (40 loc) · 905 Bytes
title date draft tags categories
Algorithm4 Java Solution 1.3.21
2019-07-04 05:47:10 +0800
false
JAVA
TECH
archives

1.3.21

Problem:

Write a method find() that takes a linked list and a string key as arguments and returns true if some node in the list has key as its item field, false otherwise.

Solution:

LinkedList implements iterable

    private class LinkedListIterator implements Iterator<Item> {

      Node cur = first;

      @Override
      public boolean hasNext() {
        return cur != null;
      }

      @Override
      public Item next() {
        Item item = cur.item;
        cur = cur.next;
        return item;
      }
    }

find method:

  public static boolean find(_LinkedList<String> l, String key) {
    for (String s : l)
      if (s.equals(key))
        return true;
    return false;
  }

Reference: