Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q1: Complete the following code of the map implemented using linked list:[6 marks] class Map { private MapNode head; private MapNode tail; private int size

Q1: Complete the following code of the map implemented using linked list:[6 marks]

class Map

{

private MapNode head;

private MapNode tail;

private int size = 0;

public class MyEntry {

private int key;

private double value;

public MyEntry(int key, double value) {

this.key = key;

this.value = value;

}

public int getKey() { return key; }

public double getValue() { return value; }

public double setValue(double value) {

// complete

}

}//end of entry class

/* Class MapNode */

class MapNode

{

MapNode prev;

MapNode next;

MyEntry entry;

/* Constructor */

public MapNode()

{

prev = null;

next = null;

entry = null;

}

/* Constructor */

public MapNode(MapNode p, MapNode n, MyEntry e )

{

prev = p;

next = n;

entry = e;

}

// Accessor methods

public MyEntry getEntry() { return entry; }

public MapNode getPrev() { return prev; }

public MapNode getNext() { return next; }

//update methods

public void setEntry (MyEntry e) { entry = e; }

public void setPrev(MapNode p) { prev = p; }

public void setNext(MapNode n) { next = n; }

}

/* Constructor */

public Map() {

head=null;

tail=null;

size = 0;

}

/* Function to check if Map is empty */

public boolean isEmpty() { return size == 0; }

public int size( ) { return size; }

public double remove(int key){

MapNode n= head;

for (int j=0; j < size; j++){

if (n.getEntry().getKey( ) == key){

double temp= n.getEntry().getValue();

// remove node

MapNode p= n.getPrev();

p.setNext(n.getNext());

n.getNext().setPrev(p);

size--;

return temp;

}

n=n.getNext ();

}

return -1;

}

public double get(int key){

//complete

return -1;

}

public double put(int key, double v){

MyEntry entry = new MyEntry(key, v);

MapNode n = head ;

for (int j=0; j < size; j++){

if(n.getEntry().getKey() == key){

double temp = n.getEntry().getValue();

n.getEntry().value = v;}

return temp;

n= n.getNext();

}

//key is not in the map

MapNode node = new MapNode();

If(isEmpty()){

head= node;

tail= node;

node.setEntry(entry);

Size++;

return node.getEntry().getValue();

}

// not empty

node.setPrev(tail);

tail.setNext(node);

tail= node;

node.setEntry(entry);

Size++;

return node.getEntry().getValue();

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions