Professional Documents
Culture Documents
DS Lab 03 (2020-Bse-051)
DS Lab 03 (2020-Bse-051)
SUBMITTED TO:
Sir Rehan Ahmed Siddiqui
SUBMITTED BY:
Mahnoor Mustafa
(2020-BSE-051)
CLASS:
BSE III B
Task 1
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;
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;
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;
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;
}
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;
}
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--)
{
s.pop();
}
}
if (s.empty())
cout << "Valid expression\n";
else
cout << "Invalid expression\n";
system("pause");
return 0;
}