Limited Offer Get 25% off — use code BESTW25
No AI No Plagiarism On-Time Delivery Free Revisions
Claim Now
Prices start at $5 per page · plagiarism-free papers · free revisions for 14 days 24/7 support: support@bestwriters.com
Uncategorized

Show how to implement a stack using one queue.

  • Posted
  • Authoradmin
  • Length364 words
  • Reading time2 min
Write My Assignment FREE

Problem 1. Add a methodItem[] multiPop(int k) toStack that pops k elements from the stack and returns them as an array of objects.

Note: for problem 1, assume you are adding it this method to one of our in-class stack implementations ArrayStack ,LinkedListStack , orDynamicArrayStack- state which of these classes you are adding your method to.

I want to add the method to ArrayList.

ArrayList.

public class MyArrayStringStack implements MyStringCollection

private String[] array;

private int stackPointer;

public MyArrayStringStack(int size)

this.array = new String[size];

this.stackPointer = 0;

public MyArrayStringStack(MyArrayStringStack mass)

this.array = new String[mass.toArray().length];

String[] temp = mass.toArray();

for (int i = 0; i

this.array[i] = temp[i];

@Override

public void insert(String myString)

if (this.stackPointer

this.array[this.stackPointer++] = myString;

else

System.out.println(“Stack overflow!”);

@Override

public String remove()

if (!this.isEmpty())

return this.array[–this.stackPointer];

else

System.out.println(“Stack empty!”);

return “”;

@Override

public boolean isEmpty()

return (this.stackPointer == 0);

public String[] toArray()

return this.array;

public static void main(String[] args)

MyArrayStringStack mass = new MyArrayStringStack(5);

mass.insert(“Hello”); // 0++ -> 1

mass.insert(“World”); // 1++ -> 2

mass.insert(“This”); // 2++ -> 3

mass.insert(“Something”); // 3++ -> 4

mass.insert(“Else”); // 4++ -> 5

mass.insert(“Blah”);

System.out.println(mass.remove());

System.out.println(mass.remove());

System.out.println(mass.remove());

System.out.println(mass.remove());

System.out.println(mass.remove());

System.out.println(mass.remove());

Problem 2: Stack with one queue. Show how to implement a stack using one queue. Hint: to delete an item, get all of the elements on the queue one at a time, and put them at the end, except for the last one which you should delete and return.

Note:forproblem 2you may assume you are using any queue implementation. You only need to provide the code for the class, you do not need to provide any “testing” code (although I won’t discourage you from testing for correctness).

The post Show how to implement a stack using one queue. appeared first on My Assignment Online.

Plagiarism Free Assignment Help

Expert Help With This Assignment — On Your Terms

✓ Native UK, USA & Australia writers ✓ Deadline from 3 hours ✓ 100% Plagiarism-Free — Turnitin included ✓ Unlimited free revisions ✓ Free to submit — compare quotes

admin

Academic writer and editor at BestWriters.

Get your paper off your plate tonight

Post your instructions for free. Prices start at $5 per page.

Place an order