Question: java problems Question 3 Implement the class method: public static void swap(Stack xs , Stack ys) The method exchanges the content of two stacks, xs

java problems

Question 3

Implement the class method: public static void swap(Stack xs , Stack ys)

The method exchanges the content of two stacks, xs and ys.

ThemethodmustworkforanyvalidimplementationoftheinterfaceStack;

You can assume the existence of the classes DynamicArrayStack and LinkedStack.

Stack a, b; a = new LinkedStack(); a . push ( alpha ) ; a . push ( beta ) ; a.push(gamma); b = new DynamicArrayStack(); b . push ( blue ) ; b . push ( green ) ; b.push(yellow ); b.push(black ); System . out . p r i n t l n ( a ) ; System.out. println(b);

swap(a, b); System . out . p r i n t l n ( a ) ; System.out. println(b);

In particular, the above statements should print the following.

[gamma, beta , alpha ] [ black , yellow , green , blue ]

[ black , yellow , green , blue ]

[gamma, beta , alpha ]

Write the code for this method with the following signature: public static void swap(Stack xs , Stack ys) {}

Question 4

Complete the implementation of the instance methods size() and swap() within the class LinkedStack below.

Themethodsize()returnsthenumberofelementsthatarecurrentlystored into this stack

The method swap exchanges the first two elements (not the values); the first element becomes the second and the second element becomes the first. The method returns false if there are less than 2 elements in the list. You cannot use the methods push and pop, instead the links of the structure (references) must be transformed.

public class LinkedStack implements Stack {

private class Elem { // Implements the nodes of the list private E info ;

private Elem next;

private Elem(E info , Elem next) { this.info=info; this.next = next;

} }

private Elem top; // Instance variable , designates the top element

public int size () {}

public boolean swap() {} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!