23-NTU-CS-1170 (Lab 5)
23-NTU-CS-1170 (Lab 5)
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--){
}
}
return st.top(); //answer
}
int main(){
cout<<"answer is: "<<prefix("-+7*45+20")<<endl;
return 0;
}
OUTPUT:
#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:
OUTPUT:
………………………………...THE
END………………………………..