implement method. public static void removeNegative( Stack s) {} to remove negative numbers in the stack pub

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

implement method. public static void removeNegative( Stack s) {} to remove negative numbers in the stack pub

Post by answerhappygod »

implement
method. public static <E>void removeNegative(
Stack<Integer> s) {}
to remove negative numbers in the stack
public class Test
{
public static void main(String[]
args) {
LinkedQueue<Character>
q1=new LinkedQueue<>();
Stack<Integer> stackInt
= new LinkedStack<Integer>();
stackInt.push(10);
stackInt.push(20);
stackInt.push(40);
stackInt.push(30);
System.out.println("searchStack: "
+ searchStack(stackInt,400));
System.out.println(postfix("57+"));
//removeEven(stackInt );
for (int i = 0; i
< stackInt.size(); i++) {
System.out.println(i);
}
//System.out.println(stackInt);
}
public static <E>boolean searchStack(Stack<E>
s, E e) {
Stack<E> tmpStack
= new LinkedStack<E>();
boolean found=false;
while (!s.isEmpty()) {
if (s.top().equals(e)) {
found = true;
break;
}
tmpStack.push(s.pop());
}
while (!tmpStack.isEmpty())
s.push(tmpStack.pop());
return found;
}
public static <E>boolean remove(Stack<E>
s, E e) {
Stack<E> tmpStack
= new LinkedStack<E>();
boolean found=false;
while (!s.isEmpty()) {
if (s.top().equals(e)) {
found = true;
s.pop();
break;
}
tmpStack.push(s.pop());
}
while (!tmpStack.isEmpty())
s.push(tmpStack.pop());
return found;
}
public static <E>boolean searchQueue(Queue<E>
q, E e) {
Queue<E> tmp
= new LinkedQueue<E>();
boolean found=false;
while (!q.isEmpty()) {
if (q.first().equals(e)) {
found = true;
break;
}
tmp.enqueue(q.dequeue()); //check
}
while (!tmp.isEmpty())
q.enqueue(tmp.dequeue());
return found;
}
public static <E>void reverseQueue(Queue<E>
q){
Stack<E>
tmpStack= new LinkedStack<E>();
while (!q.isEmpty())
tmpStack.push(q.dequeue());
while (!tmpStack.isEmpty())
q.enqueue(tmpStack.pop());
}
public static <E>void removeEven(Stack<Integer>
s) {
Stack<Integer> tmp
= new LinkedStack<Integer>();
while (!s.isEmpty()) {
if (s.top()%2!=0)
tmp.push(s.pop());
else
s.pop();
}
while (!tmp.isEmpty())
s.push(tmp.pop());
}
public static <E>boolean recursivesearchStack(Stack<E>
s, E e) {
if (!s.isEmpty())
return false;
if (s.top().equals(e))
return true;
E t=s.pop();
boolean found=recursivesearchStack(s,
e);
s.push(t);
return found;
}
public static double postfix(String
s) {
Stack<Double> st
= new LinkedStack<Double>();
for (int i=0;i<s.length();i++)
{
char c = s.charAt(i);
if (Character.isDigit(c)) {
double x=c-'0';
st.push(x);
//st.push((double) c);
}
else {
double val1= st.pop();
double val2= st.pop();
switch(c) {
case '+':st.push(val1+val2);break;
case '-':st.push(val1-val2);break;
case '*':st.push(val1*val2);break;
case '/':st.push(val1/val2);break;
}
}
}
return st.pop();
}
}
public class LinkedStack<E> implements Stack<E>
{
private SinglyLinkedList<E> list
= new SinglyLinkedList<>( ); // an
empty list
public LinkedStack( ) { } // new stack
relies on the initially empty list
public int size(
)
{
return list.size( );
}
public boolean isEmpty(
)
{
return list.isEmpty( );
}
public void push(E
element)
{
list.addFirst(element);
}
public E top( )
{
return list.first( );
}
public E pop( ) {
return list.removeFirst( );
}
}
/*
* A collection of objects that are inserted and removed
according to the last-in
* first-out principle. Although similar in purpose, this
interface differs from
* java.util.Stack.
*
* @author Michael T. Goodrich
* @author Roberto Tamassia
* @author Michael H. Goldwasser
*/
public interface Stack<E>
{
/**
* Returns the number of elements in the stack.
* @return number of elements in the
stack
*/
int size( );
/**
* Tests whether the stack is empty.
* @return true if the stack is empty,
false otherwise
*/
boolean isEmpty( );
/**
* Inserts an element at the top of the stack.
* @param e the element to be
inserted
*/
void push(E e);
/**
* Returns, but does not remove, the element at the top of the
stack.
* @return top element in the stack
(or null if empty)
*/
E top( );
/**
* Removes and returns the top element from the stack.
* @return element removed (or null if
empty)
*/
E pop( );
}
public class SinglyLinkedList<E>
{
private Node<E>
head=null;
private Node<E>
tail=null;
private int size=0;
public int size()
{ return size;}
public boolean isEmpty(){ return size==0;}
public E first()
{
if (isEmpty()) return null;
return head.getElement();
}
public E last()
{
if (isEmpty()) return null;
return tail.getElement();
}
public void addFirst(E
e)
{
head=new Node<>(e,head);
if(size==0)
tail=head;
size++;
}
public void addLast(E
e)
{
Node<E>
newest=new Node<>(e,null);
if(isEmpty())
head=newest;
else
tail.setNext(newest);
tail=newest;
size++;
}
public E removeFirst()
{
if(isEmpty()) return null;
E answer=head.getElement();
head=head.getNext();
size--;
if (size==0)
tail=null;
return answer;
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply