You are on page 1of 5

From : -

Name: Md Abdullah Al Mahmud Pias


Id : 2012434642
Section : 14
Email : abdullah.pias@northsouth.edu
To :-
Mohammed Shafiul Alam Khan (SAK1)
Faculty Member
Department of Computer Science and
Engineering
North South University.
Answer of Question 1:

Applications of Stack: Last in First out (LIFO)


Expression Handling:

It is used to convert infix to postfix or infix to prefix expression. With the


help of stacks, we can convert some infix expressions to their equivalent
postfix or prefix.

Memory management
The assignment of memory takes place in contiguous memory blocks. We
call this stack memory allocation because the assignment takes place in the
function call stack. The size of the memory to be allocated is known to the
compiler. When a function is called, its variables get memory allocated on
the stack. When the function call is completed, the memory for the variables
is released. All this happens with the help of some predefined routines in the
compiler. The user does not have to worry about memory allocation and
release of stack variables.

Applications of Queue: FIFO (first in first out)


Cpu Scheduling:

There is an algorithm in operating system of CPU scheduling known as


FCFs which works on the application of FiFo (queue).In this a queue is
maintained of the processes according to their arrival time and are processed
according to that .

Cashier line in any store:

The one who gets first in the line request is served first then the one behind
him and so on. If someone wants to get there request served has to stand at
the last of the line and wait till the ones who arrived early than him request
have been served. This is a real life application of Queue.
Answer of Question 2:

The Code Is:


#include<iostream>
#include<stack>
#include<string>
using namespace std;
// Function to check whether two characters are x
// and y of same type.
bool check (char x,char y)
{
if(x == '(' && y == ')')
return true;
else if(x == '{' && y == '}')
return true;
else if(x == '[' && y == ']')
return true;
return false;
}
bool experiment(string s){
stack<char> i;
for (int c = 0; c < s.length(); c++) {
if(s[c] == '[[' || s[c] == '{{' || s[c] == '(('){
i.push(s[c]); /* If s[i] is open parenthesis then
*get top char from the stack and then match it
*to the current char, if they match
* then pop it from Stack else
*return false*/
} else if(s[c] == ']]' || s[c] == '}}' || s[c] == '))'){
if(s.empty() || !(i.top()+1==s[c+1]))
return false;
}else{
i.pop();
}
}
return true;
}
int main()
{
/*Code to test the function experiment*/
string expression;
cout<<"Enter an expression: "; // input expression from
STDIN/Console
cin>>expression;
if(experiment(expression))
cout<<"\n\nThe Expression is Balanced\n";//check and output
for balanced
else
cout<<"\n\nThe Experiment is Not Balanced\n";//check and
output for balanced
}

Out Put:

You might also like