PROGRAM TO COMPARE AN ARRAY OF NUMBERS
#include<iostream>
using namespace std;
int main()
{
//constant number
const int max=10;
int temp;
int number[max];
int i;
float avg;
//this program calculates the sum of array elemens
int sum=0;
cout<<"please enter 10 intergers. n";
for(i=0;i<max;i++)
{
cout<<"Number "<<i+1<<": ";
cin>>number[i];
sum+=number[i];
}
avg=sum/max;
cout<<"The array elements are:"<<"nn";
for(i=0;i<max;i++)
{
cout<<number[i]<<",";
}
cout<<"nn The sum is "<<sum<<"nn";
cout<<"The average is:"<<avg<<"nn";
//descending order sort
for(int k =0;k<max;k++)
{
for(int x=0;x<max;x++)
{
if(number[x]<number[x+1])
{
//swap the two numbers
temp = number[x];
number[x]=number[x+1];
number[x+1]=temp;
}
}
}
cout<<"nn Descending order :"<<"nn";
//output in descending order
for(i=0;i<max;i++)
{
cout<<number[i]<<",";
}
//ascending order sort
for(int j =0;j<max;j++)
{
for(i=0;i<max;i++)
{
if(number[i]>number[i+1])
{
//swap the two numbers
temp = number[i];
number[i]=number[i+1];
number[i+1]=temp;
}
}
}
cout<<"nn Ascending order :"<<"nn";
//output in ascending order
for(i=0;i<max;i++)
{
cout<<number[i]<<",";
}
return 0;
}
PROGRAM2
#include<iostream>
#include<math.h>
main()
{
double a,b,c,s,area, perimeter;
s=(a+b+c)*0.5;
area=sqrt(s*(s-a)*(s-b)*(s-c));
perimeter=a+b+c;
cout <"nter a"
cin>>a;
cout<<"Enter b";
cin>>b;
cout<<"Enter c";
cin>>c;
cout<<"Area is"<<""<<area<<"n";
cout<<"Perimeter is"<<""<<perimeter<<"n";
}
PROGRAM3
/*
* Salary allocating system using a Boolean expression.
*/
#include <iostream.h>
int main()
{
int pay;
int HourlySalary;
int TotalHours;
cout<<:"Enter salary";
cin>>HourlySalary;
cout<<endl;
cout<<"Enter the total hours worked"<<endl;
cin>>TotalHours;
if(TotalHours<=40)
pay=HourlySalary*TotalHours;
else
pay=(40.0*HourlySalary)+(TotalHours-40)*(HourlySalary*1.5);
cout<<endl;
cout<<"Worker’s pay is $"<<pay;
cout<<endl;
return 0;
}
http://programming-
technique.blogspot.com/search/label/Android%20Application%20Development
IMPLEMENTATION OF ADTs
(a) IMPLEMENTATION OF TREE ADT
Example 1
#include <iostream>
#include <cstdlib>
usingnamespace std;
classBinarySearchTree
{
private:
structtree_node
{
tree_node*left;
tree_node*right;
int data;
};
tree_node*root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const{ returnroot==NULL; }
voidprint_inorder();
voidinorder(tree_node*);
voidprint_preorder();
voidpreorder(tree_node*);
voidprint_postorder();
voidpostorder(tree_node*);
voidinsert(int);
voidremove(int);
};
// Smallerelementsgoleft
// largerelementsgoright
voidBinarySearchTree::insert(intd)
{
tree_node*t = newtree_node;
tree_node*parent;
t->data = d;
t->left= NULL;
t->right= NULL;
parent= NULL;
//is thisa newtree?
if(isEmpty()) root=t;
else
{
//Note:ALLinsertionsare asleaf nodes
tree_node*curr;
curr = root;
// Findthe Node'sparent
while(curr)
{
parent= curr;
if(t->data>curr->data) curr = curr->right;
else curr= curr->left;
}
if(t->data< parent->data)
parent->left=t;
else
parent->right= t;
}
}
voidBinarySearchTree::remove(intd)
{
//Locate the element
bool found= false;
if(isEmpty())
{
cout<<" ThisTree is empty!"<<endl;
return;
}
tree_node*curr;
tree_node*parent;
curr = root;
while(curr!=NULL)
{
if(curr->data== d)
{
found= true;
break;
}
else
{
parent= curr;
if(d>curr->data) curr= curr->right;
else curr = curr->left;
}
}
if(!found)
{
cout<<" Data not found!"<<endl;
return;
}
// 3 cases:
// 1. We're removingaleaf node
// 2. We're removinganode witha single child
// 3. we're removinganode with2 children
// Node withsingle child
if((curr->left==NULL && curr->right!= NULL)||(curr->left!= NULL
&& curr->right == NULL))
{
if(curr->left==NULL && curr->right!= NULL)
{
if(parent->left==curr)
{
parent->left=curr->right;
delete curr;
}
else
{
parent->right= curr->right;
delete curr;
}
}
else //leftchildpresent,norightchild
{
if(parent->left==curr)
{
parent->left=curr->left;
delete curr;
}
else
{
parent->right= curr->left;
delete curr;
}
}
return;
}
//We're lookingata leaf node
if( curr->left== NULL && curr->right== NULL)
{
if(parent->left==curr) parent->left=NULL;
else parent->right=NULL;
delete curr;
return;
}
//Node with2 children
// replace node withsmallestvalue inrightsubtree
if (curr->left!= NULL && curr->right != NULL)
{
tree_node*chkr;
chkr = curr->right;
if((chkr->left==NULL) && (chkr->right== NULL))
{
curr = chkr;
delete chkr;
curr->right= NULL;
}
else //rightchildhaschildren
{
//if the node'srightchildhasa leftchild
//Move all the way downlefttolocate smallestelement
if((curr->right)->left!=NULL)
{
tree_node*lcurr;
tree_node*lcurrp;
lcurrp = curr->right;
lcurr = (curr->right)->left;
while(lcurr->left!=NULL)
{
lcurrp = lcurr;
lcurr = lcurr->left;
}
curr->data = lcurr->data;
delete lcurr;
lcurrp->left=NULL;
}
else
{
tree_node*tmp;
tmp= curr->right;
curr->data = tmp->data;
curr->right = tmp->right;
delete tmp;
}
}
return;
}
}
voidBinarySearchTree::print_inorder()
{
inorder(root);
}
voidBinarySearchTree::inorder(tree_node*p)
{
if(p!= NULL)
{
if(p->left) inorder(p->left);
cout<<" "<<p->data<<" ";
if(p->right) inorder(p->right);
}
else return;
}
voidBinarySearchTree::print_preorder()
{
preorder(root);
}
voidBinarySearchTree::preorder(tree_node*p)
{
if(p!= NULL)
{
cout<<" "<<p->data<<" ";
if(p->left) preorder(p->left);
if(p->right) preorder(p->right);
}
else return;
}
voidBinarySearchTree::print_postorder()
{
postorder(root);
}
voidBinarySearchTree::postorder(tree_node*p)
{
if(p!= NULL)
{
if(p->left) postorder(p->left);
if(p->right) postorder(p->right);
cout<<" "<<p->data<<" ";
}
else return;
}
intmain()
{
BinarySearchTree b;
int ch,tmp,tmp1;
while(1)
{
cout<<endl<<endl;
cout<<" BinarySearchTree Operations"<<endl;
cout<<" ----------------------------- "<<endl;
cout<<" 1. Insertion/Creation"<<endl;
cout<<" 2. In-OrderTraversal "<<endl;
cout<<" 3. Pre-OrderTraversal "<<endl;
cout<<" 4. Post-OrderTraversal "<<endl;
cout<<" 5. Removal "<<endl;
cout<<" 6. Exit"<<endl;
cout<<" Enteryour choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<" Enter Numbertobe inserted:";
cin>>tmp;
b.insert(tmp);
break;
case 2 : cout<<endl;
cout<<" In-OrderTraversal "<<endl;
cout<<" -------------------"<<endl;
b.print_inorder();
break;
case 3 : cout<<endl;
cout<<" Pre-OrderTraversal "<<endl;
cout<<" -------------------"<<endl;
b.print_preorder();
break;
case 4 : cout<<endl;
cout<<" Post-OrderTraversal "<<endl;
cout<<" --------------------"<<endl;
b.print_postorder();
break;
case 5 : cout<<" Enter data to be deleted:";
cin>>tmp1;
b.remove(tmp1);
break;
case 6 : system("pause");
return 0;
break;
}
}
}
}
Example 2
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct btreenode
{
struct btreenode *leftchild;
intdata ;
struct btreenode *rightchild;
} ;
voidinsert( struct btreenode **,int) ;
voidinorder( struct btreenode *) ;
voidpreorder( struct btreenode *) ;
voidpostorder( struct btreenode *) ;
voidmain( )
{
struct btreenode *bt;
intreq,i = 1, num ;
bt = NULL ; /* emptytree */
clrscr( ) ;
printf ( "Specifythe numberof itemstobe inserted:") ;
scanf ( "%d",&req ) ;
while ( i++ <= req)
{
printf ( "Enter the data: " ) ;
scanf ( "%d",&num ) ;
insert( &bt, num) ;
}
printf ( "nIn-orderTraversal:") ;
inorder( bt ) ;
printf ( "nPre-orderTraversal:") ;
preorder( bt ) ;
printf ( "nPost-orderTraversal:") ;
postorder( bt ) ;
}
/* insertsa newnode ina binarysearchtree */
voidinsert( struct btreenode **sr,intnum)
{
if ( *sr == NULL )
{
*sr = malloc( sizeof ( structbtreenode ) ) ;
( *sr ) -> leftchild=NULL ;
( *sr ) -> data = num;
( *sr ) -> rightchild=NULL ;
return;
}
else /*search the node to whichnewnode will be attached*/
{
/* if newdata is less,traverse toleft*/
if ( num< ( *sr ) -> data )
insert( &( ( *sr ) -> leftchild),num) ;
else
/* else traverse toright*/
insert( &( ( *sr ) -> rightchild),num) ;
}
return;
}
/* traverse a binarysearchtree in a LDR (Left-Data-Right) fashion*/
voidinorder( struct btreenode *sr)
{
if ( sr != NULL )
{
inorder( sr -> leftchild) ;
/* printthe data of the node whose leftchildisNULLor the pathhas alreadybeentraversed*/
printf ( "t%d",sr -> data ) ;
inorder( sr -> rightchild) ;
}
else
return;
}
/* traverse a binarysearch tree in a DLR (Data-Left-right) fashion*/
voidpreorder( struct btreenode *sr)
{
if ( sr != NULL )
{
/* printthe data of a node */
printf ( "t%d",sr -> data ) ;
/* traverse till leftchildisnotNULL*/
preorder( sr -> leftchild) ;
/* traverse till rightchildisnotNULL*/
preorder( sr -> rightchild) ;
}
else
return;
}
/* traverse a binarysearchtree in LRD (Left-Right-Data) fashion*/
voidpostorder( struct btreenode *sr)
{
if ( sr != NULL )
{
postorder( sr -> leftchild) ;
postorder( sr -> rightchild) ;
printf ( "t%d",sr -> data ) ;
}
else
return;
}
(b) IMPLEMENTATION OF STACK ADT
This C++ program implements the follow ing stack operations.
 Push
 Pop
 Top
 Empty
