Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This needs to be in eclipse IDE and in Java only. Implement a method transfer in class LinkedStack. This method should transfer all elements of

This needs to be in eclipse IDE and in Java only.
Implement a method transfer in class LinkedStack. This method should transfer all elements of a stack sourceS to another stack targetS so that the element that starts at the top of sourceS is the first one to be inserted in targetS, and the element at the bottom of sourceS ends up at the top of targetS. The operation should result in sourceS being an empty stack. Test this method in the main method of LinkedStack
CODE:
public class LinkedStack implements Stack {
/** The primary storage for elements of the stack */
private SinglyLinkedList list = new SinglyLinkedList<>(); // an empty list
//new SinglyLinkedList<>() cane be replaced by new SinglyLinkedList()
/** Constructs an initially empty stack. */
public LinkedStack(){}// new stack relies on the initially empty list
/**
* Returns the number of elements in the stack.
* @return number of elements in the stack
*/
@Override
public int size(){ return list.size(); }
/**
* Tests whether the stack is empty.
* @return true if the stack is empty, false otherwise
*/
@Override
public boolean isEmpty(){ return list.isEmpty(); }
/**
* Inserts an element at the top of the stack.
* @param element the element to be inserted
*/
@Override
public void push(E element){ list.addFirst(element); }
/**
* Returns, but does not remove, the element at the top of the stack.
* @return top element in the stack (or null if empty)
*/
@Override
public E top(){ return list.first(); }
/**
* Removes and returns the top element from the stack.
* @return element removed (or null if empty)
*/
@Override
public E pop(){ return list.removeFirst(); }
/** Produces a string representation of the contents of the stack.
*(ordered from top to bottom)
*
* This exists for debugging purposes only.
*
* @return textual representation of the stack
*/
public String toString(){
return list.toString();
}
//main method
public static void main(String[] args)
throws CloneNotSupportedException
{
LinkedStack ls Thit= new LinkedStack();
ls.push("Apple");
ls.push("Kiwi");
ls.push("Lime");
System.out.println( ls.toString());
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

ISBN: 1680838482, 978-1680838480

More Books

Students also viewed these Databases questions