Question

Consider a method called insertElement(string element, int position) in an array-based list data structure. The purpose...

Consider a method called insertElement(string element, int position) in an array-based list data structure. The purpose of this method is to insert an element (of type string) in the array at the given position within the array. This methods works fine for small array-based lists, but is very inefficient for large lists. Briefly explain why the same function is more efficient when implemented using a singly linked list.

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

void insertElement(String element, int position)

In an array based list structure, the above method to insert an elemetn at a position is very inefficient, because we need to shift all the elements from the position position+1 to right. When the array size is large, no. of shiftings increase linearly and takes a lot of time. It's time complexity for a single insertion is given by O(N)


This same method, when using linked list becomes highly efficinet, because, inorder to insert a new element, we need to add a new element at the position and adjust links which takes constant time O(1) irrespective of the size of the list. When using linked list based lists, inserting and deleting an element becomes trivial.

Add a comment
Know the answer?
Add Answer to:
Consider a method called insertElement(string element, int position) in an array-based list data structure. The purpose...
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
  • A method called linearSearch(), which takes as parameters an array of int followed by three values...

    A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...

  • Implement an application based on a singly linked list that maintains a list of the top...

    Implement an application based on a singly linked list that maintains a list of the top five performers in a video game. An entry on the list consists of a name and a score (you can make this into a blueprint class if you like), and the list must be maintained in descending order of scores. Here is an example of such a list when it only has three elements. ​Spike​120 ​Whiz​105 ​G-man​ 99 Use a class based on singly...

  • LinkedListBox Patterned after the ArrayBox assignment, please create your own implementation of LinkedList called LinkedListBox as...

    LinkedListBox Patterned after the ArrayBox assignment, please create your own implementation of LinkedList called LinkedListBox as explained in class. Refer to the class slides on linked lists. Specifically, implement your generic classes as: public class LinkedListNode { public E element; public LinkedListNode next; } public class LinkedListBox { public LinkedListNode head; public LinkedListNode tail; public int count = 0; } Within the LinkedListBox class, implement the following methods: public boolean add(E e). Returns true after adding an element to the...

  • /* * Purpose: Test an implementation of an array-based list ADT. * To demonstrate the use...

    /* * Purpose: Test an implementation of an array-based list ADT. * To demonstrate the use of "ListArrayBased" the project driver * class will populate the list, then invoke various operations * of "ListArrayBased". */ import java.util.Scanner; public class ListDriver {    /*    * main    *    * An array based list is populated with data that is stored    * in a string array. User input is retrieved from that std in.    * A text based...

  • Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String a...

    Q1)(20p)Write a static member function called removeLongestRepeatingSegment that takes a String argument. The method will return a new String by removing the logest segment that contains consequtively repeating characters. public static void main(String []args){ String s=”1111222223333311111111444455552222”; String res= removeLongestRepeatingSegment(s); will return “11112222233333444455552222” } public static String removeLongestRepeatingSegment(String s){ --------------- Q2)15p)Given a list of objects stored (in ascending order) in a sorted linked list, implement member method which performs search as efficiently as possible for an object based on a key....

  • Static methods can be called directly from the name of the class that contains the method....

    Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5);   System.out.println(var); } } For your final exercise, create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is...

  • Creating linked list data structure Overview The purpose of this assignment is for you to write...

    Creating linked list data structure Overview The purpose of this assignment is for you to write a data structure called a Linked List, which utilizes templates (similar to Java’s generics), in order to store any type of data. In addition, the nature of a Linked List will give you some experience dealing with non-contiguous memory organization. This will also give you more experience using pointers and memory management. Pointers, memory allocation, and understand how data is stored in memory will...

  • package week_3; /** Write a method called countUppercase that takes a String array argument. You can...

    package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....

  • A bounded string list has an upper limit on its size, which is set when the...

    A bounded string list has an upper limit on its size, which is set when the list is created. The size limit specifies the maximum number of strings that can be stored in a bounded string list. String lists are mutable and allow random accesses to their elements using indexes. The list operations include BoundedStringList(int sizeLimit); void insert(String newString, int index); void delete(int index); String get(int index); The constructor creates an empty list with a maximum size of sizeLimit. The...

  • Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static i...

    Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5);   System.out.println(var); } } For your final exercise, create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is...

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