Sei sulla pagina 1di 31

1. Program to implement array operations.

#include<iostream>
using namespace std;
#define max 100
class array
{
int a[max],n,p,po;
public:
array()
{
cout<<"\n enter array size\n";
cin>>n;
}
void create();
void display();
void insertion();
void deletion();
void search();
void sorting();

};
void array::create()
{
cout<<"\nenter array elements\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void array::display()
{
cout<<"\n array elements are \n";
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}

void array::insertion()//position must be between 1 and n+1


{
cout<<"\n enter inserted element\n";
cin>>p;
for(int i=n;i>=p;i--)
{
a[i]=a[i-1];
}
cout<<"\n enter element to a given position\n";
cin>>a[p-1];
n++;

}
void array::deletion()//position must be 1 to n
{
cout<<"\nenter position (between 1 and n) to delete\n";
cin>>po;
for(int i=po-1;i<n;i++)
a[i]=a[i+1];
n--;
}
void array::search()
{
int i,key,found=0;
cout<<"\n enter key\n";
cin>>key;
for(i=0;i<n;i++)
{
if(key==a[i])
{
cout<<"\n the number found \t"<<key<<"\t"<<i+1;
found=1;

}
}
if(found==0)
{
cout<<"\n element not found\t"<<key;

}
void array::sorting()
{
int i,j,temp;
cout<<"\n after sorting:";

for(i=n;i>=0;i--)
{
for(j=0;j<i-1;j++)
{
if(a[j]>=a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

}
}
}
}

main()
{
array ar1;
int i;
ar1.create();
ar1.display();
ar1.insertion();
ar1.display();
ar1.deletion();
ar1.display();
ar1.search();
ar1.display();
ar1.sorting();
ar1.display();
}

Output:
2. Program to display sparse representation for a given m*n matrix.
#include<iostream>
#include<cmath>
using namespace std;
main()
{
int s[10][10],i,j,r1,c1,size=0;
cout<<"\n enter rows\n";
cin>>r1;
cout<<"\n enter cols\n";
cin>>c1;
cout<<"\n enter elements";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>s[i][j];
}
}
cout<<"\n enter matrix\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(s[i][j]!=0)
{
size++;
}
}
}
int s1[size][3],k=0;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(s[i][j]!=0)
{
s1[k][0]=j;
s1[k][1]=i;
s1[k][2]=s[i][j];
k++;
}
}
}

cout<<"\nsparse matrix is\n";

for(i=0;i<k;i++)
{
for(j=0;j<3;j++)
{
cout<<s1[i][j]<<"\t";
}
cout<<endl;
}

}
Output:
3. Program to implement stack operations using arrays
#include<iostream>
//#include<stdlib.h>
using namespace std;

