Question

Problem 1 Enhance the LinkedList class by adding following methods to do some more operations of the linked list: 1. insertAt(pos, data): inserts the element data at position pos of the list. If position exceeds the length of the list throw an exception. Example: List: 4 672 insertAt(2, 8) List: 4 6872 insertAt(0, 9) List: 946872 insertAt(-1,10) IllegalArgumentException List: 94 6872 insertAt(8, 12) IndexOutOfBoundsException List: 94 6872

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below the implementation of insertAt() method to the existing LinkedList class.

Copy the below method to your existing LinkedList class. Make minor modifications as per your class functionality and test.

=======================================================================================

@Override

public void insertAt(int index, Object o) throws IllegalAccessException {

Node node = head;

Node prevNode = null;

//Throw IllegalArgumentException if index <0 or > size

if(index <0 || index > size){

throw new IllegalArgumentException("Insertion index out of range");

}else if (index == 0) { // if index = 0 means insertion at first node.

Node newNode = new Node();

newNode.data = o;

newNode.next = head;//current head node pointing to new node next.

head = newNode;// new node set as head node.

size++;

} else {

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

if (i == index) {

Node newNode = new Node();

newNode.data = o;

newNode.next = prevNode.next;//link new node next to prevnode next

prevNode.next = newNode;//link new node to prev node next

size++;

break;

}

prevNode = node;

node = node.next;

}

}

}

=======================================================================================

Add a comment
Know the answer?
Add Answer to:
Problem 1 Enhance the LinkedList class by adding following methods to do some more operations of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains,...

    In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or...

    RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or a fail. Its very imortant that this and the other questions posted are answered 100% Thank you. 13 - Template C++ Advance Please Only answer assignment if your code is 100%. When pasteing your code use text so I may copy and paste into visual studio. The code and questions must be answered 100% correct and works. Thank you. Programming Assignment Convert the int...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Modify listlink.java program (non generic) by adding the following methods: public void insertsorted(x); // Inert x...

    Modify listlink.java program (non generic) by adding the following methods: public void insertsorted(x); // Inert x in a sorted list. public void deletex(x); //Search for x in the sorted list, if found, delete it from the sorted list. Assume you have a data file p2.txt with the following contents: 8 4 15 23 12 36 5 36 42 3 5 14 4 and your java program is in xxxxx.java file, where xxxxx is the first 5 characters of your last...

  • Add the following methods to the ArrayList class that we wrote during lecture. You may call...

    Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...

  • PART 1 Implement a C++ class for an array representation of a list. The methods must...

    PART 1 Implement a C++ class for an array representation of a list. The methods must be based upon the pseudocode provided on the CS210 Algorithms below. Use the following declarations as a starting point for your implementation. const int MAX_LENGTH = some application-specific max value; typedef <some data type> DataType; class List { public:     methods go here private:     int p;     int length;     DataType a [MAX_LENGTH]; }; Note: Any other variable or constant declarations required would...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT