Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

MODULE 4

JOANN E. FAUSTO
BSIT2A

SELF-CHECK
Q-1: Given the following sequence of stack operations, what is
the top item on the stack when the sequence is complete? stack m;
m.push(5); m.push(12); m.pop(); m.push(27); cout << m.top();
A. 5
B. 12
C. 27
D. The stack is empty
Answer: C

Q-2: Given the following sequence of stack operations, what is


the top item on the stack when the sequence is complete? stack m;
m.push(37); m.push(56); m.push(4); while (!m.empty()){ m.pop();
m.pop(); }
A. 37
B. the stack is empty
C. an error will occur
D. 4
Answer: C

SELF-CHECK

Q-1: Without using the activecode infixToPostfix function,


convert the following expression to postfix 10 + 3 * 5 / (16 -
4).

Answer: 10 3 5 * 16 4 - / +

Q-2: What is the result of evaluating the following: 17 10 + 3 *


9 / == ?

Answer: 9

Teaching and Learning Activities

1. What is the output for input "geeksquiz", using above


pseudocode?

 ziuqskeeg

2. Create a C++ program that implements stack data structure to


find all elements in a given unsorted array that are greater than
all elements present to their right


#include <iostream>
#include <stack>
using namespace std;
 
// Function to print all elements which are greater than all
// elements present to their right
void find(int arr[], int n)
{
    // create an empty stack
    stack<int> stk;
 
ASSESSMENT TASK

1. Write a C++ program to implement stack data structure with


push and pop operation.
//push and pop operation
#include <iostream>
#include <stack>
using namespace std;

int main()
{
int j = 14;
// Empty stack
stack<int> joann;
joann.push(8);
joann.push(14);
joann.push(18);
joann.push(3);
joann.push(11);
// stack becomes 8, 14, 18, 3, 11

// Counting number of elements in queue


while (!joann.empty()) {
joann.pop();
j++;
}
cout << j;
2.} Create a C++ program that implements stack data structure to
convert an Infix arithmetic expression to Prefix arithmetic
expression.
//C++ implementation to convert infix expression to postfix
// Note that here we use std::stack
// for Stack operations
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

//Function to return precedence of operators


int prec(char c)
{
if(c == '+')
return 3;
else if(c == '*' || c == '/')
return 2;
3. Create a C++ program that evaluates Prefix arithmetic
expression
// C++ program to evaluate a prefix expression.
#include <bits/stdc++.h>
using namespace std;

bool isOperand(char c)
{
// If the character is a digit then it must
// be an operand
return isdigit(c);
}

double evaluatePrefix(string exprsn)


{
stack<double> Stack;

for (int j = exprsn.size() - 1; j >= 0; j--) {

You might also like