ALGORITHM/ STEPS:
 Create an array to store the stack elements.
 Get the size of the stack.
 To push an element into the stack check if the top element is less than the size and increment
the top.
 Else print overflow .
 To pop an element, check if the stack is empty and decrement the stack.
 If all the elements are popped, print underflow .
 Find the topmost element in the stack by checking if the size is equal to top.
 If the stack is empty print empty, else print not empty.
CODING:
#include<iostream.h>
#include<conio.h>
int max=7;
int t=0;
class stack
{
int s[7];
public:
void push(int);
void pop();
void top();
void empty();
void show ();
};
void stack::push(int y) //Push Operation
{
if(t<max)
{
t=t+1;
s[t]=y;
}
else
cout<<endl<<"stack overflow s..."<<endl;
}
void stack::pop() //Pop Operation
{
int item;
if(t>=0)
{
t=t-1;
item=s[t+1];
cout<<endl<<"popped item >>"<<item<<endl;
}
else
cout<<endl<<"stack underflow s"<<endl;
}
void stack::top() //To find the top of the stack
{
if(t>=0)
cout<<endl<<"topmost element >> "<<s[t]<<endl;
else
cout<<endl<<"stack underflow s..."<<endl;
}
void stack::empty() //To check if the stack is empty
{
if(t<0)
cout<<endl<<"stack is empty..."<<endl;
else
cout<<endl<<"stack is not empty..."<<endl;
}
void main()
{
int a,x;
stack s1;
clrscr();
do
{
cout<<"enter an option..."<<endl<<"1-push"<<endl<<"2-pop"<<endl<<"3-top"<<endl<<"4-empty"<<endl;
cout<<"5-end"<<endl;
cin>>a;
cout<<endl;
sw itch(a)
{
case 1:
{
cout<<endl<<"enter a value >> "<<endl;
cin>>x;
s1.push(x);
}
break;
case 2:
s1.pop();
break;
case 3:
s1.top();
break;
case 4:
s1.empty();
break;
}
}
w hile(a!=5);
getch();
}
(c ) IMPLEMENTATION OF QUEUE ADT
Example 1
Algorithm for Implementation of Queue in C++
1. Declare and initialize neccessary variables, front = 0, rear = -1 etc.
2. For enque operation,
If rear >= MAXSIZE - 1
print "Queue is full"
Else
- Increment rear by 1 i.e. rear = rear + 1;
- queue[rear] = item;
3. For next enqueue operation, goto step 2.
4. For dequeue operation
If front > rear
print "Queue is Empty"
Else
- item = queue[front]
- increment front by 1 i.e. front = front + 1
5. For dequeue next data items, goto step 4.
6. Stop
Source Code:
#include<iostream>
#include<cstdlib>
#define MAX_SIZE 10
using namespace std;
class Queue{
private:
int item[MAX_SIZE];
int rear;
int front;
public:
Queue();
void enqueue(int);
int dequeue();
int size();
void display();
bool isEmpty();
bool isFull();
};
Queue::Queue(){
rear = -1;
front = 0;
}
void Queue::enqueue(int data){
item[++rear] = data;
}
int Queue::dequeue(){
return item[front++];
}
void Queue::display(){
if(!this->isEmpty()){
for(int i=front; i<=rear; i++)
cout<<item[i]<<endl;
}else{
cout<<"Queue Underflow"<<endl;
}
}
int Queue::size(){
return (rear - front + 1);
}
bool Queue::isEmpty(){
if(front>rear){
return true;
}else{
return false;
}
}
bool Queue::isFull(){
if(this->size()>=MAX_SIZE){
return true;
}else{
return false;
}
}
int main(){
Queue queue;
int choice, data;
while(1){
cout<<"n1. Enqueuen2. Dequeuen3. Sizen4. Display all elementn5. Quit";
cout<<"nEnter your choice: ";
cin>>choice;
switch(choice){
case 1:
if(!queue.isFull()){
cout<<"nEnter data: ";
cin>>data;
queue.enqueue(data);
}else{
cout<<"Queue is Full"<<endl;
}
break;
case 2:
if(!queue.isEmpty()){
cout<<"The data dequeued is :"<<queue.dequeue();
}else{
cout<<"Queue is Emtpy"<<endl;
}
break;
case 3:
cout<<"Size of Queue is "<<queue.size();
break;
case 4:
queue.display();
break;
case 5:
exit(0);
break;
}
}
return 0;
}
Example 2
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
}
};
main()
{
int ch;
queue qu;
while(1)
{
cout <<"n1.insert 2.delet 3.display 4.exitnEnter ur
choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
qu.insert(ch);
break;
case 2: qu.delet(); break;
case 3: qu.display();break;
case 4: exit(0);
}
}
return (0);
}
EXAMPLE 3
#include<stdio.h>
#include<conio.h>
#include<process.h>
int queue[5];
long front,rear;
void initqueue();
void display();
void main()
{
int choice,info;
clrscr();
while(1)
{
clrscr();
printf(" MENU n");
printf("1.Insert an element in queuen");
printf("2.Delete an element from queuen");
printf("3.Display the queuen");
printf("4.Exit!n");
printf("Your choice: ");
scanf("%i",&choice);
//Coding by: Snehil Khanor
//http://WapCPP.blogspot.com
switch(choice)
{
case 1:if(rear<4)
{
printf("enter the number: ");
scanf("%d",&info);
if (front==-1)
{
front=0;
rear=0;
}
else
rear=rear+1;
queue[rear]=info;
}
else
printf("queue is full");
getch();
break;
case 2: int info;
if(front!=-1)
{
info=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
front=front+1;
printf("no deleted is = %d",info);
}
else
printf("queue is empty");
getch();
break;
case 3: display();
getch();
break;
case 4: exit(1);
break;
default:printf("You entered wrong choice!");
getch();
break;
}
}
}
void initqueue()
{
front=rear=-1;
}
void display()
{
int i;
for(i=front;i<=rear;i++)
printf("%in",queue[i]);
}

