Question

In: Computer Science

Write a member method named countMin.  This method is inside the class SingleLinkedList<E>. So it has the...

Write a member method named countMin.  This method is inside the class SingleLinkedList<E>. So it has the direct access to Node<E> class and data fields head, size. This method counts how many times the smallest data item in this SingleLinkedList<E> appears in the list.  It is assumed that the class passed to E implements the Comparable<E> interface.

public int countMin()

----------------------

Write a method named endWith.  This method is OUTSIDE the class LinkedList<E>. It takes in two parameters: two LinkedList of Integers named list1, list2. It checks if list1 ends with list2, and returns true if yes, false otherwise.  list1 ends with list2 if list2 is identical to the tail of list1. For example, list1 below ends with list2 below.

list1:  100 80 200 250 30 20 35

list2:  30  20  35

public static boolean endWith(LinkedList<Integer> list1, LinkedList<Integer> list2)

----------------

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 the class <E>.

Example 1:

                  stackIn                   Returned Stack

   top                                                               top     

                    30                                     100

                    10                                     50

                    100                                   30

   bottom    50                                     10        bottom

Example 2:

                  stackIn                   Returned Stack

   top                                                                top     

                    15                                     65

                    3                                       8

                    200                                   200

                  65                                     15     

bottom      8                                        3        bottom

The Stack class includes all methods necessary for the stack operations. You can consider Stack is like the ArrayDeque in Java API used as a Stack.

public static Stack<Integer> changeStack(Stack<Integer> stackIn)

Solutions

Expert Solution

Solution

1. Write a member method:

public int countMin() {
int min_val = head.value;
Node<E> curr = head;
int count = 0;
for(int i=0;i<size;i++) {
if(curr.value < min_val) {
min_val = curr.value;
count = 1;
}
else {
count++;
}
curr = curr.next;
}
return count;
}

2 write a method endwith

public static boolean endsWith(LinkedList<Integer> list1,LinkedList<Integer> list2) {
Node<Integer> h1 = list1.head,h2 = list2.head;
int extra = lis11.size - list2.size;
if(extra < 0) {
return false;
}
while(extra--) {
h1 = h1.next;
}
int sz = h2.size;
while(sz--) {
if(h1.value != h2.value) {
return false;
}
h1 = h1.next;
h2 = h2.next;
}
return false;
}

3 write a method name changed stack

public static Stack<Integer> changeStack(Stack<Integer> stackIn) {
int sz = stackIn.size;
  
Stack<Integer> stackOut = new Stack<Integer>(sz);
boolean flag = true;
if(sz%2 == 0)
flag = false;
sz = sz/2;
Stack<Integer> stacktmp = new Stack<Integer>(sz);
for(int i=0;i<sz;i++) {
stactmp.push(stackIn.top());
stackIn.pop();
}
for(int i=0;i<sz;i++) {
stackOut.push(stacktmp.top());
stacktmp.pop();
}
if(flag) {
stackOut.push(stackIn.top());
stackIn.pop();
}
for(int i=0;i<sz;i++) {
stactmp.push(stackIn.top());
stackIn.pop();
}
for(int i=0;i<sz;i++) {
stackOut.push(stacktmp.top());
stacktmp.pop();
}
return stackOut;
}


Related Solutions

Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a...
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call: isPalindrome(q) should...
write JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
Write a complete java program that Define a class named sample containing: A method named division...
Write a complete java program that Define a class named sample containing: A method named division that receives two integers x and y and returns the division of the two numbers. It must handle any possible exceptions. A method named printArray that receives an array of double arr and an integer index and prints the element in the positon index. It must handle any possible exceptions. main method that recievs input from user and repeat if wrong input. Then it...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers,...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers, finds and returns (1) the smallest index of the largest element of the array and (2) the number of times the largest element occurs in the array. The header of the method should be public static int[ ] indexAndCountOfMax (double[ ] A). The method should return an array of length 2, where the value at index 0 is the smallest index of the largest...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list) b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0) c. to output the original array that displays all integers entered d. to output the new array that displays the list with duplicates removed Use this TEST...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the node that contains the val. .
Write a remove(E val) method for CircularlyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for CircularlyLinkedList class This method remove the first occurrence of the node that contains the val.
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT