|
|
|
Example Map content:
|
Implementation with list:
public class ListElem
{
public String key;
public String value;
public ListElem prev; // Double linked list for easy deletion
public ListElem next;
public ListElem(String k, String v)
{
key = k;
value = v;
prev = null;
next = null;
}
public String toString()
{
return( "(" + key + "," + value + ")" );
}
}
|
|
public class ListMap extends List
{
|
public class ListElem<KeyType, ValueType> // ListElem class with 2 parameters
{
public KeyType key;
public ValueType value;
public ListElem<KeyType,ValueType> prev;
public ListElem<KeyType,ValueType> next;
public ListElem(KeyType k, ValueType v)
{
key = k;
value = v;
prev = null;
next = null;
}
public String toString()
{
return( "(" + key + "," + value + ")" );
}
}
|
public class List<KeyType, ValueType>
{
public ListElem<KeyType, ValueType> head;
public int N;
public List()
{
head = null; // Start with empty list
N = 0;
}
// *************************************************
// delete(p): delete p
// *************************************************
public void delete(ListElem<KeyType, ValueType> p)
{
ListElem<KeyType, ValueType> prevElem, nextElem;
if ( N == 0 )
{
return ; // Nothing to delete
}
else if ( N == 1 ) // Delete last element ==> empty list
{
head = null;
}
else if ( p.prev == null ) // Delete FIRST element
{
System.out.println("Delete: " + p.key);
nextElem = p.next;
nextElem.prev = null;
head = nextElem; // The second is the new "first element"
}
else if ( p.next == null ) // Delete LAST element
{
System.out.println("Delete: " + p.key);
prevElem = p.prev;
prevElem.next = null;
}
else // Delete a "middle element"
{
System.out.println("Delete: " + p.key);
prevElem = p.prev;
nextElem = p.next;
nextElem.prev = p.prev;
prevElem.next = p.next;
}
N--;
}
// -------------------------------
// insert(v): insert at head
// -------------------------------
public void insert(ListElem<KeyType, ValueType> p)
{
if ( N == 0 )
{
p.next = null;
p.prev = null;
head = p;
}
else
{
p.next = head;
p.prev = null;
head.prev = p;
head = p;
}
N++;
}
public String toString()
{
String r = "";
ListElem<KeyType, ValueType> p;
p = head; // Very important: start at first element
r = r + "{";
while ( p != null )
{
r = r + p + " ";
p = p.next;
}
return(r);
}
}
|
Instead of:
DataType varName
Use:
DataType< Actual_Type, ... > varName
|
Example:
List<String, String> L1 = new List<String, String> () ;
List<String, Integer> L2 = new List<String, Integer> () ;
|
(Programs will a lot of these notation is terribly difficult to read...)
public class ListMap<KeyType, ValueType> extends List<KeyType, ValueType>
{
|
I am more concern to teach data structures and algorithm to manage the data structure.
|
The purpose of this course is:
|
It's not above how to make programs safer using parameterized types
But I do want to show you how to convert a NON parameterized typed class into a parameterized typed one...
So you can convert it yourself: you can parameterize every data structures taught in the rest of the course.
|