0% found this document useful (0 votes)
53 views7 pages

Stack ADT: Data Structures and Algorithms Lab Journal - Lab 2

1. This lab document introduces students to stacks and their applications. It defines a Stack class with functions like Push(), Pop(), isEmpty() etc. 2. The document contains exercises to implement stacks - like converting a decimal number to binary using a stack, reversing a string using a stack, and validating mathematical expressions with proper parenthesis using a stack. 3. The student implements the Stack class and provides code solutions to the exercises. They are required to get the solutions checked by the instructor.

Uploaded by

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

Stack ADT: Data Structures and Algorithms Lab Journal - Lab 2

1. This lab document introduces students to stacks and their applications. It defines a Stack class with functions like Push(), Pop(), isEmpty() etc. 2. The document contains exercises to implement stacks - like converting a decimal number to binary using a stack, reversing a string using a stack, and validating mathematical expressions with proper parenthesis using a stack. 3. The student implements the Stack class and provides code solutions to the exercises. They are required to get the solutions checked by the instructor.

Uploaded by

Qazi Mujtaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Journal – Lab 2

Data Structures and Algorithms

Lab Journal - Lab 2


Name: MAAZ NAFEES

Enrollment #: 01-235171-074

Class/Section: BSIT (3B)

Objective

This lab is intended to introduce students to Stacks and their applications. The students will implement the
Stack and employ it in solving the given problems.

Stack ADT 

Stack.h file
#define MAX_ITEMS 100
typedef char itemType;

class Stack{

public:
Stack();
int isFull();
int isEmpty() const;
void Push(itemType newItem);
void Pop(itemType& item);
void Display();
private:
int top;
itemType items[MAX_ITEMS];

};

Data Structures and Algorithms Page 1


Lab Journal – Lab 2
Stack.cpp file
#include "stack.h"
#include<stdlib.h>
#include<iostream>

using namespace std;


Stack::Stack()
{
top=-1;
}
int Stack::isEmpty() const
{
return(top== -1);
}

int Stack::isFull()
{
return(top==MAX_ITEMS-1);
}
void Stack::Push(itemType newItem)
{
if(isFull())
{
cout<<"Stack Overflow"<<endl;
exit(1);
}
top++;
items[top]=newItem;
}

void Stack::Pop(itemType &item)


{
if(isEmpty())
{
cout<<"Stack Underflow";
exit(1);
}
item=items[top];
top--;

}
void Stack::Display()
{
if(isEmpty())
{
cout<<"Stack Underflow";
exit(1);
}
while(!isEmpty())
{
cout<<items[top];
top--;

}
}

Data Structures and Algorithms Page 2


Lab Journal – Lab 2

Task 1 :

Give answers to the following.

1. 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’); AB
3. S.Push(‘C’); ABC
4. S.Pop(); AB
5. S.Pop(); A
6. S.Push(‘D’); AD
7. S.Push(‘E’); ADE
8. S.Pop(); AD
9. S.Push(‘F’); ADF
10. S.Push(‘G’); ADFG
11. S.Pop(); ADF
12. S.Pop(); AD
2. Check the validity of parenthesis and show the contents of Stack and the output
expression at each step.
(A+B) *{ [ (C – D)+F*G]}

Symbol Stack Contents

( (

+ (

{ {

[ {[

Data Structures and Algorithms Page 3


Lab Journal – Lab 2

( {[(

- {[(

) {[

+ {[

* {[

] {

Result: VALID EXPRESSION because stack is empty at the end

Task 2 :

Implement the Stack class and employ it to solve the given exercises.

Exercise 1

Write a C++ program that prompts user to enter a number (in decimal). Convert the number
into binary and display the binary number using the Stack.

CODE :

#include<iostream>
#include "stack.h"
#include<stdlib.h>
#include<string>

using namespace std;


void main()
{
Stack s;
int num;
cout << "Enter number in Decimal:";
cin >> num;
while (num > 1)
{
s.Push(num % 2);
num /= 2;
}
s.Push(num);
cout << "Number in Binary form:";

Data Structures and Algorithms Page 4


Lab Journal – Lab 2
s.Display();
cout << "\n";
system("pause");}
Exercise 2

Write a program that reads a string (an array of characters) from a text file. Reverse the string
using the Stack and write the reversed string to another text file.
CODE :

#include<iostream>
#include "stack.h"
#include<stdlib.h>
#include<string>
#include<fstream>

using namespace std;


void main()
{
Stack s;
char str[10],st[10];
ifstream data;
ofstream data2;

data.open("text.txt", ios::in);
for (int i = 0; !data.eof(); i++)
{
data >> str[i];
s.Push(str[i]);
}

data.close();
data2.open("text2.txt", ios::out);
for (int i = 0; !s.isEmpty();i++)
{

s.Pop(st[i]);
data2 << st[i];
}
data2.close();

system("pause");
}

Data Structures and Algorithms Page 5


Lab Journal – Lab 2

Exercise 3

Write a function that reads a Mathematical expression from a text file and verifies the validity of
parenthesis in the expression using a Stack.
CODE :

#include<iostream>
#include "stack.h"
#include<stdlib.h>
#include<string>
#include<fstream>

using namespace std;


bool compare(char, char);
void main()
{
Stack s;
char str[50], a;
cout << "Enter Expression:";
cin >> str;

for (int i = 0; i < strlen(str); i++)


{
if (str[i] == '[' || str[i] == '{' || str[i] == '(')
s.Push(str[i]);
else if (str[i] == ']' || str[i] == '}' || str[i] == ')')
{
if (s.isEmpty()||!compare(s.Top(),str[i]))
{
cout << "Expression is not valid!:" << endl;
system("pause");
exit(1);
}

else
s.Pop(a);
}
}
if (s.isEmpty())
cout << "Expression is valid!" << endl;
else
cout << "Expression is not valid!:" << endl;

system("pause");
}

bool compare(char open, char close)


{
if (open == '('&&close == ')')
return true;
else if (open == '{'&& close == '}')
return true;
else if (open == '['&&close == ']')
return true;
Data Structures and Algorithms Page 6
Lab Journal – Lab 2
else
return false;}
Implement the given exercises and get them checked by your instructor. If you are unable to complete the
tasks in the lab session, deposit this journal alongwith your programs (printed or handwritten) before the
start of the next lab session.

S No. Exercise Checked By:


1. Exercise 1

2. Exercise 2

3. Exercise 3

+++++++++++++++++++++++++

Data Structures and Algorithms Page 7

You might also like