13. Suppose that a Sequence ADT has the following interface. public interface Se
ID: 3601589 • Letter: 1
Question
13. Suppose that a Sequence ADT has the following interface. public interface Sequence [ public int sizeO; // Return number of elements in sequence. public void addFirst(int e); // Insert e at the front of the sequence. public void addLast(int e); // Insert e at the back of the sequence // Inserts an element e to be at index i public void add (int i, int e) throus IndexoutOfBoundsException; // Returns the element at index i, without removing it. public int get(int i) throws IndexoutofBoundsException; // Removes and returns the element at index i. public int remove(int i) throws IndexoutofBoundsException; Starting with an empty sequence A, below each operation write down what the (cumula- tive) contents of the list would be after performing the operation. (Write the contents of the sequence as a horizontal, comma separated, list of numbers with the index 0 element on the left.) A.add (0, 4) A. add (o, 3) A.addFirst (2) A. addLast (7) A.add(2, 1) A. add (1, 4) A.add(1, 5) A. add (3, 2)Explanation / Answer
We think sequence as being a vector or one-dimensional matrix like
a ={1,2,3,4,5,6,7,8,9,10}
Now performing operations
Let a ={} be empty sequence.
1. add(0,4) : Add 4 at index 0
a ={4} // at index = 0 element 4 is there.
2. add(0,3) : Add 3 at index 0
a = {3,4} // at index =0 element 3 is there
3. addFirst(2): add 3 at head
a ={2,3,4}
4. addLast(7): Add 7 at tail
a ={2,3,4,7}
5. add(2,1): Add 1 at index 2
a= {2,3,1,4,7}
6. add(1,4): Add 4 at index 1
a= {2,4,3,1,4,7}
7. add(1,5): Add 5 at index 1
a={2,5,4,3,1,4,7}
8. add(3,2): Add 2 at index 3
a={2,5,4,2,3,1,4,7}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.