You are on page 1of 10

Department of Software Engineering

Data Structures & Algorithms Lab


Lab # 3

SUBMITTED TO:
Sir Rehan Ahmed Siddiqui
SUBMITTED BY:
Mahnoor Mustafa
(2020-BSE-051)
CLASS:
BSE III B
Task 1

Give answers to the following. 


1.  Show the contents of stack (at each step) and value of top once the
following sequence of statements is executed. 
Stack S; 

1. S.Push(‘A’);  A
2. S.Push(‘B’);  B,A
3. S.Push(‘C’);  C,B,A
4. S.Pop();  B,A
5. S.Pop();  A
6. S.Push(‘D’);  D,A
7. S.Push(‘E’);  E,D,A
8. S.Pop();  D,A
9. S.Pop(); A
CODE TASK #01:
Implement the Stack class and its basic functions and test all the
functions in main constructor, push(), pop(), Is_empty(), Is_full(),
Display(), Top()

PROGRAM:
#include<iostream>
using namespace std;

const int SIZE = 5;


class STACK
{
private:
int num[SIZE];
int top;
public:
STACK(); //defualt constructor
int push(int);
int pop();
int Is_empty();
int Is_full();
int Top();
void Display();
};
STACK::STACK() {
top = -1;
}

int STACK::Is_empty() {
if (top == -1)
return 1;
else
return 0;
}

int STACK::Is_full() {
if (top == (SIZE - 1))
return 1;
else
return 0;
}

int STACK::push(int n) {
//check stack is full or not
if (Is_full())
{
return 0;
}
++top;
num[top] = n;
return n;
}

int STACK::pop() {
//to store and print which number
//is deleted
int temp;
//check for empty
if (Is_empty())
return 0;
temp = num[top];
--top;
return temp;

}
int STACK::Top()
{
if (top >= 0)
return (num[top]);
else
{
cout << "Stack is empty." << endl;

return num[SIZE - 1];


}
}
void STACK::Display()
{
int i; //for loop
cout << "Elements of stack are: ";
for (i = (top); i >= 0; i--)
cout << num[i] << " ";
cout << endl;
}

int main()
{
STACK s1;
int choice, n, temp;
cout << "1) Push Into Stack" << endl;
cout << "2) Pop From Stack" << endl;
cout << "3) Display Stcak" << endl;
cout << "4) Show Element on TOP" << endl;
cout << "5) Exit." << endl;

do
{
cout << "Enter your choice: " << endl;
cin >> choice;

switch (choice)
{

case 1:
cout << "Enter number to insert: ";
cin >> n;
temp = s1.push(n);
if (temp == 0)
cout << "Stack is full." << endl;
else
cout << temp << " inserted." << endl;
break;

case 2:
temp = s1.pop();
if (temp == 0)
cout << "Stack is empty." << endl;
else
cout << temp << " is removed (popped)." << endl;
break;

case 3:
s1.Display();
break;
case 4:
cout << "The Element on top is: " << s1.Top() << endl;
break;

case 5: cout << "EXIT";


break;

default:
cout << "An Invalid choice." << endl;
}
} while (choice != 5);
return 0;
CODE TASK #02:
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.
PROGRAM:

#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
}*top = NULL, * p = NULL, * np = NULL;
int x;
void push(int n)
{
np = new node;
np->data = n;
np->next = NULL;
if (top == NULL)
{
top = np;
}
else
{
np->next = top;
top = np;
}
}
int pop()
{
if (top == NULL)
{
cout << "underflow\n";
}
else
{
p = top;
top = top->next;
x = p->data;
delete(p);
return(x);
}
}
int main()
{
int n, a;
cout << "Enter Decimal number you want to convert" << endl;
cin >> n;
while (n > 0)
{
a = n % 2;
n = n / 2;
push(a);
}
p = top;
cout << "Converted number: ";
while (true)
{
if (top != NULL)
cout << pop() << "\t";
else
break;
}

CODE TASK #03:


Write a C++ program to check the mathematical expression is valid or
not using the Stack.
PROGRAM:
#include<iostream>
#include<string>
using namespace std;
const int size = 20;

class stack
{
private:
char array [size];
int top;
public:
stack()
{
top = -1;
}
bool empty();
void push(int);
bool full();
void pop();
void display();
};
bool stack::empty()
{
if (top == -1)
return true;
else
return false;
}

void stack::push(int value)


{
if (top < size - 1)
{
++top;
array[top] = value;
array[top];
}
else
cout << "stack is full" << endl;
}

bool stack::full()
{
if (top == size - 1)
return true;
else
return false;
}

void stack::pop()
{
if (top > -1)
{
cout << array[top--];
}
else
cout << "stack is empty" << endl;
}

void stack::display()
{
for (int i = top; i >= 0; i--)
{

cout << array[i] << endl;


}
}
int main()
{
stack s;
string str;
cout << "Enter the expression you want to check: " << endl;
cin >> str;
int x = str.length();
str.at(0);

for (int i = 0; i < x; i++)


{
if (str.at(i) == '(')
{
s.push(str.at(i));
}
if (str.at(i) == ')')
{
if (s.empty())
{
cout << "Invalid expression\n";
}
else

s.pop();
}
}

if (s.empty())
cout << "Valid expression\n";
else
cout << "Invalid expression\n";

system("pause");
return 0;
}

You might also like