Question

Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists...

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)

Homework Answers

Answer #1

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

Know the answer?
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
Similar Questions
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
Write the following method in java: public static void reverse(Queue<Integer> q) which uses a stack to...
Write the following method in java: public static void reverse(Queue<Integer> q) which uses a stack to reverse the order of all the items in q.
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
java 1) Create a mutator method called setPosition that accepts one integer parameter. Update the position...
java 1) Create a mutator method called setPosition that accepts one integer parameter. Update the position variable by adding the position to the parameter variable. 2)debug the code public class food { public static void main(String[] args) { Fruits apple = new Fruits(20); // Write the statement to call the method that will increase the instance variable position by 6. Fruits.setPosition(6); apple.getPosition(); } }
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node next; } public boolean isEmpty() { return (first == null); } public void push(String item) { // Insert a new node at the beginning of the list. Node oldFirst = first; first = new Node(); first.item = item; first.next = oldFirst; } public String pop() { // Remove the first node from the list and return item. String item = first.item; first = first.next;...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand...
Write a method called recursiveDownAndUp() that takes one non-negative integer parameter, recursively starts at one thousand and prints all the integers from one thousand to the parameter (that is, prints 1000, 999, etc. all the way down to the parameter), then recursively starts at the integer parameter and prints all the integers from the parameter up to one thousand (that is, prints the parameter, the parameter + 1, the parameter + 2, etc. all the way up to 1000). Hint:...
Write a method named changeStack that takes in a Stack of Integer objects named stackIn as...
Write a method named changeStack that takes in a Stack of Integer objects named stackIn as a parameter and returns another Stack of Integer objects. The returned Stack contains contents that is the result of switching the top half and the bottom half of the stackIn. But the ordering of integers in each half is NOT changed. In the case of odd-size stackIn, the middle element remains in the same position before and after the switch. This method is OUTSIDE...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ public class Node<T> { private T info; private Node nextLink; public Node(T info) { } public void setInfo(T info) { } public void setNextLink(Node nextNode) { } public T getInfo() { } public Node getNextLink() { } } Run this program ( Homework2Driver.java ) to test. Comment out...