class mystack
{
int a[5],size,top;
public:
mystack()
{
top=-1;
size=5;
}

void push(int ele)


{
if(top==size-1)
{
cout<<"Stack is Full and can't be inserted";
return;
}

top=top+1;
a[top]=ele;
}

void pop()
{
if (top==-1)
{
cout<<"Stack is Empty and can't be deleted";
return;
}

cout<<"Deleted element is "<<a[top];


top=top-1;
}

void disp()
{
int i;
for(i=top;i>=0;i--)
cout<<a[i]<<" ";
}
};
int main()
{
mystack sta;
int ch;
while(1)
{
cout<<"\n";
cout<<"Menu\n";
cout<<"1. Push\n";
cout<<"2. Pop\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";
cout<<"Enter your choice ";
cin>>ch;

switch(ch)
{
case 1:
int ele;
cout<<"\n enter the element to insert into stack";
cin>>ele;
sta.push(ele);
break;

case 2:
sta.pop();
break;

case 3:
sta.disp();
break;

case 4:
exit(1);

}
}
Stack implementation without class

#include<iostream>
using namespace std;

#define size 20
int a[size];
int top=-1;
void push(int x)
{
if(top==size-1)
{
cout<<"stack over flow\n:";
}
else
{
top=top+1;
a[top]=x;
}
}
void pop()
{
if(top==-1)
{
cout<<"stack is empty\n";
}
else
{
top=top-1;
}
}
void display()
{
cout<<"stack elements\n";
for(int i=0;i<=top;i++)
{
cout<<a[i]<<endl;
}
}
main()
{
int ch;
while(1)
{
cout<<"\n";
cout<<"Menu\n";
cout<<"1. Push\n";
cout<<"2. Pop\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";
cout<<"Enter your choice ";
cin>>ch;

switch(ch)
{
case 1:{

int ele;
cout<<"\n enter the element to insert into stack";
cin>>ele;
push(ele);
break;
}
case 2:
pop();
break;

case 3:
display();
break;
case 4:
exit(1);

}
}

4. Program to implement queue operations using arrays.


#include<iostream>
using namespace std;
#define max 5
int queue[max];
int front=-1;
int rear=-1;
void enqueue(int x)
{
if(rear==max-1)
{
cout<<"\n queue is full";
}
if(front==-1)
{
front=0;
}
rear=rear+1;
queue[rear]=x;
}
void dequeue()
{
if(front==-1||front==rear+1)
{
cout<<"\nqueue is empty";
}
int x;
x=queue[front];
front=front+1;
cout<<"\ndelete element"<<x;
}
void display()
{
int i;
cout<<"\n the elements are\n";
for(i=front;i<=rear;i++)
{
cout<<queue[i]<<endl;
}
}
main()
{

enqueue(10);
enqueue(50);
enqueue(100);
enqueue(150);
display();
dequeue();
display();
enqueue(150);
display();

Output:

5.Program to convert infix expression into postfix.


#include<iostream>
using namespace std;
#define size 20
class intopost
{
public:
char exp[20],s[size];
int top,i;
intopost()
{
top=-1;
i=0;
}
void readexp();
void push(char);
char pop();
char readsymbol();
int isp(char);
int icp(char);
void convert();
};
void intopost::readexp()
{
cout<<"enter expression";
gets(exp);

}
void intopost::push(char ch)
{
if(top==(size-1))
cout<<"stack is full";
else
s[++top]=ch;
}
char intopost::pop()
{
if(top==-1)
{
cout<<"stack is empty";
}
else
return(s[top--]);
}

char intopost::readsymbol()
{
return(exp[i++]);
}
void intopost::convert()
{
int i=1;
char isop;
char ch=readsymbol();
while(ch!='\0')
{
if((('a'<=ch)&&(ch<='z'))||(('A'<=ch)&&(ch<='Z')))
cout<<ch;
else if(ch=='(')
{
push(ch);
}
else if(ch==')')
{
isop=pop();
while(isop!='(')
{
cout<<isop;
isop=pop();
}
}
else if((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||(ch=='^')||(ch=='%'))
{
isop=pop();
if(isp(isop)<icp(ch))
{
push(isop);
push(ch);
}
else if(isp(isop)>=icp(ch))
{
while(isp(isop)>=icp(ch))
{
cout<<isop;
isop=pop();
}
push(isop);
push(ch);
}
}
ch=readsymbol();
}
if(ch=='\0')
{
isop=pop();
while(isop!='(')
{
cout<<isop;
isop=pop();
}
}
}
int intopost::isp(char ch)
{
switch(ch)
{
case '+':
{
return(1);
break;
}
case '-':
{
return(1);
break;
}
case '*':
{
return(2);
break;
}
case '/':
{
return(2);
break;
}
case '%':
{
return(2);
break;
}
case '^':
{
return(3);
break;
}
case '(':
{
return(0);
break;
}
}
}
int intopost::icp(char ch)
{
switch(ch)
{
case '+':
{
return(1);
break;
}
case '-':
{
return(1);
break;
}
case '*':
{
return(2);
break;
}
case '/':
{
return(2);
break;
}
case '%':
{
return(2);
break;
}
case '^':
{
return(4);
break;
}
case '(':
{
return(5);
break;
}
}
}
main()
{
intopost ip;
ip.push('(');
ip.readexp();
ip.convert();
}
Output:
6. program for infix to prefix
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
#define size 50
class stack
{
char s[size];
int top;
public:stack()
{
top=-1;
}
int isfull()
{
return top==(size-1);
}
int isempty()
{
return top==-1;
}
void push(char);
char pop();
};

class intopre
{
char a[size];
stack s;
int i;
public:intopre()
{
i=0;
}
void getexpression();
char getsymbol();
int icp(char);
int isp(char);
void conversion();
};
void intopre::getexpression()
{
cout<<"enter expression"<<endl;
gets(a);
}
char intopre::getsymbol()
{
return(a[i++]);
}
void stack::push(char ch)
{
if(isfull())
cout<<"stack overflow"<<endl;
else
s[++top]=ch;
}
char stack::pop()
{
if(isempty())
{
cout<<"stack is empty"<<endl;
return ' ';
}
else
return(s[top--]);
}

int intopre::icp(char ch)


{
if(ch==')')
return(6);
else if(ch=='^')
return(5);
else if(ch=='*'||ch=='/')
return(3);
else if(ch=='+'||ch=='-')
return(2);
else
return 0;
}
int intopre::isp(char ch)
{
if(ch==')')
return(1);
else if(ch=='^')
return(4);
else if(ch=='*'||ch=='/')
return(3);
else if(ch=='+'||ch=='-')
return(2);
else
return 0;
}

void intopre::conversion()
{
strrev(a);
char isop,res[15],j=0;
s.push(')');
char ch=getsymbol();
while(ch!='\0')
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
res[j++]=ch;
else if(ch=='^'||ch=='*'||ch=='/'||ch=='+'||ch=='-'||ch==')')
{
isop=s.pop();
if(icp(ch)>=isp(isop))
{
s.push(isop);
s.push(ch);
}
else if(icp(ch)<isp(isop))
{
while(icp(ch)<isp(isop))
{
res[j++]=isop;
isop=s.pop();
}
s.push(isop);
s.push(ch);
}
}
if(ch=='(')
{
isop=s.pop();
while(isop!=')')
{
res[j++]=isop;
isop=s.pop();
}
}
ch=getsymbol();
}
if(ch=='\0')
{
isop=s.pop();
while(isop!=')')
{
res[j++]=isop;
isop=s.pop();
}
}
for(int k=--j;k>=0;k--)
cout<<res[k];
}
int main()
{
intopre p;
p.getexpression();
p.conversion();
getch();
return 0;
}
Output:

7.program for postfix evaluation.


#include<iostream>
#include<stdio.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
#define MAX 30
class posteval
{
float stk[MAX];
char post[MAX];
int top,l,i;
public:
posteval()
{
top=-1;
i=0;
l=0;
}

void push(float);
float pop();
void readexp();
void eval();
void show();
};

void posteval::push(float c)
{
stk[++top]=c;
return;
}//push
float posteval::pop()
{
return(stk[top--]);
}//pop
void posteval::readexp()
{
cout<<"Enter the Postfix Expression delimited by # : "<<endl;
gets(post);
l=strlen(post);
cout<<"\n The length of the given string is: "<<l<<endl;
}//readexp
void posteval::eval()
{
float n1,n2,n3,value;
while(post[i] != '#')
{
if(isalpha(post[i]))
{
cout<<"enter value for "<<post[i]<<endl;
cin>>value;
push(value);
}
else
{
n1 = pop( ) ;
n2 = pop( ) ;
char opr=post[i];
switch ( opr )
{
case '+' :
n3 = n2 + n1 ;
break ;
case '-' :
n3 = n2 - n1 ;
break ;
case '/' :
n3 = n2 / n1 ;
break ;
case '*' :
n3 = n2 * n1 ;
break ;
case '^' :
n3 = pow(n2,n1);
break;
default :
cout << "Unknown operator" ;
exit(1) ;
}
push(n3) ;
}
i++;
}
}//eval
void posteval::show()
{
float result= pop();
cout<<"Result is= "<<result;
}
main()
{
posteval obj;
obj.readexp();
obj.eval();
obj.show();
}//main
Output:

9.program for single linked list.


#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;
class linkedlist
{
struct node
{
int data;
node *next;
};
public:
node *first;
linkedlist()
{
first=NULL;
}
void create();
void insnode();
void delnode();
void display();

};
void linkedlist::create()
{
node *last,*newnode;
char ch;
cout<<"\n do you want continue\n";
cin>>ch;
while(ch=='y')
{
newnode=new node();
cout<<"\n enter data in the node\n";
cin>>newnode->data;
if(first==NULL)
{
first=newnode;
last=newnode;

}
else
{
last->next=newnode;
last=newnode;
}
cout<<"\n do you want continue\n";
cin>>ch;
}
last->next=NULL;
}
void linkedlist::display()
{
node *trav;
trav=first;
if(trav==NULL)
{
cout<<"\nlinkedlist is empty\n";
}
while(trav!=NULL)
{
cout<<trav->data<<"\t";
trav=trav->next;
}
}

void linkedlist::insnode()
{
node *temp,*newnode;
int no,pos;
cout<<"\n enter the number and position to be inserted\n";
cin>>no>>pos;
newnode=new node();
newnode->data=no;
newnode->next=NULL;
if(pos==1)
{
if(first==NULL)
first=newnode;
else
{
newnode->next=first;
first=newnode;

}
else
{
int i;
temp=first;
for(i=2;i<pos;i++)
{
if(temp!=NULL)
temp=temp->next;
else
break;
}
if(temp==NULL)
{
cout<<"\n insertion not possible pos outof scope\n";

}
newnode->next=temp->next;
temp->next=newnode;
}

}
void linkedlist::delnode()
{
node *temp1,*temp2;
int ele;
cout<<"enter the element to be deleted\n";
cin>>ele;
temp2=first;
while(temp2!=first)
{
if(temp2->data==ele)
{
if(first->next==NULL)
first=temp2->next;
else
temp1->next=temp2->next;
delete temp2;

}
else

{
temp1=temp2;
temp2=temp2->next;
}

}
}

int main()
{
linkedlist l1;
l1.create();
l1.display();
l1.insnode();
l1.display();
l1.delnode();
l1.display();
}

Output:

10. program for linked stacks


#include<iostream>
using namespace std;
class stack
{
public:
struct node
{
int data;
node *link;
};
node *top;
public :stack()
{
top=NULL;
}
void push(int item);
public: void pop();
void display();
};
void stack::push(int item)
{
node *temp;
temp=new node();
if(top==NULL)

cout<<"empty";
}
temp->data=item;
temp->link=top;
top=temp;
}
void stack::pop()
{
if(top==NULL)
{
cout<<"\n stack is empty";

}
node *del;
int item;
del=top;
item=del->data;
top=del->link;
delete del;
cout<<"deleted element\n";
cout<<item;
}
void stack::display()
{
node *p;
if(top==NULL)
{
cout<<"\n stack is empty";
}
else
{
p=top;
while(p!=NULL)
{
cout<<"\n"<<p->data<<endl;
p=p->link;
}
}
}
main()
{

stack s;
s.push(11);
s.push(22);
s.push(33);
s.push(44);
s.push(55);
s.push(66);
cout<<"\n elements are";
s.display();
s.pop();

cout<<"\n elements are";


s.display();
s.pop();

cout<<"\n elements are";


s.display();

}
Output:
11.program for QUEUE using linkedlist
#include<iostream>
using namespace std;
#include<stdlib.h>

class node
{
public:
int data;
node *link;
};

class ls
{
node *f,*r;
public:
ls()
{
f=NULL;
r=NULL;
}

void ins(int k)
{
node *t;
t=new node;
t->data=k;
t->link=NULL;

if(r==NULL)
{
r=t;
f=t;

}
else
{
r->link=t;
r=t;
}
}
void del()
{
node *temp;
if(f==NULL)
{
cout<<" DEletioln cant be possible";
return;

}
else
{
temp=f;
cout<<"The Deleted elemnt is "<<temp->data;
f=f->link;

if(f==NULL)
r=NULL;

delete temp;
}
}
void display()
{
node *temp;
temp=f;
if(temp==NULL)
cout<<" ";
else
{
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->link;
}
}
}

};
int main()
{
ls l;
while(1)
{
cout<<"\nMenu\n";
cout<<"1. Insertion\n";
cout<<"2. Deletion\n";
cout<<"3. Display\n";
cout<<"4.Exit\n";
cout<<"enter your choice";
int f;
cin>>f;
switch(f)
{
case 1:
int k;
cout<<"enter element to psuh into linked stack";
cin>>k;
l.ins(k);
break;

case 2:
l.del();
break;

case 3:
l.display();
break;

case 4:
exit(1);
}
}
}
Output:

Potrebbero piacerti anche