Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

23 (34 pts) Implement the methods of the ADT Bag of integers using a linked list. A Bag is just a container for a collection

image text in transcribed
image text in transcribed
23 (34 pts) Implement the methods of the ADT Bag of integers using a linked list. A Bag is just a container for a collection of items (analogy: a bag of keys) with the following behaviors: -- The positions of data items do not matter. For instance, (3, 2, 10,6) is equivalent to {2, 3, 6, 10) -- There can be duplicate items (e.g., {7,2, 10, 7,5}) You may assume all required libraries have been imported. Your task is to implement the ADT Bag class with the following public methods: (10pts). public void addint item): add an item to the Bag (you may insert it at the front - the order of insertions does not matter). You may draw some pictures to help you implement methods correctly, e.g. adding an item at the front of the link: -12 (10pts). public boolean remove(int item): remove one occurrence of item (if any) from the Bag. Return true if removal is successful; otherwise return false. (10pts). public int grab(: get an item at random, without removing it, which reflects the fact that item orders do not matter (and we can't say "get the 5th item in the Bag"). Hint: Math.random( generates doubles between 0 (inclusive) and 1 (exclusive). Throw an exception when the user attempts to get an item from an empty bag. (2pts). public int size(): return the number of items in the Bag (2pts). public boolean isEmpty: indicate whether no items are stored in the Bag public class Bag public class Node { int item; Node next; // default value is null private Node head; private int size; public Bag() // constructor { head = null; size = 0; } 17 your implementation - Start with easy ones public int size() // OK to use "size" for a method & an instance variable

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions