C Programming

Program to Accept a 2X2 Matrix in C using DS

/*WAP to accept a 2X2 matrix & display it in proper format*/
Given source code of ‘C’ program to accept a matrix of order 2X2 matrix and display the matrix in proper format. This source code is successfully compiled and executed
void main()
{
int a[3][3],i,j;
clrscr();
printf(“\n \tEnter the terms for matrix”);

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n The Matrix is..\n”);
for(i=0;i<3;i++)
{

for(j=0;j<3;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
}

Posted by Kumar in C Programming, 0 comments

Program to Check Validity of Expression using Stack

/*program to check validity of an expression using stack */
#include<iostream.h>
#include<conio.h>
#define MAX 10
class stack
{
char s[MAX];
int top;
public:
stack()
{
top=-1;
}

void push(char );
char pop();
int isfull();
int isempty();
void display();
void validate(char *str);
};
void stack:: push(char x)
{
if(isfull()==1)
{
cout<<“\n stack full”;
return;
}
else
{
top++;
s[top]=x;
}
}
char stack:: pop()
{
char ele=’\0′;
if(isempty()==1)
{
cout<<“\n stack empty “;
return ele;
}
else
{
ele=s[top];
top–;
}
return ele;
}
void stack::display()
{
int i;
for(i=top;i>=0;i–)
cout<<“\n”<<s[i];
}
int stack:: isempty()
{
int i=0;
if(top==-1)
i=1;
return i;
}
int stack:: isfull()
{
int i=0;
if(top==MAX)
i=1;
return i;
}

void stack:: validate(char *str)
{
char poped;
int flag=0;
while(*str)
{
if(*str=='(‘||*str=='{‘||*str=='[‘)
{
push(*str);
str++;
}
else if (*str==’)’||*str==’}’||*str==’]’)
{
poped=pop();
if(*str==’)’&& poped=='(‘)
{
flag=0;
}
else if(*str==’}’&& poped=='{‘ )
{
flag=0;
}
else if(*str==’]’ && poped=='[‘)
{
flag=0;
}
else
{
flag=1;
break;
}
str++;
}
else
{
str++;
continue;
}
}
if(flag==1)
cout<<“\n Invalid expression “;
else
cout<<“\n valid expression “;
}
void main()
{
clrscr();
char str[20];
stack st;
cout<<“\n enter expresion “;
cin>>str;
st.validate(str);
getch();
}

Posted by Kumar in C Programming, 0 comments

How to Implement Stack using Array

/*implement stack using array */
#include<iostream.h>
#include<conio.h>
#define MAX 10
class stack
{
int s[MAX];
int top;
public:
stack()
{
top=-1;
}

void push(int );
int pop();
int isfull();
int isempty();
void display();
};
void stack:: push(int x)
{
if(isfull()==1)
{
cout<<“\n stack full”;
return;
}
else
{
top++;
s[top]=x;
}
}
int stack:: pop()
{
int ele=0;
if(isempty()==1)
{
cout<<“\n stack empty “;
return ele;
}
else
{
ele=s[top];
top–;
}
return ele;
}
void stack::display()
{
int i;
for(i=0;i<=top;i++)
cout<<“\n”<<s[i];
}
int stack:: isempty()
{
int i=0;
if(top==-1)
i=1;
return i;
}
int stack:: isfull()
{
int i=0;
if(top==MAX)
i=1;
return i;
}

void main()
{
stack s1;
int x;
int ch;
clrscr();
do
{
cout<<“\n\n1.push\n2.pop\n3.display\n4.exit”;
cout<<“\n Enter Choice “;
cin>>ch;
switch(ch)
{
case 1:
cout<<“\n enter element to insert “;
cin>>x;
s1.push(x);
break;
case 2:
cout<<“\n poped element is “<<s1.pop();
break;
case 3:
s1.display();
break;

}
}while(ch!=4);
}

Posted by Kumar in C Programming, 0 comments

Reverse String using Stack

/* Program to reverse string using stack */
#include<iostream.h>
#include<conio.h>
#define MAX 10
class stack
{
char s[MAX];
int top;
public:
stack()
{
top=-1;
}
void push(char );
char pop();
int isfull();
int isempty();
void display();
void reverse(char *str);
};

void stack:: push(char x)
{
if(isfull()==1)
{
cout<<“\n stack full”;
return;
}
else
{
top++;
s[top]=x;
}
}
char stack:: pop()
{
char ele=’\0′;
if(isempty()==1)
{
cout<<“\n stack empty “;
return ele;
}
else
{
ele=s[top];
top–;
}
return ele;
}
void stack::display()
{
int i;
for(i=top;i>=0;i–)
cout<<“\n”<<s[i];
}
int stack:: isempty()
{
int i=0;
if(top==-1)
i=1;
return i;
}
int stack:: isfull()
{
int i=0;
if(top==MAX)
i=1;
return i;
}
void stack:: reverse(char *str)
{
while(*str)
{
push(*str);
str++;
}
display();
int i=0;
while(isempty()!=1)
{
str[i]=pop();
i++;
}
cout<<“\n str= “<<str;
}

void main()
{
clrscr();
char str[10];
stack st;
cout<<“\n enter String “;
cin>>str;
st.reverse(str);
getch();
}

Posted by Kumar in C Programming, 0 comments

Postfix to Infix Conversion using Stack

/* Program to convert post fix expression in to infix expression using stack */
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define MAX 20
class stack
{
char s[MAX];
int top;
public:
stack()
{
top=-1;
}
void push(char );
char pop();
int isfull();
int isempty();
void display();
void convert(char *str);
int priority(char ch);
};

void stack:: push(char x)
{
if(isfull()==1)
{
cout<<“\n stack full”;
return;
}
else
{
top++;
s[top]=x;
}
}
char stack:: pop()
{
char ele=’\0′;
if(isempty()==1)
{
cout<<“\n stack empty “;
return ele;
}
else
{
ele=s[top];
top–;
}
return ele;
}
void stack::display()
{
int i;
for(i=top;i>=0;i–)
cout<<“\n”<<s[i];
}
int stack:: isempty()
{
int i=0;
if(top==-1)
i=1;
return i;
}
int stack:: isfull()
{
int i=0;
if(top==MAX)
i=1;
return i;
}

void stack:: convert(char *str)
{
char exp[20],op1[10],op2[10],op3[10];
int k=0;
char op1,op2;
while(*str!=’\0′)
{
if( isalpha(*str) || isdigit(*str))
{
push(*str);
str++;
}
else
{
=pop();
if(isdigit(poped)||isalpha(poped))
{
exp[k]=poped;
k++;
poped=pop();
}
str++;
}
/* else if(*str==’+’|| *str==’*’|| *str==’+’|| *str==’-‘|| *str==’/’||*str==’%’||*str==’^’)
{
if(top!=-1)
{
poped=pop();
if(poped=='(‘)
{
push(poped);
push(*str);
str++;
continue;
}
if (priority(poped)>=priority(*str))
{
exp[k]=poped;
k++;
poped=pop();
}
push(poped);
push(*str);
}
else
{
push(*str);
}
str++;
}*/
}
exp[k]=’\0′;
cout<<“postfix string is “<<exp;
}

int stack :: priority(char ch)
{
if(ch==’^’||ch==’$’)
return 3;
else if(ch==’*’||ch==’/’|| ch==’%’)
return 2;
else if(ch==’+’ || ch==’-‘)
return 1;
else
return 4;
}
void main()
{
stack st1;
char exp[20];
clrscr();
cout<<“\n enter expression “;
cin>>exp;
st1.convert(exp);
getch();
}

Posted by Kumar in C Programming, 0 comments

Program to Convert Infix to Prefix using Stack

/* Program to convert infix expression in to pre fix expression */
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define MAX 20
class stack
{
char s[MAX];
int top;
public:
stack()
{
top=-1;
}

void push(char );
char pop();
int isfull();
int isempty();
void display();
void convert(char *str);
int priority(char ch);
};
void stack:: push(char x)
{
if(isfull()==1)
{
cout<<“\n stack full”;
return;
}
else
{
top++;
s[top]=x;
}
}
char stack:: pop()
{
char ele=’\0′;
if(isempty()==1)
{
cout<<“stack empty “;
return ele;
}
else
{
ele=s[top];
top–;
}
return ele;
}
void stack::display()
{
int i;
for(i=top;i>=0;i–)
cout<<“\n”<<s[i];
}
int stack:: isempty()
{
int i=0;
if(top==-1)
i=1;
return i;
}
int stack:: isfull()
{
int i=0;
if(top==MAX)
i=1;
return i;
}

void stack:: convert(char *s)
{
cout<<“\n reversed string is “<<s;
char opr;
int t;
char target[20];
t=0;
while(*s!=’\0′)
{
if( *s==’ ‘ || *s==’\t’)
{
s++;
continue;
}
if(isdigit(*s) || isalpha(*s))
{
target[t]=*s;
s++;
t++;
}
if(*s==’)’)
{
push(*s);
s++;
}
if(*s==’*’ || *s==’+’ || *s==’/’ || *s==’%’ || *s==’-‘ || *s==’$’)
{
if(top!=-1)
{
opr=pop();
if(opr==’)’)
{
push(opr);
push(*s);
s++;
continue;
}
else if (priority(opr)>priority(*s))
{
target[t]=opr;
t++;
opr=pop();
}
push(opr);
push(*s);
}
else
{
push(*s);
}
s++;
}
if(*s=='(‘)
{
opr=pop();
while(opr!=’)’)
{
target[t]=opr;
t++;
opr=pop();
}
s++;
}
}
while(top!=-1)
{
opr=pop();
target[t]=opr;
t++;
}
t++;
target[t]=’\0′;
cout<<“\n prefix string “<<strrev(target);
}
int stack :: priority(char ch)
{
if(ch==’^’||ch==’$’)
return 3;
else if(ch==’*’||ch==’/’|| ch==’%’)
return 2;
else if(ch==’+’ || ch==’-‘)
return 1;
else
return 4;
}
void main()
{
stack st1;
char exp[20];
clrscr();
cout<<“\n enter expression “;
cin>>exp;
strrev(exp);
st1.convert(exp);
getch();
}

Posted by Kumar in C Programming, 0 comments

Program to Convert Infix to Postfix Expression using Stack

/* Program to convert infix expression in to post fix expression */
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define MAX 20
class stack
{
char s[MAX];
int top;
public:
stack()
{
top=-1;
}

void push(char );
char pop();
int isfull();
int isempty();
void display();
void convert(char *str);
int priority(char ch);
};
void stack:: push(char x)
{
if(isfull()==1)
{
cout<<“\n stack full”;
return;
}
else
{
top++;
s[top]=x;
}
}
char stack:: pop()
{
char ele=’\0′;
if(isempty()==1)
{
cout<<“\n stack empty “;
return ele;
}
else
{
ele=s[top];
top–;
}
return ele;
}
void stack::display()
{
int i;
for(i=top;i>=0;i–)
cout<<“\n”<<s[i];
}
int stack:: isempty()
{
int i=0;
if(top==-1)
i=1;
return i;
}
int stack:: isfull()
{
int i=0;
if(top==MAX)
i=1;
return i;
}

void stack:: convert(char *str)
{
char exp[20];
int k=0;
char poped;
while(*str!=’\0′)
{
if(*str=='(‘)
{
push(*str);
str++;
}
else if( isalpha(*str) || isdigit(*str))
{
exp[k]=*str;
k++;
str++;
}
else if(*str==’)’)
{
poped=pop();
while(poped!='(‘)
{
exp[k]=poped;
k++;
poped=pop();
}
str++;
}
else if(*str==’+’|| *str==’*’|| *str==’+’|| *str==’-‘|| *str==’/’||*str==’%’||*str==’^’)
{
if(top!=-1)
{
poped=pop();
if(poped=='(‘)
{
push(poped);
push(*str);
str++;
continue;
}
if (priority(poped)>=priority(*str))
{
exp[k]=poped;
k++;
poped=pop();
}
push(poped);
push(*str);
}
else
{
push(*str);
}
str++;
}
}
exp[k]=’\0′;
cout<<“postfix string is “<<exp;
}

int stack :: priority(char ch)
{
if(ch==’^’||ch==’$’)
return 3;
else if(ch==’*’||ch==’/’|| ch==’%’)
return 2;
else if(ch==’+’ || ch==’-‘)
return 1;
else
return 4;
}

void main()
{
stack st1;
char exp[20];
clrscr();
cout<<“\n enter expression “;
cin>>exp;
st1.convert(exp);
getch();
}

Posted by Kumar in C Programming, 0 comments

Program to Implement queue using Array in c++

/* Program to implement queue using array in c++*/
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define MAX 20
class queue
{
int q[MAX];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}

void display();
void insert(int x);
int delete_q();
};
void queue:: display()
{
int i;
for(i=front;i<=rear;i++)
cout<<q[i]<<“\t”;
}
void queue:: insert(int x)
{
if(rear==MAX-1)
{
cout<<“\n queue full “;
return;
}
else
{
if(front==-1)
front=0;
rear++;
q[rear]=x;
}
}
int queue:: delete_q()
{
int ele;
if(front==-1|| front>rear)
{
cout<<“\n queue empty “;
ele=0;
return ele;
}
else
{
ele=q[front];
front++;
return ele;
}
}
void main()
{
int ch;
queue obj;
int ele;
clrscr();
do
{
cout<<“\n1.insert \n2.Delete \n3.Display n4.Exit \n”;
cout<<“\n enter Choice “;
cin>>ch;
switch(ch)
{
case 1:
cout<<“\n enter Data “;
cin>>ele;
obj.insert(ele);
break;
case 2:
ele=obj.delete_q();
cout<<“\n ele= “<<ele;
break;
case 3:
obj.display();
break;

}
}while(ch!=4);
}

Posted by Kumar in C Programming, 0 comments

Program to Implement Input Restricted Dequeue using Array in c++

/* Program to implement input restricted dequeue using array in c++*/
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define MAX 5
class queue
{
int q[MAX];
int left,right;
public:
queue()
{
left=-1;
right=-1;
}

void display();
void insert(int x);
int delete_left();
int delete_right();
};
void queue:: display()
{
int l=left,r=right;
if(l<=r)
{
while(l<=r)
{
cout<<q[l]<<“\t”;
l++;
}
}
else
{
while(l<=MAX-1)
{
cout<<q[l]<<“\t”;
l++;
}
l=0;
while(l<=r)
{
cout<<q[l]<<“\t”;
l++;
}
}
}
void queue:: insert(int x)
{
if((left==0 && right==MAX-1)||(left==right+1))
{
cout<<“\n queue full “;
return;
}
else
{
if(left==-1)
{
left=0;
right=0;
}
else if(right==MAX-1)
right=0;
else
right++;
q[right]=x;
}
}
int queue:: delete_left()
{
int ele;
if(left==-1)
{
cout<<“\n queue empty”;
return 0;
}
ele=q[left];
if(left==right)
{
left=-1;
right=-1;
}
else if(left==MAX-1)
{
left=0;
}
else
left++;

return ele;
}
int queue:: delete_right()
{
int ele;
ele=q[right];
right–;
return ele;
}
void main()
{
int ch;
queue obj;
int ele;
clrscr();
do
{
cout<<“\n1.insert \n2.Delete from left \n3.Dlete from right \n4.Display \n5.Exit \n”;
cout<<“\n enter Choice “;
cin>>ch;
switch(ch)
{
case 1:
cout<<“\n enter Data “;
cin>>ele;
obj.insert(ele);
break;
case 2:
ele=obj.delete_left();
cout<<“\n ele= “<<ele;
break;
case 3:
ele=obj.delete_right();
cout<<“\n ele= “<<ele;
break;
case 4:
obj.display();
break;

}
}while(ch!=5);
}

Posted by Kumar in C Programming, 0 comments

Program to Implement Circular Queue using Array in c++

/*program to implement circular queue using array in c++ */
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#define MAX 5
class queue
{
int q[MAX];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}

void display();
void insert(int x);
int delete_q();
};
void queue:: display()
{
int i;
int fpos,rpos;
fpos=front;
rpos=rear;
if(fpos<=rpos)
{
while(fpos<=rpos)
{
cout<<q[fpos]<<“\t”;
fpos++;
}
}
else
{
while(fpos<=MAX-1)
{
cout<<q[fpos]<<“\t”;
fpos++;
}
fpos=0;
while(fpos<=rpos)
{
cout<<q[fpos]<<“\t”;
fpos++;
}
}
}
void queue:: insert(int x)
{
if((front==0 && rear==MAX-1)||(front>rear))
{
cout<<“\n queue full “;
return;
}
else
{
if(rear==MAX-1)// && front >0)
{
rear=0;
}
else
rear=rear+1;
if(front==-1)
{
front=0;
}
q[rear]=x;
}
}
int queue:: delete_q()
{
int ele;
if(front==-1)
{
cout<<“\n queue empty “;
ele=0;
return ele;
}
ele=q[front];
if(front==rear)
{
front=rear=-1;
}
else
{
if(front==MAX-1)
front=0;
else
front=front+1;
}
return ele;

}
void main()
{
int ch;
queue obj;
int ele;
clrscr();
do
{
cout<<“\n1.insert \n2.Delete \n3.Display n4.Exit \n”;
cout<<“\n enter Choice “;
cin>>ch;
switch(ch)
{
case 1:
cout<<“\n enter Data “;
cin>>ele;
obj.insert(ele);
break;
case 2:
ele=obj.delete_q();
cout<<“\n ele= “<<ele;
break;
case 3:
obj.display();
break;

}
}while(ch!=4);
}

Posted by Kumar in C Programming, 0 comments