C++ adt c++ implementations

  • 1.
    PROGRAM TO COMPAREAN ARRAY OF NUMBERS #include<iostream> using namespace std; int main() { //constant number const int max=10; int temp; int number[max]; int i; float avg; //this program calculates the sum of array elemens int sum=0; cout<<"please enter 10 intergers. n"; for(i=0;i<max;i++) { cout<<"Number "<<i+1<<": "; cin>>number[i]; sum+=number[i]; } avg=sum/max; cout<<"The array elements are:"<<"nn"; for(i=0;i<max;i++) { cout<<number[i]<<","; } cout<<"nn The sum is "<<sum<<"nn"; cout<<"The average is:"<<avg<<"nn"; //descending order sort for(int k =0;k<max;k++) { for(int x=0;x<max;x++) { if(number[x]<number[x+1]) { //swap the two numbers temp = number[x]; number[x]=number[x+1]; number[x+1]=temp; } } } cout<<"nn Descending order :"<<"nn"; //output in descending order for(i=0;i<max;i++) { cout<<number[i]<<","; } //ascending order sort for(int j =0;j<max;j++) {
  • 2.
    for(i=0;i<max;i++) { if(number[i]>number[i+1]) { //swap the twonumbers temp = number[i]; number[i]=number[i+1]; number[i+1]=temp; } } } cout<<"nn Ascending order :"<<"nn"; //output in ascending order for(i=0;i<max;i++) { cout<<number[i]<<","; } return 0; } PROGRAM2 #include<iostream> #include<math.h> main() { double a,b,c,s,area, perimeter; s=(a+b+c)*0.5; area=sqrt(s*(s-a)*(s-b)*(s-c)); perimeter=a+b+c; cout <"nter a" cin>>a; cout<<"Enter b"; cin>>b; cout<<"Enter c"; cin>>c; cout<<"Area is"<<""<<area<<"n"; cout<<"Perimeter is"<<""<<perimeter<<"n"; } PROGRAM3 /* * Salary allocating system using a Boolean expression. */ #include <iostream.h> int main() { int pay; int HourlySalary;
  • 3.
    int TotalHours; cout<<:"Enter salary"; cin>>HourlySalary; cout<<endl; cout<<"Enterthe total hours worked"<<endl; cin>>TotalHours; if(TotalHours<=40) pay=HourlySalary*TotalHours; else pay=(40.0*HourlySalary)+(TotalHours-40)*(HourlySalary*1.5); cout<<endl; cout<<"Worker’s pay is $"<<pay; cout<<endl; return 0; } http://programming- technique.blogspot.com/search/label/Android%20Application%20Development IMPLEMENTATION OF ADTs (a) IMPLEMENTATION OF TREE ADT Example 1 #include <iostream> #include <cstdlib> usingnamespace std; classBinarySearchTree { private: structtree_node
  • 4.
    { tree_node*left; tree_node*right; int data; }; tree_node*root; public: BinarySearchTree() { root =NULL; } bool isEmpty() const{ returnroot==NULL; } voidprint_inorder(); voidinorder(tree_node*); voidprint_preorder(); voidpreorder(tree_node*); voidprint_postorder(); voidpostorder(tree_node*); voidinsert(int); voidremove(int); }; // Smallerelementsgoleft // largerelementsgoright voidBinarySearchTree::insert(intd)
  • 5.
    { tree_node*t = newtree_node; tree_node*parent; t->data= d; t->left= NULL; t->right= NULL; parent= NULL; //is thisa newtree? if(isEmpty()) root=t; else { //Note:ALLinsertionsare asleaf nodes tree_node*curr; curr = root; // Findthe Node'sparent while(curr) { parent= curr; if(t->data>curr->data) curr = curr->right; else curr= curr->left; } if(t->data< parent->data) parent->left=t; else
  • 6.
    parent->right= t; } } voidBinarySearchTree::remove(intd) { //Locate theelement bool found= false; if(isEmpty()) { cout<<" ThisTree is empty!"<<endl; return; } tree_node*curr; tree_node*parent; curr = root; while(curr!=NULL) { if(curr->data== d) { found= true; break; } else {
  • 7.
    parent= curr; if(d>curr->data) curr=curr->right; else curr = curr->left; } } if(!found) { cout<<" Data not found!"<<endl; return; } // 3 cases: // 1. We're removingaleaf node // 2. We're removinganode witha single child // 3. we're removinganode with2 children // Node withsingle child if((curr->left==NULL && curr->right!= NULL)||(curr->left!= NULL && curr->right == NULL)) { if(curr->left==NULL && curr->right!= NULL) { if(parent->left==curr) {
  • 8.
    parent->left=curr->right; delete curr; } else { parent->right= curr->right; deletecurr; } } else //leftchildpresent,norightchild { if(parent->left==curr) { parent->left=curr->left; delete curr; } else { parent->right= curr->left; delete curr; } } return; }
  • 9.
    //We're lookingata leafnode if( curr->left== NULL && curr->right== NULL) { if(parent->left==curr) parent->left=NULL; else parent->right=NULL; delete curr; return; } //Node with2 children // replace node withsmallestvalue inrightsubtree if (curr->left!= NULL && curr->right != NULL) { tree_node*chkr; chkr = curr->right; if((chkr->left==NULL) && (chkr->right== NULL)) { curr = chkr; delete chkr; curr->right= NULL; } else //rightchildhaschildren { //if the node'srightchildhasa leftchild
  • 10.
    //Move all theway downlefttolocate smallestelement if((curr->right)->left!=NULL) { tree_node*lcurr; tree_node*lcurrp; lcurrp = curr->right; lcurr = (curr->right)->left; while(lcurr->left!=NULL) { lcurrp = lcurr; lcurr = lcurr->left; } curr->data = lcurr->data; delete lcurr; lcurrp->left=NULL; } else { tree_node*tmp; tmp= curr->right; curr->data = tmp->data; curr->right = tmp->right; delete tmp; }
  • 11.
  • 12.
    preorder(root); } voidBinarySearchTree::preorder(tree_node*p) { if(p!= NULL) { cout<<" "<<p->data<<""; if(p->left) preorder(p->left); if(p->right) preorder(p->right); } else return; } voidBinarySearchTree::print_postorder() { postorder(root); } voidBinarySearchTree::postorder(tree_node*p) { if(p!= NULL) { if(p->left) postorder(p->left); if(p->right) postorder(p->right);
  • 13.
    cout<<" "<<p->data<<" "; } elsereturn; } intmain() { BinarySearchTree b; int ch,tmp,tmp1; while(1) { cout<<endl<<endl; cout<<" BinarySearchTree Operations"<<endl; cout<<" ----------------------------- "<<endl; cout<<" 1. Insertion/Creation"<<endl; cout<<" 2. In-OrderTraversal "<<endl; cout<<" 3. Pre-OrderTraversal "<<endl; cout<<" 4. Post-OrderTraversal "<<endl; cout<<" 5. Removal "<<endl; cout<<" 6. Exit"<<endl; cout<<" Enteryour choice : "; cin>>ch; switch(ch) { case 1 : cout<<" Enter Numbertobe inserted:";
  • 14.
    cin>>tmp; b.insert(tmp); break; case 2 :cout<<endl; cout<<" In-OrderTraversal "<<endl; cout<<" -------------------"<<endl; b.print_inorder(); break; case 3 : cout<<endl; cout<<" Pre-OrderTraversal "<<endl; cout<<" -------------------"<<endl; b.print_preorder(); break; case 4 : cout<<endl; cout<<" Post-OrderTraversal "<<endl; cout<<" --------------------"<<endl; b.print_postorder(); break; case 5 : cout<<" Enter data to be deleted:"; cin>>tmp1; b.remove(tmp1); break; case 6 : system("pause"); return 0; break;
  • 15.
    } } } } Example 2 #include <stdio.h> #include<conio.h> #include <alloc.h> struct btreenode { struct btreenode *leftchild; intdata ; struct btreenode *rightchild; } ; voidinsert( struct btreenode **,int) ; voidinorder( struct btreenode *) ; voidpreorder( struct btreenode *) ; voidpostorder( struct btreenode *) ; voidmain( ) { struct btreenode *bt; intreq,i = 1, num ; bt = NULL ; /* emptytree */ clrscr( ) ; printf ( "Specifythe numberof itemstobe inserted:") ; scanf ( "%d",&req ) ; while ( i++ <= req) { printf ( "Enter the data: " ) ; scanf ( "%d",&num ) ; insert( &bt, num) ; } printf ( "nIn-orderTraversal:") ; inorder( bt ) ; printf ( "nPre-orderTraversal:") ; preorder( bt ) ; printf ( "nPost-orderTraversal:") ;
  • 16.
    postorder( bt ); } /* insertsa newnode ina binarysearchtree */ voidinsert( struct btreenode **sr,intnum) { if ( *sr == NULL ) { *sr = malloc( sizeof ( structbtreenode ) ) ; ( *sr ) -> leftchild=NULL ; ( *sr ) -> data = num; ( *sr ) -> rightchild=NULL ; return; } else /*search the node to whichnewnode will be attached*/ { /* if newdata is less,traverse toleft*/ if ( num< ( *sr ) -> data ) insert( &( ( *sr ) -> leftchild),num) ; else /* else traverse toright*/ insert( &( ( *sr ) -> rightchild),num) ; } return; } /* traverse a binarysearchtree in a LDR (Left-Data-Right) fashion*/ voidinorder( struct btreenode *sr) { if ( sr != NULL ) { inorder( sr -> leftchild) ; /* printthe data of the node whose leftchildisNULLor the pathhas alreadybeentraversed*/ printf ( "t%d",sr -> data ) ; inorder( sr -> rightchild) ; } else return; } /* traverse a binarysearch tree in a DLR (Data-Left-right) fashion*/ voidpreorder( struct btreenode *sr) { if ( sr != NULL ) {
  • 17.
    /* printthe dataof a node */ printf ( "t%d",sr -> data ) ; /* traverse till leftchildisnotNULL*/ preorder( sr -> leftchild) ; /* traverse till rightchildisnotNULL*/ preorder( sr -> rightchild) ; } else return; } /* traverse a binarysearchtree in LRD (Left-Right-Data) fashion*/ voidpostorder( struct btreenode *sr) { if ( sr != NULL ) { postorder( sr -> leftchild) ; postorder( sr -> rightchild) ; printf ( "t%d",sr -> data ) ; } else return; } (b) IMPLEMENTATION OF STACK ADT This C++ program implements the follow ing stack operations.  Push  Pop  Top  Empty ALGORITHM/ STEPS:  Create an array to store the stack elements.  Get the size of the stack.  To push an element into the stack check if the top element is less than the size and increment the top.  Else print overflow .  To pop an element, check if the stack is empty and decrement the stack.  If all the elements are popped, print underflow .  Find the topmost element in the stack by checking if the size is equal to top.  If the stack is empty print empty, else print not empty. CODING: #include<iostream.h> #include<conio.h> int max=7;
  • 18.
    int t=0; class stack { ints[7]; public: void push(int); void pop(); void top(); void empty(); void show (); }; void stack::push(int y) //Push Operation { if(t<max) { t=t+1; s[t]=y; } else cout<<endl<<"stack overflow s..."<<endl; } void stack::pop() //Pop Operation { int item; if(t>=0) { t=t-1; item=s[t+1]; cout<<endl<<"popped item >>"<<item<<endl; } else cout<<endl<<"stack underflow s"<<endl; } void stack::top() //To find the top of the stack { if(t>=0) cout<<endl<<"topmost element >> "<<s[t]<<endl; else cout<<endl<<"stack underflow s..."<<endl; } void stack::empty() //To check if the stack is empty { if(t<0) cout<<endl<<"stack is empty..."<<endl; else cout<<endl<<"stack is not empty..."<<endl; } void main() { int a,x; stack s1; clrscr(); do { cout<<"enter an option..."<<endl<<"1-push"<<endl<<"2-pop"<<endl<<"3-top"<<endl<<"4-empty"<<endl; cout<<"5-end"<<endl; cin>>a; cout<<endl; sw itch(a) {
  • 19.
    case 1: { cout<<endl<<"enter avalue >> "<<endl; cin>>x; s1.push(x); } break; case 2: s1.pop(); break; case 3: s1.top(); break; case 4: s1.empty(); break; } } w hile(a!=5); getch(); } (c ) IMPLEMENTATION OF QUEUE ADT Example 1 Algorithm for Implementation of Queue in C++ 1. Declare and initialize neccessary variables, front = 0, rear = -1 etc. 2. For enque operation, If rear >= MAXSIZE - 1 print "Queue is full" Else - Increment rear by 1 i.e. rear = rear + 1; - queue[rear] = item; 3. For next enqueue operation, goto step 2. 4. For dequeue operation If front > rear print "Queue is Empty" Else - item = queue[front] - increment front by 1 i.e. front = front + 1 5. For dequeue next data items, goto step 4. 6. Stop Source Code: #include<iostream> #include<cstdlib> #define MAX_SIZE 10
  • 20.
    using namespace std; classQueue{ private: int item[MAX_SIZE]; int rear; int front; public: Queue(); void enqueue(int); int dequeue(); int size(); void display(); bool isEmpty(); bool isFull(); }; Queue::Queue(){ rear = -1; front = 0; } void Queue::enqueue(int data){ item[++rear] = data; } int Queue::dequeue(){ return item[front++]; } void Queue::display(){ if(!this->isEmpty()){ for(int i=front; i<=rear; i++) cout<<item[i]<<endl; }else{ cout<<"Queue Underflow"<<endl; } } int Queue::size(){ return (rear - front + 1); } bool Queue::isEmpty(){ if(front>rear){ return true; }else{ return false; } } bool Queue::isFull(){ if(this->size()>=MAX_SIZE){ return true; }else{ return false; } } int main(){ Queue queue; int choice, data; while(1){
  • 21.
    cout<<"n1. Enqueuen2. Dequeuen3.Sizen4. Display all elementn5. Quit"; cout<<"nEnter your choice: "; cin>>choice; switch(choice){ case 1: if(!queue.isFull()){ cout<<"nEnter data: "; cin>>data; queue.enqueue(data); }else{ cout<<"Queue is Full"<<endl; } break; case 2: if(!queue.isEmpty()){ cout<<"The data dequeued is :"<<queue.dequeue(); }else{ cout<<"Queue is Emtpy"<<endl; } break; case 3: cout<<"Size of Queue is "<<queue.size(); break; case 4: queue.display(); break; case 5: exit(0); break; } } return 0; } Example 2 #include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; class queue { int queue1[5]; int rear,front; public: queue() { rear=-1; front=-1; }
  • 22.
    void insert(int x) { if(rear> 4) { cout <<"queue over flow"; front=rear=-1; return; } queue1[++rear]=x; cout <<"inserted" <<x; } void delet() { if(front==rear) { cout <<"queue under flow"; return; } cout <<"deleted" <<queue1[++front]; } void display() { if(rear==front) { cout <<" queue empty"; return; } for(int i=front+1;i<=rear;i++) cout <<queue1[i]<<" "; } }; main() { int ch; queue qu; while(1) { cout <<"n1.insert 2.delet 3.display 4.exitnEnter ur choice"; cin >> ch; switch(ch) { case 1: cout <<"enter the element"; cin >> ch; qu.insert(ch); break; case 2: qu.delet(); break; case 3: qu.display();break; case 4: exit(0); } } return (0); }
  • 23.
    EXAMPLE 3 #include<stdio.h> #include<conio.h> #include<process.h> int queue[5]; longfront,rear; void initqueue(); void display(); void main() { int choice,info; clrscr(); while(1) { clrscr(); printf(" MENU n"); printf("1.Insert an element in queuen"); printf("2.Delete an element from queuen"); printf("3.Display the queuen"); printf("4.Exit!n"); printf("Your choice: "); scanf("%i",&choice); //Coding by: Snehil Khanor
  • 24.
    //http://WapCPP.blogspot.com switch(choice) { case 1:if(rear<4) { printf("enter thenumber: "); scanf("%d",&info); if (front==-1) { front=0; rear=0; } else rear=rear+1; queue[rear]=info; } else printf("queue is full"); getch(); break; case 2: int info; if(front!=-1) {
  • 25.
    info=queue[front]; if(front==rear) { front=-1; rear=-1; } else front=front+1; printf("no deleted is= %d",info); } else printf("queue is empty"); getch(); break; case 3: display(); getch(); break; case 4: exit(1); break; default:printf("You entered wrong choice!"); getch(); break; }
  • 26.
    } } void initqueue() { front=rear=-1; } void display() { inti; for(i=front;i<=rear;i++) printf("%in",queue[i]); }