0% found this document useful (0 votes)
7 views1 page

Stack Applications and Evaluations Guide

The document outlines various stack applications, including operations such as copying a stack, merging two stacks, and splitting a stack based on positive and negative numbers. It also includes tasks for reversing a number series and converting decimal numbers to octal and hexadecimal formats. Additionally, it covers expression evaluation techniques, such as balancing parentheses and evaluating mathematical expressions with given variable values.

Uploaded by

pencent2021
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Stack Applications and Evaluations Guide

The document outlines various stack applications, including operations such as copying a stack, merging two stacks, and splitting a stack based on positive and negative numbers. It also includes tasks for reversing a number series and converting decimal numbers to octal and hexadecimal formats. Additionally, it covers expression evaluation techniques, such as balancing parentheses and evaluating mathematical expressions with given variable values.

Uploaded by

pencent2021
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Stack Applications

1. Imagine we have two empty stacks of integers, s1 and s2. Print the status of Stack at the end (Practice
Question)

pushStack(s1, 3);
pushStack(s1, 5);
pushStack(s1, 7);
pushStack(s1, 9);
pushStack(s1, 11);
pushStack(s1, 13);
loop not emptyStack(s1)
popStack(s1, x)
pushStack(s2, x)
end loop

Simple Stack Applications (Any two – 5 Marks)

1. Copy Stack – copy stack S1 contents into another stack S2 without changing the order

2. Merge stack S1 and stack S2 into another stack S3

3. Split Stack S into stacks S1 and S2 such that S1 contains only positive numbers and S2 contains only
negative numbers

4. Write a program to implement the Algorithm “reverse the number series”. Test your program with the
number series 1, 3, 5, 7, 9, 2, 4, 6, 8

5. Write a program that converts a decimal number into octal number

6. Write a program that converts a decimal number into hexa-decimal number

Expression Evaluation (any one 5 Marks)

1. Balancing Parenthesis

i. ( ( a+ b ) * c )
ii. (a*b)*c )
iii. A*(B+C))
iv. Reading a C /CPP / Java source file and check its for { } matching

2. Evaluate expression when A =1, B=2, C=3, D=4, E=5, F=6, G=7, Z=7

i. D +B -C
ii. (A * B) + ( C * D )
iii. A B* C* D F* - G+
iv. A Z BC+ *-DE* - F/

Common questions

Powered by AI

A stack can be used to reverse a sequence of numbers by first pushing each number one-by-one onto the stack. Since a stack operates on a Last In, First Out (LIFO) principle, when you subsequently pop the numbers off the stack, they will appear in reverse order. For instance, pushing the sequence 1, 3, 5, 7, 9, 2, 4, 6, 8 onto a stack and then popping all of them will give the sequence 8, 6, 4, 2, 9, 7, 5, 3, 1 .

To split a stack containing both positive and negative integers into two stacks (S1 for positives and S2 for negatives), continuously pop elements from the original stack and evaluate each number. If a number is positive, push it onto S1; if negative, push it onto S2. This ensures that S1 ends up containing only positive numbers and S2 only negative numbers, achieving a clear separation based on integer sign .

To convert a decimal number into an octal number using a stack, repeatedly divide the number by 8, pushing the remainder onto the stack each time. Continue this process until the quotient is zero. Then, pop operands off the stack to get the octal equivalent in correct order. This approach relies on the Last In, First Out (LIFO) characteristic of stacks where the most recent remainder represents the least significant digit, as it should be processed last .

In converting a decimal to a hexadecimal number with a stack, divide the decimal number by 16 repeatedly, each time pushing the remainder onto the stack. Continue until the quotient becomes zero. Hexadecimal digits extend from 0 to 9 and A to F, where A through F represent 10 through 15. Pop all the elements from the stack, which gives the hexadecimal number in correct order, exploiting the LIFO property of stacks to reverse the remainder sequence .

To evaluate 'A Z B C + * - D E * - F /' using a stack, process the expression left to right with values (A=1, Z=7, B=2, C=3, D=4, E=5, F=6). 1) Achieve 'B C +' by popping 2 and 3, getting 5; 2) Push original values and compute '7 (result) * Z' yielding 35, then 'A - (result)' yielding -34; 3) Compute 'D E *' as 20; 4) Apply subtraction '-34 - 20', yielding -54; 5) Divide by F (-54 / 6), yielding -9. Through step-by-step operand stacking and result replacement, this approach reveals the final evaluated outcome [-9].

To merge two stacks, S1 and S2, into a third stack, S3, one must first empty S1 into S3 by popping elements from S1 and pushing them onto S3. Subsequently, do the same with S2. Care should be taken to assess the desired order post-merge, as the operation described here naturally results in S3 containing elements of S1 followed by those of S2, both in reverse order relative to their initial state .

To copy the contents of one stack, S1, to another stack, S2, without changing the order, follow these steps: 1) Prepare an auxiliary stack, S3; 2) Transfer all elements from S1 to S3 (this operation reverses the order); 3) From S3, transfer the elements to S2 (this reverses the order back to the original), effectively copying S1 to S2 while preserving order .

To check if parentheses in an expression are balanced, use a stack to track open parentheses. When encountering an opening bracket '(', push it onto the stack. For each closing bracket ')', check if the stack is empty. If it is, the parentheses are unbalanced because there’s a closing bracket without an opening pair. If the stack is not empty, pop the top of the stack which should be its matching opening bracket. At the end of the expression, if the stack is not empty, this indicates there were unmatched opening brackets. This guarantees that each closing bracket has a matching opening bracket and vice versa .

To evaluate the expression (A * B) + (C * D) using stack, first expand it as 1 * 2 + 3 * 4 based on given values for A, B, C, and D. Follow these steps: 1) Push 1 and 2 onto the stack, pop them, and push the result 2; 2) Push 3 and 4 onto the stack, pop them, and push the result 12; 3) Finally, pop the results 2 and 12 and push back the result of their addition, 14. Thus, the expression evaluates to 14 .

For managing '{ }' matching in source files, traverse the code character-by-character. Push each opening brace '{' found onto the stack. For each closing brace '}', check the stack: if it's empty, or the top is not a matching opening brace, the braces are unbalanced. Otherwise, pop the opening brace. Upon file traversal completion, an empty stack confirms all braces properly matched, else certain '{' lack counterparts. For example, given code `{ int x = y + (z - w); }`, stack operations ensure the internal parentheses and the outer braces match, attesting to code syntactic validity .

You might also like