SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 313 Weeks Ago, 6 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 12 Dec 2017 My Price 10.00

implement a very basic Least Recently Used (LRU) cached

I need to implement a very basic Least Recently Used (LRU) cached using LinkedList. I have to use the following framework:

  • theSize: The current number of items in the cache
  • capacity: The capacity of the cache (the maximum number of items which can be stored inthe cache)
  • head: reference to the head node
  • Tail: reference to the tail node 

and I need to implement the the following three parts:

  1. The nested CacheNode<K,V> class: This class encapsulates the building block of a cache node. It contains a key and value, as well as a reference to both the next and previous nodes in the list. K is a parametric (generic) type for the key and V is a parametric type for the value. 
  2. LRUGet(K key): This method returns the value for a given key in the cache and moves the node which contains the key to the end of the list (because it is most recently accessed). The method returns null if there is no node with the given key. 
  3. LRUPut(K key, V value): This method adds a new node with the given key and value to the end of the list. If the cache is full, the least recently used node (the first node after head) is removed to make room for new node. Since duplicate keys are not allowed, the method must first check to see if a node with a given key already exists in the cache and if so, it updates its value and move the node to the end of the list. 

 

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

    }
    

}


Answers

(5)
Status NEW Posted 12 Dec 2017 12:12 PM My Price 10.00

-----------  ----------- 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

Not Rated(0)