100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Assignment 2 – Stacks |Johns Hopkins University CS 605.202 -ALL ANSWERS ARE CORRECT $7.99   Add to cart

Exam (elaborations)

Assignment 2 – Stacks |Johns Hopkins University CS 605.202 -ALL ANSWERS ARE CORRECT

 7 views  0 purchase
  • Course
  • Institution

Assignment 2 – Stacks Write pseudo-code not Java for problems requiring code. You are responsible for the appropriate level of detail. 1. a) Use the operations push, pop, peek and empty to construct an operation which sets i to the second element from the top of the stack, leaving the stack u...

[Show more]

Preview 2 out of 8  pages

  • January 29, 2023
  • 8
  • 2022/2023
  • Exam (elaborations)
  • Questions & answers
avatar-seller
Assignment 2 – Stacks

Write pseudo-code not Java for problems requiring code. You are responsible for the
appropriate level of detail.

1. a) Use the operations push, pop, peek and empty to construct an operation which sets i to
the second element from the top of the stack, leaving the stack unchanged.

h = pop (s);
i = pop (s);
push (s, i);
push (s, h);

b) Use the operations push, pop, peek and empty to construct an operation which sets i to
the nth element from the top of the stack, leaving the stack without its top n elements. You are
given integer n.

for (elem = 1; elem < n; elem++)
pop (s);
i = pop (s);

2. Use the operations push, pop, peek and empty to construct an operation which sets i to the
bottom element of the stack, leaving the stack unchanged. (hint: use an auxiliary stack.)
(STACK s1, s2)
while (!empty (s1)) {
h = pop (s1);
push (s2, h);
}
i = h;
while (!empty (s2)) {
h = pop (s2);
push (s1, h);
}

b) Use the operations push, pop, peek and empty to construct an operation which sets i to
the third element from the bottom of the stack. The stack may be left changed.
(STACK s1, s2)
while (!empty (s1)) {
h = pop (s1);
push (s2, h);
}
for (elem = 1; elem <= 3; elem++)
i = pop (s2);
/* i will exit loop with value of 3rd element from bottom
of s1, the original stack */

, 3. Simulate the action of the algorithm for checking delimiters for each of these strings by
using a stack and showing the contents of the stack at each point. Do not write an algorithm.

a) {[A+B]-[(C-D)]




b) ((H) * {([J+K])})




4. Write an algorithm to determine whether an input character string is of the form

xCy

where x is a string consisting only of the letters ‘A’ and ‘B’ and y is the reverse of the x (i.e. if
x=”ABABBA” then y must equal “ABBABA”). At each point you may read only the next
character in the string, i.e. you must process the string on a left to right basis. You may not use
string functions.

while (char != C) {
push (stack, char);
char = nextchar;

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller ExamsConnoisseur. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $7.99. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

77254 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$7.99
  • (0)
  Add to cart