Data Structures Programs Using C++::: 1) Stacks Using Arrays (MSTACK - CPP)
Data Structures Programs Using C++::: 1) Stacks Using Arrays (MSTACK - CPP)
Data Structures Programs Using C++::: 1) Stacks Using Arrays (MSTACK - CPP)
#include<iostream.h>
#include<process.h>
#include<conio.h>
template<class T>
class stack
{
T *s;
int top,size;
public: stack(int);
~stack();
void push();
void pop();
void display();
};
template<class T>
stack<T>::stack(int k)
{
size=k;
s=new T[size];
top=0;
}
template<class T>
stack<T>::~stack()
{
delete s;
}
template<class T>
void stack<T>::push()
{
if(top==size)
cout<<"Stack Overflow!"<<endl;
else
{
T x;
cout<<"Enter the data value: ";
cin>>x;
s[top]=x;
top++;
}
}
template<class T>
void stack<T>::pop()
{
if(top==0)
cout<<endl<<"Stack Underflow";
else
{
top--;
cout<<"\nThe pop element is: "<<s[top];
void main()
{
int m;
clrscr();
cout<<endl<<"Enter the size of array: ";
cin>>m;
stack <int> t1(m);
while(1)
{
int ch;
cout<<endl<<"*****STACK MENU*****"<<endl;
cout<<"1.PUSH"<<endl;
cout<<"2.POP"<<endl;
cout<<"3.DISPLAY"<<endl;
cout<<"4.EXIT"<<endl;
cout<<"Enter Choice:";
cin>>ch;
switch(ch)
{
case 1:t1.push();
break;
case 2:t1.pop();
break;
case 3:t1.display();
break;
case 4:exit(0);
break;
}
}
}
#include<iostream.h>
#include<process.h>
#include<conio.h>
template<class T>
#include<iostream.h>
#include<process.h>
#include<conio.h>
K S V KRISHNA SRIKANTH Page 4
template<class T>
class lqueue
{
typedef struct node
{
int data;
struct node *link;
}s;
s *head,*tail,*p,*k;
public: lqueue();
void insertion();
void deletion();
void display();
};
template<class T>
lqueue<T>::lqueue()
{
head=tail=NULL;
}
template<class T>
void lqueue<T>::insertion()
{
T x;
p=new s;
cout<<"Enter data value:";
cin>>x;
cout<<endl;
p->data=x;
p->link=NULL;
if(head==NULL)
head=tail=p;
else
{
tail->link=p;
tail=p;
}
}
template<class T>
void lqueue<T>::deletion()
{
if(head==NULL)
cout<<"Queue Underflow"<<endl;
else
{
p=head;
head=head->link;
cout<<"The deleted element is "<<p->data;
delete p;
}
cout<<endl;
}
template<class T>
void lqueue<T>::display()
{
#include<iostream.h>
#include<process.h>
#include<conio.h>
template<class T>
class cqueue
{
T *v;
int size,front,rear;
public: cqueue(int);
~cqueue();
void insertion();
void deletion();
void display();
};
template<class T>
#include<iostream.h>
#include<process.h>
#include<conio.h>
template <class T>
class cqueue
{
typedef struct node
{
int data;
struct node *link;
}s;
s *front,*rear,*p,*k;
public: cqueue();
void insertion();
void deletion();
void display();
};
template<class T>
cqueue<T>::cqueue()
{
front=rear=NULL;
}
template<class T>
void cqueue<T>::display()
{
if(front==NULL)
cout<<endl<<"Queue Empty";
else
{
cout<<endl<<"The elements are: ";
for(p=front;p->link!=front;p=p->link)
#include<iostream.h>
#include<conio.h>
#inlcude<process.h>
template<class T>
class dequeue
{
typedef struct nnode
{
int data;
node *left,*right;
}s;
s *front,*rear,*p,*q,*k;
public:
dequeue()
{
front=rear=NULL;
}
void insbegin();
void insend();
void delbegin();
void delend();
template<class T>
void dequeue<T>::display()
{
cout<<"The Dequeue elements are: ";
for(p=front;p!=NULL;p=p->right)
cout<<p->data<<" ";
cout<<endl;
}
template<class T>
void dequeue<T>::insbegin()
{
T x;
q=new s;
cout<<"Enter data value: ";
cin>>x;
q->data=x;
q->left=q->right=NULL;
if(front==NULL)
front=rear=q;
else
{
q->right=front;
front->left=q;
front=q;
}
}
template<class T>
void dequeue<T>::insend()
{
T x;
q=new s;
cout<<"Enter data value: ";
cin>>x;
q->data=x;
q->left=q->right=NULL;
if(front==NULL)
front=rear=q;
else
{
rear->right=q;
q->left=rear;
rear=q;
}
}
template<class T>
void dequeue<T>::delbegin()
{
p=front;
front=front->right;
front->left=NULL;
void main()
{
int ch;
clrscr();
dequeue <int> t1;
while(1)
{
cout<<"**Dequeue Menu**"<<endl;
cout<<"1.Insertion at front"<<endl;
cout<<"2.Insertion at rear"<<endl;
cout<<"3.Deletion at front"<<endl;
cout<<"4.Deletion at rear"<<endl;
cout<<"5.Display"<<endl;
cout<<"6.Exit"<<endl;
cout<<"Enter choice: ";
cin>>ch;
switch(ch)
{
case 1: t1.insbegin();
break;
case 2: t1.insend();
break;
case 3: t1.delbegin();
break;
case 4: t1.delend();
break;
case 5: t1.display();
break;
case 6: exit(0);
break;
default: cout<<"Enter valid Option";
break;
}
}
}
#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<iostream.h>
#include<conio.h>
#include<process.h>
char stack[30],postfix[30],infix[30];
class intopost
{
public:
int top;
public:
intopost()
{
top=-1;
}
int pri(char);
void push(char);
char stacktop();
int isalnum(char);
char pop();
void conintopost(char[],char[]);
};
int intopost::pri(char x)
{
int value;
switch(x)
{
case ')':value=0;
break;
case '+':value=1;
break;
case '-':value=1;
break;
case '*':value=2;
break;
case '/':value=2;
break;
void main()
{
clrscr();
intopost t1;
cout<<"Enter infix expression:";
cin>>infix;
t1.conintopost(infix,postfix);
cout<<endl<<"Postfix Expression is ";
cout<<postfix;
getch();
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
int main()
{
clrscr();
int sp1[10][3],sp2[10][3],sp3[10][3];
int i,j,m,n,p,q,t1,t2,d,s,element;
int sum[10][3],diff[10][3];
cout<<"Enter the number of rows and columns\n";
cin>>m;
cin>>n;
t1=t2=0;
cout<<" Enter the first matrix("<<m<<"*"<<n<<")\n";
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
cin>>element;
if(element!=0)
{
t1=t1+1;
sp1[t1][1]=i;
sp1[t1][2]=j;
sp1[t1][3]=element;
}
}
}
sp1[0][1]=m;
sp1[0][2]=n;
sp1[0][3]=t1;
cout<<"Enter the Second Matrix("<<m<<"*"<<n<<"):\n";
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
{
if(sp1[i][2]=sp1[j][2])
{
sum[s][1]=diff[d][1]=sp1[i][1];
sum[s][2]=diff[d][2]=sp1[j][2];
sum[s][3]=sp1[i][3]+sp2[j][3];
diff[d][3]=sp1[i][3]-sp2[j][3];
i++;
j++;
if(sum[s][3]!=0)
s++;
if(diff[d][3]!=0)
d++;
}
else
{
if(sp1[i][2]<sp2[3][2])
{
sum[s][1]=diff[d][1]=sp1[i][1];
sum[s][2]=diff[d][2]=sp1[i][2];
sum[s][3]=diff[d][3]=sp1[i][3];
i++;
s++;
d++;
}
else
}
sum[0][1]=diff[0][1]=m;
sum[0][2]=diff[0][2]=n;
sum[0][3]=s-1;
diff[0][3]=d-1;
cout<<"the sum of two sparse matrices is:";
for(i=0;i<s;i++)
{
cout<<"\n"<<sum[i][1]<<"\t"<<sum[i][2]<<"\t"<<sum[i][3]<<"\n";
}
cout<<"The difference of the two matrices is :";
for(i=0;i<d;i++)
{
cout<<"\n"<<diff[i][1]<<"\t"<<diff[i][2]<<"\t"<<diff[i][3]<<"\n";
}
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
template<class T>
class bstree
{
typedef struct node
{
int data;
struct node *left,*right;
}s;
s *head,*prev,*p,*c,*k,*q;
int t;
public:bstree();
void creation();
void display();
void insertion();
void deletion();
void preorder(s *p)
{
if(p!=NULL)
{
cout<<p->data<<" ";
preorder(p->left);
preorder(p->right);
}
else
return;
}
void inorder(s *p)
template<class T>
bstree<T>::bstree()
{
head=prev=c=NULL;
}
template<class T>
void bstree<T>::creation()
{
int n;
T x;
cout<<"Enter the n value: ";
cin>>n;
cout<<endl;
for(int i=1;i<=n;i++)
{
p=new s;
cout<<"Enter the data value:";
cin>>x;
p->data=x;
p->left=p->right=NULL;
if(head==NULL)
head=p;
else
c=head;
while(c!=NULL)
{
prev=c;
if(c->data<p->data)
c=c->right;
else
template<class T>
void bstree<T>::insertion()
{
T x;
q=new s;
cout<<endl<<"Enter the data value: ";
cin>>x;
q->data=x;
q->left=q->right=NULL;
c=head;
while(c!=NULL)
{
prev=c;
if(c->data<q->data)
c=c->right;
else
c=c->left;
}
if(prev!=NULL)
{
if(prev->data<q->data)
prev->right=q;
else
prev->left=q;
}
}
template<class T>
void bstree<T>::display()
{
int ch;
cout<<endl<<"The tree elements:\n ";
cout<<"\n---------------------------\n";
cout<<" DISPLAY MENU \n";
cout<<"----------------------------\n";
cout<<" 1. Pre-Order \n";
cout<<" 2. In-Order \n";
cout<<" 3. Post-Order \n";
cout<<"----------------------------\n";
do
{
cout<<endl<<"Enter the display choice: ";
switch(ch)
{
case 1: cout<<endl<<"Preorder Display: ";
preorder(head);
cout<<endl;
break;
case 2: cout<<endl<<"Inorder Display: ";
inorder(head);
cout<<endl;
break;
case 3: cout<<endl<<"Postorder Display: ";
postorder(head);
cout<<endl;
break;
}
}while(ch<=3);
}
template<class T>
void bstree<T>::deletion()
{
if(head==NULL)
cout<<endl<<"Deletion not possible";
else
{
int x;
cout<<endl<<"Enter the delete element: ";
cin>>x;
if(head->data==x)
cout<<endl<<"The root element deletion not possible ";
else
{
c=head;
while(c->data!=x)
{
prev=c;
if(x<c->data)
c=c->left;
else
c=c->right;
}
if(c->left==NULL && c->right==NULL)
{
if(prev->left==c)
prev->left=NULL;
else
prev->right=NULL;
}
else
if(c->left!=NULL && c->right==NULL)
prev->left=c->left;
else
}
}
void main()
{
int ch;
clrscr();
bstree <int> t1;
t1.creation();
while(1)
{
cout<<"\n--------------------- \n";
cout<<" MAIN MENU \n";
cout<<"----------------------\n";
cout<<" 1.Insertion \n";
cout<<" 2.Deletion \n";
cout<<" 3.Display \n";
cout<<"-----------------------\n";
cout<<endl<<"Enter the choice: ";
cin>>ch;
switch(ch)
{
case 1: t1.insertion();break;
case 2: t1.deletion();break;
case 3: t1.display();break;
default:exit(0);
}
}
}
#include<iostream.h>
#include<conio.h>
}
void bstree::search(node **sr,int num,node **par,node **x,int *found)
{
node *q;
q=*sr;
*found=FALSE;
*par=NULL;
while(q!=NULL)
#include<iostream.h>
#include<conio.h>
#define n 100
class poly
{
private:
int a[n],b[n],add[n],mul[n],p,q,at;
public:
void init();
void main()
{
poly ob;
clrscr();
ob.init();
ob.input();
ob.process();
ob.display();
getch();
}
#include<iostream.h>
#include<conio.h>
class hsort
{
private:
int *a,n;
public:
hsort(int x)
{
n=x;
a=new int[n];
}
void getdata()
{
cout<<"\n";
for(int i=0;i<n;i++) {
cout<<"\tEnter "<<i<<" th value: ";
cin>>a[i];
}
}
void heapsort(int l)
{
for(int i=(l-1)/2;i>=0;i--)
formheap(i,l);
for(i=l;i>0;i--)
{
int t=a[0];
a[0]=a[i];
a[i]=t;
formheap(0,i-1);
}
}
void formheap(int i,int n)
{
int j=2*i+1;
while(j<=n)
{
void main()
{
int n;
clrscr();
cout<<"Enter n value:";
cin>>n;
hsort t(n);
t.getdata();
cout<<"\n\nElements before sorting:\n\n\t";
t.display();
t.heapsort(n-1);
cout<<"\n\nElements after sorting are:\n\n\t";
t.display();
}
#include<iostream.h>
#include<conio.h>
class msort
{
int *a,*b,n,i,mid,k;
public:
msort(int size)
{
n=size;
a=new int[n];
b=new int[n];
}
void getdata()
{
cout<<"\n";
for(i=0;i<n;i++) {
cout<<"\tEnter "<<i<<" th value:";
void main()
{
int m;
clrscr();
cout<<"Enter the array size:";
cin>>m;
msort t(m);
t.getdata();
cout<<"\n\nElements before sorting: \n\n\t";
t.display();
t.sort(0,m-1);
cout<<"\n\nElements after sorting: \n\n\t";
t.display();
#include<iostream.h>
#include<conio.h>
class isort
{
private:
int array[100],n,i,j,temp;
public:
void init();
void sort();
};
void isort::init()
{
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers\n";
for(i=0;i<n;i++)
cin>>array[i];
}
void isort::sort()
{
for(i=0;i<n;i++)
{
j=i;
while((j>0)&&(array[j-1]>array[j]))
{
temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;
j--;
}
}
cout<<"\nArray is sorted in ascending order:\n\n";
for(i=0;i<n;i++)
cout<<array[i]<<" ";
}
void main()
{
clrscr();
isort b;
b.init();
b.sort();
getch();
}
#include<iostream.h>
#include<iostream.h>
#include<conio.h>
class bsort
{
private:
int array[100],n,i,j,temp;
public:
void init();
void sort();
};
void bsort::init()
{
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"Enter "<<n<<" numbers\n";
for(i=0;i<n;i++)
cin>>array[i];
}
void bsort::sort()
{
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
if(array[j]>array[j+1])
{
temp=array[j];
void main()
{
clrscr();
bsort b;
b.init();
b.sort();
getch();
}
#include<conio.h>
#include<iostream.h>
#include<process.h>
void main()
{
int array[100],n,i,j,temp;
clrscr();
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers\n\n";
for(i=0;i<n;i++)
cin>>array[i];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
cout<<"\nArray is sorted in ascending order:\n\n";
for(i=0;i<n;i++)
cout<<array[i]<<" ";
getch();
}
#include<iostream.h>
#include<conio.h>
class ssort
{
K S V KRISHNA SRIKANTH Page 33
private:
int array[100],n,i,j,temp;
public:
void init();
void sort();
};
void ssort::init()
{
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers\n";
for(i=0;i<n;i++)
cin>>array[i];
}
void ssort::sort()
{
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
cout<<"\nArray is sorted in ascending order:\n\n";
for(i=0;i<n;i++)
cout<<array[i]<<" ";
}
void main()
{
clrscr();
ssort b;
b.init();
b.sort();
getch();
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
int ar[100],beg,mid,end,i,n,search;
clrscr();
cout<<"Enter the size of array: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers in ascending order: \n";
for(i=0;i<n;i++)
#include<iostream.h>
#include<conio.h>
#include<process.h>
class bsearch
{
private:
int ar[100],beg,mid,end,i,n,s;
public:
void search();
};
void bsearch::search()
{
cout<<"Enter the size of array: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers in ascending order: \n";
for(i=0;i<n;i++)
cin>>ar[i];
beg=0;
end=n-1;
cout<<"\nEnter a number to search: ";
cin>>s;
while(beg<=end)
{
mid=(beg+end)/2;
if(ar[mid]==s)
{
cout<<"\nItem "<<s<<" found at position "<<(mid+1);
getch();
exit(0);
void main()
{
clrscr();
bsearch b;
b.search();
}
#include<iostream.h>
#include<conio.h>
int main()
{
int a[10],i,n,m,c=0;
clrscr();
cout<<"Enter the size of an array: ";
cin>>n;
if(c==0)
cout<<"\nThe number "<<m<< " is not in the list";
else
cout<<"\nThe number "<<m<< " is found";
return 0;
}
lsearch::lsearch()
{
c=0;
}
void lsearch::init()
{
cout<<"Enter the size of an array: ";
cin>>n;
if(c==0)
cout<<"\nThe number "<<m<< " is not in the list";
else
cout<<"\nThe number "<<m<< " is found";
return 0;
}
int main()
{
clrscr();
#include<iostream.h>
#include<process.h>
#include<conio.h>
int main()
{
int i,n;
clrscr();
cout<<"How many numbers you want to sort: ";
cin>>n;
cout<<"\nEnter "<<n<<" numbers:\n";
for (i = 0; i<n; i++)
cin>>numbers[i];
//perform quick sort on array
qsort(numbers,0,n-1);
// Function to sort
void qsort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];