mirror of
https://git.adityakumar.xyz/data-structures.git
synced 2024-11-13 22:49:44 +00:00
add dictionary stuff
This commit is contained in:
parent
03a169dd13
commit
a5f0cb8f4c
4 changed files with 129 additions and 0 deletions
28
java/Dictionary/Dictionary.java
Normal file
28
java/Dictionary/Dictionary.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
/** The Dictionary abstract class */
|
||||
public interface Dictionary<Key, E> {
|
||||
|
||||
/** Reinitialize dictionary */
|
||||
public void clear();
|
||||
|
||||
/** Insert a record.
|
||||
* @param k The key for the record being inserted.
|
||||
* @param e The record being inserted. */
|
||||
public void insert(Key k, E e);
|
||||
|
||||
/** Remove and return a record.
|
||||
* @param k The key of the record to be removed.
|
||||
* @return A matching record. If multiple records match "k", remove an arbitrary one. Return null if no record with key "k" exists. */
|
||||
public E remove(Key k);
|
||||
|
||||
/** Remove and return arbitrary record from dictionary.
|
||||
* @return the record removed, or null if none exists. */
|
||||
public E removeAny();
|
||||
|
||||
/** @return A record matching "k" (null if none exists).
|
||||
* If multiple records match, return an arbitrary one.
|
||||
* @param k The key of the record to find */
|
||||
public E find(Key k);
|
||||
|
||||
/** @return The number of records in the dictionary. */
|
||||
public int size();
|
||||
};
|
24
java/Dictionary/KVpair.java
Normal file
24
java/Dictionary/KVpair.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
/** Container class for a key-value pair */
|
||||
class KVpair<Key, E> {
|
||||
private Key k;
|
||||
private E e;
|
||||
|
||||
/** Constructors */
|
||||
KVpair() {
|
||||
k = null;
|
||||
e = null;
|
||||
}
|
||||
|
||||
KVpair(Key kval, E eval) {
|
||||
k = kval; e = eval;
|
||||
}
|
||||
|
||||
/** Data member access functions */
|
||||
public Key key() {
|
||||
return k;
|
||||
}
|
||||
|
||||
public E value() {
|
||||
return e;
|
||||
}
|
||||
}
|
18
java/Dictionary/Payroll.java
Normal file
18
java/Dictionary/Payroll.java
Normal file
|
@ -0,0 +1,18 @@
|
|||
/** A simple payroll entry with ID, name, address fields */
|
||||
class Payroll {
|
||||
private Integer ID;
|
||||
private String name;
|
||||
private String address;
|
||||
|
||||
/** Constructor */
|
||||
Payroll(int inID, String inname, String inaddr) {
|
||||
id = inID;
|
||||
name = inname;
|
||||
address = inaddr;
|
||||
}
|
||||
|
||||
/** Data member access functions */
|
||||
public Integer getID() { return ID; }
|
||||
public String getname() { return name; }
|
||||
public String getaddr() { return address; }
|
||||
}
|
59
java/Dictionary/UALdictionary.java
Normal file
59
java/Dictionary/UALdictionary.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
/** Dictionary implemented by unsorted array-based list */
|
||||
class UALdictionary<Key, E> implements Dictionary<Key, E> {
|
||||
private static final int defaultSize = 10; // Default size
|
||||
private AList<KVpair<Key, E>> list; // To store dictionary
|
||||
|
||||
/** Constructors */
|
||||
UALdictionary() {
|
||||
this(defaultSize);
|
||||
}
|
||||
|
||||
UALdictionary(int sz) {
|
||||
list = new AList<KVpair<Key, E>>(sz);
|
||||
}
|
||||
|
||||
/** Reintialize */
|
||||
public void clear() {
|
||||
list.clear();
|
||||
}
|
||||
|
||||
/** Insert an element: append to list */
|
||||
public void insert(Key k, E e) {
|
||||
KVpair<Key, E> temp = new KVpair<Key, E>(k , e);
|
||||
list.append(temp);
|
||||
}
|
||||
|
||||
/** Use sequential search to find the element to remove */
|
||||
public E remove(Key k) {
|
||||
E temp = find(k);
|
||||
if (temp != null) list.remove();
|
||||
return temp;
|
||||
}
|
||||
|
||||
/** Remove the last element */
|
||||
public E removeAny() {
|
||||
if (size != 0) {
|
||||
list.moveToEnd();
|
||||
list.prev();
|
||||
KVpair<Key, E> e = list.remove();
|
||||
|
||||
return e.value();
|
||||
}
|
||||
else return null;
|
||||
}
|
||||
|
||||
/** Find k using sequential search
|
||||
* @return Record with key value k */
|
||||
public E find(Key k) {
|
||||
for (list.moveToStart(); list.curPos() < list.length(); list.next()) {
|
||||
KVpair<Key, E> temp = list.getValue();
|
||||
if (k == temp.key()) return temp.value();
|
||||
}
|
||||
return null; // "k" does not appear in dictionary
|
||||
}
|
||||
|
||||
/** @return List size */
|
||||
public int size() {
|
||||
return list.length();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue