In: Computer Science
public class StackTest { public static void main(String[] args) { StackX theStack = new StackX(10); // make new stack theStack.push(20); // push items onto stack theStack.push(30); theStack.push(40); theStack.push(40); theStack.push(60); theStack.push(80); theStack.showStack(); System.out.println("removeDownTo(40)"); theStack.removeDownTo(40); theStack.showStack(); } // end main() }
public class QueueTest { public static void main(String[] args) { Queue theQueue = new Queue(20); // queue holds 5 items theQueue.insert(10); // insert 4 items theQueue.insert(20); theQueue.insert(30); theQueue.insert(40); theQueue.showQueue(); System.out.println("Removing 3 items"); theQueue.remove(); // remove 3 items theQueue.remove(); // (10, 20, 30) theQueue.remove(); theQueue.showQueue(); System.out.println("Inserting 4 more items"); theQueue.insert(50); // insert 4 more items theQueue.insert(60); // (wraps around) theQueue.insert(70); theQueue.insert(80); theQueue.showQueue(); System.out.println("Calling removeSecond()"); long second = theQueue.removeSecond(); theQueue.showQueue(); } // end main() }
Attached Files:
You may not use the java.util.Stack
class.
You may not use the java.util.Queue class.
Ex1: Write a program that converts decimal to
binary using a stack.
StackX class:
Ex2: Write method showStack(): It displays the
contents of the stack starting with the first inserted element to
the last. The stack would contain the same elements after
showStack(). . If for example if to an empty stackx we push(20)
then push(30) showStack() would print 20 30
Ex3: Write a method public static void
removeDownTo (long n): It pops all values off the stack down to but
not including the first element it sees that is equal to the second
parameter. If none are equal, leave the stack empty.
Attach your StackX.java. I will use StackTest.java
to test the above methods.
Queue class:
Ex4: Write method showQueue() to display the
contents of the queue from first to last. The queue would contain
the same elements after showQueue().
Ex5: Write a method public static Object
removeSecond (): It removes and returns the element just behind the
front element. Precondition: The queue has at least two
elements.
Attach your Queue.java. I will use QueueTest.java
to test the above methods.
Answer :2.,3
package stackOperations;
import java.util.Stack;
public class StackX {
int st_elements[] = new int[10];
int tos;
StackX() {
tos = -1; // indicate it's
empty
}
public void push(int item) // for inserting element
into stack in stack order
{
if (tos == 10) {
System.out.println("stack is full");
return;
} else {
st_elements[++tos] = item;
}
}
public boolean empty_stack() // check that stack is
empty or not
{
if (tos == -1)
return
true;
else
return
false;
}
public int pop() // for retrieving element from
stack
{
if (tos < 0) {
System.out.println("stack underflow");
return 0;
} else
return
st_elements[tos--];
}
public boolean isEmpty() {
return tos == -1;
}
public void showStack() // for printing the stack
contains
{
int temp = 0;
if (!empty_stack()) {
temp =
pop();
System.out.println(temp);
showStack();
}
push(temp);
}
public long peek() {
return st_elements[tos];
}
static void removeDownTo(final StackX stack, final
long downTo) {
while (!stack.isEmpty()) {
if (stack.peek()
== downTo) {
return;
}
}
}
}