Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a method addBefore for OurLinkedList. This method takes 2 values x and y as arguments. It searches the list for x and adds y

Implement a method addBefore for OurLinkedList. This method takes 2 values x and y as arguments. It searches the list for x and adds y to the list immediately before the first time that x appears. If x doesnt appear, the method does nothing

This is the given code

.image text in transcribed

1 2 public class OurLinkedLists T> { private Node head; 4 public class Node { public T value; public Node next; public Node(t value, Node next) { this.value = value; this.next = next; } } 6 7 8 9 10 11 12 13 14 151 16 17 18 19 200 21 22 public void addFront (T newItem) { Node newNode = new Node(newItem, head); head = newNode; } public int size() { Node curr = head; int count = 0; while(curr != null) { count++; curr = curr.next; return count; } public void add(T toAdd) { Node curr = head; if(curr == null) { //special case: adding to empty list addFront(toAdd); return; } 27 28 29 301 31 32 33 34 35 36 37 38 39 40 41 42 43 44e 45 46 47 48 49 50 51 52 53 54 while(curr.next != null) { //advance curr so it points at the last node curr = curr.next; } Node newNode = new Node(toAdd, null); curr.next= newNode; } public I get(int index) { Node curr = head; int count = 0; while(curr != null) { if(count == index) return curr.value; count++; curr = curr.next; } throw new IndexOutOfBoundsException(); }

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

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions

Question

4. How could you face and handle a similar challenge in the future?

Answered: 1 week ago