Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this I need to create size, delete and keys. I\'m not sure what information

ID: 3662290 • Letter: F

Question

For this I need to create size, delete and keys. I'm not sure what information I need to include from what I have and all my research hasn't given me much and I'm not getting any output. I need editing help and to point out where I'm going wrong.

My code:

import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class SequentialSearchST<Key, Value> {
   private int N;           // number of key-value pairs
    private Node first;      // the linked list of key-value pairs

    // a helper linked list data type
    private class Node {
        private Key key;
        private Value val;
        private Node next;

        public Node(Key key, Value val, Node next) {
            this.key = key;
            this.val = val;
            this.next = next;
        }
    }
    public SequentialSearchST() {
    }
    public int size() {
       //Part 1
        return N;
    }
    public boolean isEmpty() {
        return size() == 0;
    }
    public boolean contains(Key key) {
        if (key == null) throw new NullPointerException("argument to contains() is null");
        return get(key) != null;
    }
    public Value get(Key key) {
        if (key == null) throw new NullPointerException("argument to get() is null");
        for (Node x = first; x != null; x = x.next) {
            if (key.equals(x.key))
                return x.val;
        }
        return null;
    }
    public void put(Key key, Value val) {
        if (key == null) throw new NullPointerException("first argument to put() is null");
        if (val == null) {
            delete(key);
            return;
        }

        for (Node x = first; x != null; x = x.next) {
            if (key.equals(x.key)) {
                x.val = val;
                return;
            }
        }
        first = new Node(key, val, first);
        N++;
    }
    public void delete(Key key) {
       //Part 2?
        if (key == null) throw new NullPointerException("argument to delete() is null");
        first = delete(first, key);
    }
    private Node delete(Node x, Key key) {
       //or is this Part 2?
        if (x == null) return null;
        if (key.equals(x.key)) {
            N--;
            return x.next;
        }
        x.next = delete(x.next, key);
        return x;
    }
    public Iterable<Key> keys() {
       //Part 3
        Queue<Key> queue = new Queue<Key>();
        for (Node x = first; x != null; x = x.next)
            queue.enqueue(x.key);
        return queue;
    }


    public static void main(String[] args) {
        SequentialSearchST<String, Integer> st = new SequentialSearchST<String, Integer>();
      
        st.put("one", 1);
        st.put("two", 2);
        st.put("three", 3);
        st.put("four", 4);
      
        for (int i = 0; !StdIn.isEmpty(); i++) {
            String key = StdIn.readString();
            st.put(key, i);
        }
        for (String s : st.keys())
            StdOut.println(s + " " + st.get(s));
    }

}

Explanation / Answer

package edu.princeton.cs.algs4;

import edu.princeton.cs.stdlib.StdIn;

import edu.princeton.cs.stdlib.StdOut;

public class SequentialSearchST<Key, Value> {

private int N;

private Node first;

private class Node {

private Key key;

private Value val;

private Node next;

public Node(Key key, Value val, Node next) {

this.key = key;

this.val = val;

this.next = next;

}

}

public int size() {

return N;

}

public boolean isEmpty() {

return size() == 0;

}

public boolean contains(Key key) {

return get(key) != null;

}

public Value get(Key key) {

for (Node x = first; x != null; x = x.next) {

if (key.equals(x.key))

return x.val;
}

return null;

}

public void put(Key key, Value val) {

if (val == null) {

delete(key);

return;

}

for (Node x = first; x != null; x = x.next)

if (key.equals(x.key)) {

x.val = val;

return;

}

first = new Node(key, val, first);

N++;

}

public void delete(Key key) {

first = delete(first, key);

}

private Node delete(Node x, Key key) {

if (x == null)

return null;

if (key.equals(x.key)) {

N--;

return x.next;

}

x.next = delete(x.next, key);

return x;

}

public Iterable<Key> keys() {

Queue<Key> queue = new Queue<Key>();

for (Node x = first; x != null; x = x.next)

queue.enqueue(x.key);

return queue;
}

public static void main(String[] args) {

SequentialSearchST<String, Integer> st = new SequentialSearchST<String, Integer>();

for (int i = 0; !StdIn.isEmpty(); i++) {

String key = StdIn.readString();

st.put(key, i);

}

for (String s : st.keys())

StdOut.println(s + " " + st.get(s));

}

}