Data Structures & Algorithms Lab Lab Journal 2
Data Structures & Algorithms Lab Lab Journal 2
Lab Journal 2
Show the contents of stack (at each step) once the following sequence of
statements is executed.
Stack S;
1. S.push('A'); A
2. S.push('B'); A,B
3. S.push('C'); A,B,C
4. S. Pop(); A,B
5. S. Pop(); A
6. S.push('D' ); A,D
7. S.push('E'); A,D,E
8. S. Pop(); A,D
9. S. Pop(); A
• Evaluate the given Postfix expression and trace the contents of the Stack at each step using
the standard evaluation algorithm. 2 7 3 - / 2 1 5 + * +
RESULT:12.5
• Convert the following expression from infix to postfix and show the contents of Stack and
the output expression at each step.
(A+B) * C – D+F*G
SYMOBL STACK CONTENTS OUTPUT EXPRESSION
( (
A ( A
+ (+ A
B (+ AB
) (+) AB+
* * AB+
C * AB+C
- *- AB+C*
D - AB+C*D
+ -+ AB+C*D-
F + AB+C*D-F
* +* AB+C*D-F
G +* AB+C*D-FG*+
Use the above stack class given in lab manual to write a function to reverse a string.
For Example:
CODE:
#include <iostream>
#include<conio.h>
using namespace std;
class stack
{
char *stack_array;
int stackSize=20;
int top;
public:
bool IsFull();
bool IsEmpty();
stack(int size)
{
stack_array = new char[size];
top = -1;
}
void push(char x)
{
if (IsFull())
{
cout << "stack over flow";
exit(0);
}
stack_array[++top] = x;
}
int pop()
{
if (IsEmpty())
{
cout << "stack under flow";
return 0;
}
return stack_array[top--];
}
void display()
{
if (IsEmpty())
{
cout << " stack empty";
exit(0);
}
for (int i = top; i >= 0; i--)
cout << stack_array[i] << " ";
}
};
bool stack::IsEmpty()
{
if (top == -1)
return true;
else
return false;
}
bool stack::IsFull()
{
if (top == stackSize - 1)
return true;
else
return false;
}
int main()
{
stack st(20);
char str[20], ch;
int l, i;
cout << "Enter string::" << endl;
cin >> str;
l = strlen(str);
for (i = 0; i < l; i++)
{
st.push(str[i]);
}
cout << "Reversed string is::" << endl;
for (i = 0; i < l; i++)
{
ch = st.pop();
cout << ch;
}
cout << endl;
_getch();
return 0;
}
OUTPUT:
Check given expression is balance or not i.e. [()] , ()[]() are balanced expression [()) [[ are not
balanced expressions.
CODE:
#include <iostream>
#include<conio.h>
#include <stack>
#include <string>
using namespace std;
bool is_balanced(string expression) {
stack<char> s;
for (char c : expression) {
if (c == '(' || c == '[' || c == '{') {
s.push(c);
}
else if (c == ')' || c == ']' || c == '}') {
if (s.empty()) {
return false;
}
else if (c == ')' && s.top() != '(') {
return false;
}
else if (c == ']' && s.top() != '[') {
return false;
}
else if (c == '}' && s.top() != '{') {
return false;
}
else {
s.pop();
}
}
}
return s.empty();
}
int main() {
string expression;
cout << "Enter an expression: ";
getline(cin, expression);
if (is_balanced(expression)) {
cout << "The expression is balanced." << endl;
}
else {
cout << "The expression is not balanced." << endl;
}
_getch();
return 0;
}
OUTPUT:
FOR BALANCED EXPRESSION::
1. Write a program that uses a stack to convert a binary number into an equivalent decimal
number.
CODE:
#include<iostream>
#include<conio.h>
using namespace std;
class stackk
{
private:
int size = 1;
int* stack = new int(size);
int top;
public:
void setsize()
{
int x;
cout << "\nSet size of array : ";
cin >> x;
size = x;
}
void binary_decimal()
{
int sum = 0;
for (int i = 0; i < size; i++)
{
cout << "Enter binary value : ";
cin >> stack[i];
}
for (int i = 0; i < size; i++)
{
sum += stack[i] * powf(2, size - 1 - i);
}
cout << "\nDecimal : " << sum;
delete[] stack;
}
stackk()
{
top = -1;
}
int isempty()
{
if (top == -1)
{
return true;
}
else
{
return false;
}
}
int isfull()
{
if (top == 100 - 1)
{
return true;
}
else
{
return false;
}
}
void push(int x)
{
if (isfull())
{
cout << "stack over flow";
return;
}
else
stack[++top] = x;
cout << "\npushed : " << x;
}
int reverse()
{
int tempstack[100];
for (int i = 0; i <= top; i++)
{
tempstack[i] = stack[i];
}
cout << "\nReverse";
for (int i = top; i >= 0; i--)
{
cout << " \n " << tempstack[i];
}
return 0;
}
void pop()
{
if (isempty())
{
cout << "\nstack under flow";
return;
}
cout << "\npop element " << stack[top];
stack[top--];
}
void display()
{
if (isempty())
{
cout << "\nstack empty ";
return;
}
for (int i = 0; i <= top; i++)
cout << " \n " << stack[i];
}
};
int main()
{
stackk s;
s.setsize();
s.binary_decimal();
s.display();
cout << "\n";
_getch();
return 0;
}
OUTPUT:
1. Infix to Postfix Conversion Implement a program to read a infix expression from a text file,
the input for this problem is a valid infix expression (with or without parenthesis). The operand
should be single letter digits and valid operators are +,
-, * and /. The output is the postfix version of the expression. Write the result
into another file
CODE:
#include <iostream>
#include <fstream>
#include<conio.h>
#include <stack>
#include <string>
using namespace std;
int precedence(char c) {
if (c == '+' || c == '-') {
return 1;
}
else if (c == '*' || c == '/') {
return 2;
}
else {
return 0;
}
}
string infixToPostfix(string infix) {
stack<char> s;
string postfix = "";
for (char c : infix) {
if (isalnum(c)) {
postfix += c;
}
else if (c == '(') {
s.push(c);
}
else if (c == ')') {
while (!s.empty() && s.top() != '(') {
postfix += s.top();
s.pop();
}
if (!s.empty() && s.top() == '(') {
s.pop();
}
}
else {
while (!s.empty() && precedence(c) <= precedence(s.top())) {
postfix += s.top();
s.pop();
}
s.push(c);
}
}
while (!s.empty()) {
postfix += s.top();
s.pop();
}
return postfix;
}
int main() {
ifstream inFile("input.txt");
ofstream outFile("output.txt");
string infix;
getline(inFile, infix);
cout << "The infix expression in text file (input.txt) is:" << endl;
cout << infix;
cout << endl;
string postfix = infixToPostfix(infix);
outFile << postfix << endl;
cout << "The postfix expression in textfile (output.txt) is:" << endl;
cout << postfix;
inFile.close();
outFile.close();
_getch();
return 0;
}
OUTPUT:
Input textfie:
Output textfile:
1. Postfix Evaluation Evaluate a valid postfix expression and display the result.
CODE:
#include<iostream>
#include<conio.h>
#include<stack>
#include<string>
using namespace std;
int helper(string);
int main()
{
cout << "Please input the postfix expression :";
string str;
cin >> str;
int x = helper(str);
if (x == -1)
cout << "Invalid postfix expression\n";
else
{
cout << "The result is: ";
cout << x;
}
_getch();
return 0;
}
int helper(string str)
{ // create an empty stack
stack<int> st;
// traverse the given expression
for (int i = 0; i < str.length(); i++)
{ // if current char is an operand, push it to the stack
if (str[i] >= '0' && str[i] <= '9')
{
st.push(str[i] - '0');
}
// if current char is an operator
else if (st.size()<2)
return -1;
else
{
// pop top two elements from the stack
int x = st.top();
st.pop();
int y = st.top();
st.pop();
// evaluate the expression x op y, and push the
// result back to the stack
if (str[i] == '+')
st.push(y + x);
else if (str[i] == '-')
st.push(y - x);
else if (str[i] == '*')
st.push(y * x);
else if (str[i] == '/')
st.push(y / x);
}
} // At this point, the st is left with only one element
// i.e. expression result
cout << st.top() << endl;
return st.top();
}
OUTPUT: