3.1 You are given a list, L, and another list, P, containing integers sorted in
ID: 3884436 • Letter: 3
Question
3.1 You are given a list, L, and another list, P, containing integers sorted in ascending order. The operation printLots(L,P) will print the elements in L that are in positions specified by P. For instance, if P = 1, 3, 4, 6, the elements in positions 1, 3, 4, and 6 in L are printed. Write the procedure printLots(L,P). You may use only the public STL container operations. What is the running time of your procedure?
3.2 Swap two adjacent elements by adjusting only the links (and not the data) using singly linked lists doubly linked lists
3.3 Implement the STL find routine that returns the iterator containing the first occurrence of x in the range that begins at start and extends up to but not including end. If x is not found, end is returned. This is a nonclass (global function) with signature
template iterator find( Iterator start, Iterator end, const Object & x );
3.4 Given two sorted lists, L1 and L2, write a procedure to compute L1 L2 using only the basic list operations.
3.21 Write a program to check for balancing symbols in the following languages:
C++ (/* */, (), [], {} ).
3.32 Efficiently implement a queue class using a singly linked list, with no header or tail nodes.
Explanation / Answer
Hi, It is against chegg policy to ask many questions in a single one, please post the others ones as different questions and i would be happy to solve them, solving the first one here, here is the fully executable code with comments added
/* package whatever; */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.*;
class Ideone {
public static void main(String[] args) {
Collection<Integer> L = new ArrayList<Integer>(Arrays.asList(5,6,7,9,10,15,56,18)); //creating the list
Collection<Integer> P = new ArrayList<Integer>(Arrays.asList(2,4,6));
new Ideone().printLots(L, P);
}
public void printLots(Collection<Integer> L, Collection<Integer> P) {
/*getting iterators */
Iterator<Integer> l = L.iterator();
Iterator<Integer> p = P.iterator();
int i = 0;
while (p.hasNext()) { //iterating through indexes list
int k = p.next();
while (i < k - 1) { // if index less than size-1
if (l.hasNext())
{
l.next(); //moving forward by the number of steps given in P
i++;
}
}
if(l.hasNext()) // exception check
System.out.println(l.next()); //printing the index
i++;
}
}
}
Thumbs up if this was helpful, otherwise let me know in comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.