The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I need to implement a very basic Least Recently Used (LRU) cached using LinkedList. I have to use the following framework:
and I need to implement the the following three parts:
I'm attaching my code so far, I'm looking a response with the actual code block and a brief explanation to make sense of it. I appreciate your help!/**
*
* @author esahe2
*
* @param <K> The type of the key
* @param <V> The type of the value
*/
import java.util.HashMap;
import java.util.LinkedList;
import static java.util.Objects.hash;
import java.util.Queue;
public class LRULinkedCache<K,V>
{
/*************
* attributes
************/
/** current number of items in cache */
private int theSize;
/** The capacity of cache. */
private int capacity;
/** reference to list header node */
private CacheNode<K,V> head;
/** reference to list tail node */
private CacheNode<K,V> tail;
/***************
* constructors
**************/
/**
* return a new, empty cache with a given capacity
*/
public LRULinkedCache(int capacity)
{
this.capacity=capacity;
// empty this LinkedList
clear();
}
/**********
* methods
*********/
/**************************************
* methods inherited from class Object
*************************************/
/**
* return a String representation of the LinkedList
*
* items are listed in order from beginning to end in comma-delimited fashion
*/
public String toString()
{
String s = "";
CacheNode<K,V> current= head.next;
while(current!=tail)
{
s+= "("+current.key +"," +current.value+")";
if (current.next!= tail)
s+=",";
current= current.next;
}
return s;
}
/**********************************************
* methods inherited from interface Collection
*********************************************/
/**
* empty the LRUCache
* size will be set to zero
*/
public void clear()
{
// reset header node
head = new CacheNode<K,V>(null,null,null ,null);
// reset tail node
tail = new CacheNode<K,V>(null,null,head, null);
head.next=tail;
tail.previous=head;
// reset size to 0
theSize = 0;
}
/**
* return the number of items in the cache
*/
public int size()
{
return theSize;
}
/**
* return true if the cache is empty
*/
public boolean isEmpty()
{
return theSize == 0;
}
/**
* This operation returns the value for a given key in the cache. It returns null if the data is not currently in the cache.
* It also moves the data that is accessed to the end of the list and inserts it before tail
*/
public V LRUGet(K key)
{
int bucketIndex = hash(key.hashCode());
if (table[bucketIndex] != null) {
LinkedList<CacheNode<K, V>> bucket = table[bucketIndex];
for (CacheNode<K, V> entry: bucket)
if (entry.getKey().equals(key))
return entry.getValue();
}
return null;
}
/**
* puts a new node with key and value in the cache and adds it to the end of the cache
* If the cache is full, it removes the first node (The least recently used node)before adding the new node.
* If a node with the given key already exists in the cache, it updates the value for the key and moves the node with the key to the
* end of the cache
* @param key
* @param value
*/
public void LRUPut(K key, V value)
{
//To Do: Your implementation goes here
}
/**
* nested class ListNode
*
* encapsulates the fundamental building block of a LRU cache node
* contains a key and value, as well as references to both the next and previous nodes
* in the cache
*K is the type of the key and V is the type of value
*/
private static class CacheNode<K,V>
{
/*************
* attributes
************/
//TODO: define attributes and constructor for CacheNode
}
}
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly