Queues, Lists, and Stacks Assignment:
Write client methods to do the following
Assume that there exists in main a list declared as :
List<Integer> aList = new List<Integer>(50);
Write a client method called bubbleSort which given a list as a parameter uses the bubble sort algorithm to sort the contents of the list.
YOU MAY ASSUME that there exists a method called swap (with the following signature) which swaps the contents of two elements:
// swap
public static void swap(List<Integer> list1, int position1, int position2)
The heading for the method should be:
public static void bubbleSort( List<Integer> list1)
Assume that in main there exists a queue declared as:
Queue<String> aQueue = new Queue<String>(50);
Write a client method called removeAll that given a String as a parameter removes all occurrences of the String from the queue, leaving the other values in the queue.
The heading for the method should be:
public static void removeAll(Queue<String> q1, String word)
Assume that in main there exists 2 queue’s declared as:
Queue<String> aQueue = new Queue<String>(50);
Queue<String> bQueue = new Q<String>(50);
Write a client method called concatenate that concatenates two queues together and creating and returning a new queue.
the heading should
be
public static Queue<String> concat( Queue<String> q1, Queue<String> q2)
Assuming that in main there exists a Stack declared as:
Stack<Integer> aStack = new Stack<Integer>(50);
Write a client method called reverseStack that given a stack as a parameter, reverses the contents of the stack. In this method you can declare additional instances of queues and stacks as needed.
Its heading should be:
public static void reverseStack(Stack<Integer> s1)
Assuming that in main there exists a Stack declared as:
Stack<Integer> aStack = new Stack<Integer>(50);
Write a client method called getLast that given a stack as a parameter, returns the value on the bottom of the stack, leaving the stack unchanged when the method is finished. In this method you can declare additional instances of queues and stacks as needed.
Its heading should be:
public static int getLast(Stack<Integer> s1)
public static void bubbleSort( List<Integer> list1)
{
//loop till the list size
for (int i=0; i<list1.size();
i++)
{
if(list1.get(i)
> list1.get(i+1) )
{
//if current pointer element of list is greater
than next element then call method swap to swap the elements
swap(list1, i, i+1);
}
} // end of for loop
} // end of method
public static void removeAll(Queue<String> q1, String
word)
{
//loop through the queue till size
times
for (int i = 0; i < q1.size();
i++)
{
//check if
string word is present in the string
if(q1.element().equals(word))
{
//remove the element
q1.remove();
}
}
} //end of method
public static Queue<String> concat( Queue<String>
q1, Queue<String> q2)
{
//declare the output Queue
object
Queue<String> q3 = new
LinkedList<String>();
//assign the first Q values
q3=q1;
//append the second Q
q3.addAll(q2);
return q3;
} //end of method
public static void reverseStack(Stack<Integer> s1)
{
//declare output stack
Stack<Integer> s2 = new
Stack<Integer>();
//loop while stack is empty
while (!s1.empty())
{
//add element to the output stack
s2.push(s1.pop());
}
//print the stack
while(!s2.isEmpty())
{
System.out.println(s2.pop());
}
}
public static int getLast(Stack<Integer> s1)
{
//declare output stack
Stack<Integer> s2 = new
Stack<Integer>();
//variable to store the last
element
int lastElement=0;
//loop while stack is empty
while (!s1.empty())
{
//assign the
element, at the end of the loop, this variable will have the last
element
lastElement=s1.peek();
//add element to
the output stack
s2.push(s1.pop());
}
//print the last element
System.out.println("LastElement " +
lastElement);
//assign the stack to the original
that is s1
s1=s2;
//print the stack to show the stack
is intact
while(!s1.isEmpty())
{
System.out.println(s1.pop());
} //end of while loop
return lastElement;
} //end of method
Get Answers For Free
Most questions answered within 1 hours.