AITB International Conference, 2019
Kathmandu, Nepal
My Youtube Channel
Please Subscribe
Flag of Nepal
Built in OpenGL
World Covid-19 Data Visualization
Choropleth map
Word Cloud in Python
With masked image
Showing posts with label Data Structure & Algorithm. Show all posts
Showing posts with label Data Structure & Algorithm. Show all posts
Saturday, July 29, 2017
Friday, July 14, 2017
Data structure and algorithm
Program to convert an infix to postfix in c++
#include<iostream>
#include<ctype.h>
#define size 50
using namespace std;
char s[size];
int top=-1;
push(char elem)
{
s[++top]=elem;
}
char pop()
{
return (s[top--]);
}
int pr(char elem)
{
switch(elem)
{
case '=':return 0;
case '(':return 1;
case '-':
case '+':return 2;
case '*':
case '/':return 3;
}
}
int main()
{
char infix[50],postfix[50],ch,elem;
int i=0,k=0;
cout<<"read the infix expression:";
cin>>infix;
push('=');
while((ch=infix[i++])!='\0')
{
if(ch=='(') push(ch);
else
if(isalpha(ch)) postfix[k++]=ch;
else
if(ch==')')
{
while(s[top]!='(')
postfix[k++]=pop();
elem=pop();
}
else
{
while(pr(s[top])>=pr(ch))
postfix[k++]=pop();
push(ch);
}
}
while(s[top]!='=')
postfix[k++]=pop();
postfix[k]='\0';
cout<<endl<<"postfix expression:"<<postfix;
return 0;
}
#include<iostream>
#include<ctype.h>
#define size 50
using namespace std;
char s[size];
int top=-1;
push(char elem)
{
s[++top]=elem;
}
char pop()
{
return (s[top--]);
}
int pr(char elem)
{
switch(elem)
{
case '=':return 0;
case '(':return 1;
case '-':
case '+':return 2;
case '*':
case '/':return 3;
}
}
int main()
{
char infix[50],postfix[50],ch,elem;
int i=0,k=0;
cout<<"read the infix expression:";
cin>>infix;
push('=');
while((ch=infix[i++])!='\0')
{
if(ch=='(') push(ch);
else
if(isalpha(ch)) postfix[k++]=ch;
else
if(ch==')')
{
while(s[top]!='(')
postfix[k++]=pop();
elem=pop();
}
else
{
while(pr(s[top])>=pr(ch))
postfix[k++]=pop();
push(ch);
}
}
while(s[top]!='=')
postfix[k++]=pop();
postfix[k]='\0';
cout<<endl<<"postfix expression:"<<postfix;
return 0;
}
Data structure and algorithm
Program for circular queue in c++
#include<iostream>
using namespace std;
const int size=5;
class cqueue
{
private:
int cq[size];
int front,rear,data;
public:
cqueue()
{
front=-1;
rear=-1;
}
void insert(int n)
{
if(front==((rear+1)%size))
{
cout<<"queue is full."<<endl;
}
else if(rear==-1&&front==-1)
{
rear=0;
front=0;
cq[rear]=n;
cout<<"inserted data is:"<<cq[rear]<<endl;
}
else
{
rear=(rear+1)%size;
cq[rear]=n;
cout<<"inserted data is:"<<cq[rear]<<endl;
}
}
void deldata()
{
if(front==(rear+1))
{
cout<<"queue is empty."<<endl;
}
else
{
data=cq[front];
cq[front]=NULL;
cout<<"deleted data:"<<data<<endl;
front=(front+1)%size;
//front=front+1;
}
}
void view()
{
cout<<"f="<<front<<endl;
cout<<"r="<<rear<<endl;
}
};
int main()
{
cqueue c1;
c1.insert(10);
c1.insert(20);
c1.insert(30);
c1.insert(40);
c1.insert(50);
c1.insert(60);
c1.deldata();
c1.deldata();
c1.deldata();
c1.deldata();
c1.view();
c1.insert(60);
c1.insert(70);
c1.insert(80);
c1.insert(90);
c1.insert(80);
c1.insert(90);
c1.view();
return 0;
}
#include<iostream>
using namespace std;
const int size=5;
class cqueue
{
private:
int cq[size];
int front,rear,data;
public:
cqueue()
{
front=-1;
rear=-1;
}
void insert(int n)
{
if(front==((rear+1)%size))
{
cout<<"queue is full."<<endl;
}
else if(rear==-1&&front==-1)
{
rear=0;
front=0;
cq[rear]=n;
cout<<"inserted data is:"<<cq[rear]<<endl;
}
else
{
rear=(rear+1)%size;
cq[rear]=n;
cout<<"inserted data is:"<<cq[rear]<<endl;
}
}
void deldata()
{
if(front==(rear+1))
{
cout<<"queue is empty."<<endl;
}
else
{
data=cq[front];
cq[front]=NULL;
cout<<"deleted data:"<<data<<endl;
front=(front+1)%size;
//front=front+1;
}
}
void view()
{
cout<<"f="<<front<<endl;
cout<<"r="<<rear<<endl;
}
};
int main()
{
cqueue c1;
c1.insert(10);
c1.insert(20);
c1.insert(30);
c1.insert(40);
c1.insert(50);
c1.insert(60);
c1.deldata();
c1.deldata();
c1.deldata();
c1.deldata();
c1.view();
c1.insert(60);
c1.insert(70);
c1.insert(80);
c1.insert(90);
c1.insert(80);
c1.insert(90);
c1.view();
return 0;
}
Data structure and algorithm
Program for linear queue in c++
#include<iostream>
using namespace std;
const int size =5;
class queue
{
private:
int front,rear,n;
int q[size];
public:
queue()
{
front=0;
rear=0;
}
void insert()
{
if(rear==size)
{
cout<<"queue is full."<<endl;
}
else
{
cout<<"enter the data in queue:";
cin>>n;
q[rear]=n;
rear++;
insert();
}
}
void rem()
{
if(rear==front)
{
cout<<"queue is empty."<<endl;
}
else
{
cout<<"the deleted data is:"<<q[front]<<endl;
q[front]==NULL
front++;
rem();
}
}
};
int main()
{
queue q1;
q1.insert();
q1.rem();
return 0;
}
#include<iostream>
using namespace std;
const int size =5;
class queue
{
private:
int front,rear,n;
int q[size];
public:
queue()
{
front=0;
rear=0;
}
void insert()
{
if(rear==size)
{
cout<<"queue is full."<<endl;
}
else
{
cout<<"enter the data in queue:";
cin>>n;
q[rear]=n;
rear++;
insert();
}
}
void rem()
{
if(rear==front)
{
cout<<"queue is empty."<<endl;
}
else
{
cout<<"the deleted data is:"<<q[front]<<endl;
q[front]==NULL
front++;
rem();
}
}
};
int main()
{
queue q1;
q1.insert();
q1.rem();
return 0;
}
Data structure and algorithm
Program for stack in c++
#include<iostream>
using namespace std;
const int size=5;
class stack
{
private:
int top,q[size],data;
public:
stack()
{
top=-1;
}
void push()
{
if(top==size-1)
{
cout<<"stack overflow"<<endl;
}
else
{
cout<<"enter the data into the stack:";
cin>>q[++top];
push();
}
}
void pop()
{
if(top==-1)
{
cout<<"stack underflow"<<endl;
}
else{
data=q[top];
q[top]==NULL;
cout<<"the deleted data is:"<<data<<endl;
top--;
pop();
}
}
void display()
{
cout<<"data in the stack:"<<endl;
for(int i=0;i=4;i++)
{
cout<<q[i]<<endl;
}
}
};
int main()
{
stack s1;
s1.push();
s1.display();
s1.pop();
return 0;
}