From 7b224dfb10b25653d927fb9ce1b1261c2d308f56 Mon Sep 17 00:00:00 2001 From: Matthew Strapp Date: Wed, 20 Nov 2019 07:36:51 -0600 Subject: Do lab 10 --- csci1913/Java/lab10_strap012.java | 53 +++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) (limited to 'csci1913/Java/lab10_strap012.java') diff --git a/csci1913/Java/lab10_strap012.java b/csci1913/Java/lab10_strap012.java index c8fea79..06c0dfa 100644 --- a/csci1913/Java/lab10_strap012.java +++ b/csci1913/Java/lab10_strap012.java @@ -7,30 +7,69 @@ class AssociationList { key=k; value=v; next=n; } } + private Node head; public AssociationList() { - Node head = new Node("HEAD", "HEAD", null); + head = new Node(null, null, null); } public void delete(Key key) { + Node left = head; + Node right = head.next; + while (right!=null) { + if (isEqual(right.key, key)) { + left.next=right.next; + break; + } else { + left=right; right=right.next; + } + } } public Value get(Key key) { - + Node where=head.next; + while (where!=null) { + if (isEqual(where.key,key)) { + return where.value; + } else { + where=where.next; + } + } //No matching key found + throw new IllegalArgumentException(); } private boolean isEqual(Key leftKey, Key rightKey) { if (leftKey == null || rightKey == null) { return leftKey == rightKey; } else { - return leftkey.equals(rightKey); + return leftKey.equals(rightKey); } } public boolean isIn(Key key) { - + Node where = head.next; + while (where != null) { + if (isEqual(where.key, key)) { + return true; + } else { + where = where.next; + } + } // No matching key found + return false; } public void put(Key key, Value value) { - + Node where=head.next; + boolean found=false; + while (where!=null){ + if(isEqual(key, where.key)) { + where.value=value; + found=true; + break; + } else { + where=where.next; + } + } //End of while + if (!found) { + Node temp=head.next; + head.next= new Node(key, value, temp); + } } - - } // -- cgit v1.2.3