0% found this document useful (0 votes)
6 views17 pages

23-NTU-CS-1170 (Lab 5)

Uploaded by

mahaather45
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)
6 views17 pages

23-NTU-CS-1170 (Lab 5)

Uploaded by

mahaather45
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/ 17

National Textile University

Department of Computer Science


Subject:
DSA
Submitted to:
Sir Basit
Submitted by:
Maha Ather

Reg number:
23-NTU-CS-1170
Lab no.:5
Semester:
3
Question 1:
Print stack through arrays:
Code:
#include<iostream>
using namespace std;
const int stacksize=6;
class stack{
private:
int stackarray[stacksize];
int top;
public:
stack(){
top=0;
}
bool isempty(){
return (top==0);
}
bool isfull(){
return (top==6);
}
void push(int value){
if(isfull()){
cout<<"cant insert because stack is full";

}
else{
stackarray[top++]=value;
}
}
void pop(){
if(isempty()){
cout<<"stack is empty"<<endl;
}
else{
top--;
}
}
int stacksize(){
return top;
}
void display(){
if(top==0){
cout<<"empty stack";
return;
}
else{
for(int i=top-1;i>=0;i--){
cout<<"elments of stack
are:"<<stackarray[i]<<endl;

}
}
}
};
int main(){
stack* stk=new stack();
cout<<"original stack: "<<endl;
stk->push(100);
stk->push(200);
stk->push(300);
stk->push(400);
stk->push(500);
stk->display();
cout<<endl;
cout<<"stack after first poping"<<endl;
stk->pop();
stk->display();
cout<<endl;
cout<<"stack after second poping"<<endl;
stk->pop();
stk->display();
cout<<endl;
cout<<"stack after third poping"<<endl;
stk->pop();
stk->display();
cout<<endl;
cout<<"stack after forth poping"<<endl;
stk->pop();
stk->display();
cout<<endl;
cout<<"stack after fifth poping"<<endl;
stk->pop();
stk->display();
cout<<endl;
return 0;
}
Output:

Question 2:
Stack through linked list:
Code:
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
node(){
next=nullptr;
}

};
class stack{
private:
int stacksize;
node* top;
public:
stack(){
stacksize=0;
top=nullptr;
}
bool isempty(){
return top==nullptr;
}
bool isfull(){
node* newnode=new node();
if(newnode){ //check if newnode is created or not
delete newnode;
return false;
}
return true;
}
int push(int data){
node* newnode=new node();
newnode->data=data;
newnode->next=top;
top=newnode;
stacksize++;
return data;
}
int pop(){
if(isempty()){
cout<<"stack is empty"<<endl;
return -1;
}
int data=top->data;
node* temp=top;
top=top->next;
delete temp;
stacksize--;
return data;
}
int size(){
return stacksize;
}
void display(){
node* temp=top;
while(temp!=nullptr){
cout<<"stack: "<<temp->data<<endl;
temp=temp->next;
}
cout<<endl;
}
};
int main(){
stack* skt=new stack();
cout<<"stack after pushing data is: "<<endl;
skt->push(100);
skt->push(200);
skt->push(300);
skt->push(400);
skt->push(500);
skt->display();
cout<<endl;
cout<<"size of stack is: "<<skt->size()<<endl;
cout<<"stack after pop function is: "<<endl;
skt->pop();
skt->display();
return 0;

Output:

Question 3:
Stack applications:
1. Infix to Postfix Conversion.
2. Evaluation of Postfix Expressions.
3. Decimal to Binary Conversion.
4. Checking Balanced Symbols.
1.Infix to Postfix Conversion.
Code:
#include<iostream>
#include<stack>
using namespace std;
int prec(char c){
if(c=='^'){
return 3;
}else if(c=='/' || c=='*'){
return 2;
}else if(c=='+' || c=='-'){
return 1;
}else{
return -1;
}
}
string conversion(string s){
stack<char>st;
string ans;
for(int i=0;i<s.length();i++){
if((s[i]>='a' && s[i]<='z' ) || (s[i]>='A' && s[i]<='Z')){
ans+=s[i];
}
else if(s[i]=='('){
st.push(s[i]);

}else if(s[i]==')'){
while(!st.empty() && st.top()!='('){
ans+=st.top();
st.pop();
}
if(!st.empty()){ //opening bracket ko remove kare ga but not print
st.pop();
}
}else{
while(!st.empty() && prec(st.top())>=prec(s[i])){
ans+=st.top();
st.pop();
}
st.push(s[i]);

}
}
while(!st.empty()){
ans+=st.top();
st.pop();
}
return ans;
}
int main(){
cout<<"conversion"<<endl;
cout<<conversion("(a-b/c)*(a/k-l)");
return 0;
}

OUTPUT:
2. Evaluation of Postfix Expressions.
Code:
#include<iostream>
#include<stack>
#include<math.h>
using namespace std;
int prefix(string s){
int op1;
int op2;
stack<int>st;
for(int i=s.length()-1;i>=0;i--){

if(s[i]>='0' && s[i]<='9'){


st.push(s[i]-'0');
}else{
op1=st.top();
st.pop();
op2=st.top();
st.pop();
}
switch(s[i]){
case '+':
st.push(op1+op2);
break;
case '-':
st.push(op1-op2);
break;
case '/':
st.push(op1/op2);
break;
case '*':
st.push(op1*op2);
break;
case '^':
st.push(pow(op1,op2));
break;

}
}
return st.top(); //answer
}
int main(){
cout<<"answer is: "<<prefix("-+7*45+20")<<endl;

return 0;
}
OUTPUT:

3. Decimal to Binary Conversion.


Code:

#include<iostream>
#include<stack>
#include<string>
using namespace std;
void binarytodecimal(int num){
stack<int>st;
if(num==0){
cout<<"0";
}
while(num>0){
st.push(num % 2);
num=num/2;
}
while(!st.empty()){
cout<<st.top();
st.pop();
}
}
int main(){
int num;
cout<<"enter number: "<<endl;
cin>>num;
cout<<"decimal to binary is: "<<endl;
binarytodecimal(num);
return 0;
}

OUTPUT:

4. Checking Balanced Symbols.


Code:
#include<iostream>
#include<stack>
using namespace std;
bool balancesymbol(string s){
stack<char>st;
bool ans;
ans=true;
for(int i=0;i<s.size();i++){
if(s[i]=='(' || s[i]=='{' || s[i]=='['){
st.push(s[i]);
}else if(s[i]==')'){
if(!st.empty() && st.top()=='('){
st.pop();
}else{
ans=false;
break;
}
}else if(s[i]=='}'){
if(!st.empty() && st.top()=='{'){
st.pop();
}else{
ans=false;
break;
}
}else if(s[i]==']'){
if(!st.empty() && st.top()=='['){
st.pop();
}else{
ans=false;
break;
}
}
}
if(!st.empty()){
ans=false;
}
return ans;
}
int main(){
string st;
cout<<"enter string: "<<endl;
cin>>st;
if(balancesymbol(st)){
cout<<"string is valid"<<endl;
}else{
cout<<"string is not valid"<<endl;
}
return 0;
}

OUTPUT:
………………………………...THE
END………………………………..

You might also like