Programs
Programs
h>
#include<conio.h>
//tower of HANOI function implementation
void TOH(int n, char Sour, char Aux, char Des)
{
if (n == 1) {
cout << "Move Disk " << n << " from " << Sour << " to " << Des << endl;
return;
}
//main program
int main()
{
int n;
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
class treetrav
{
public:
struct tree
{
char no;
struct tree *left,*right;
}
*start,*root,*temp,*st[30];
int top;
treetrav()
{
start=root=NULL;
top=0;
}
void create();
void view();
void preorder(struct tree*);
void postorder(struct tree*);
void inorder(struct tree*);
};
void treetrav::create()
{
char ch;
root=start=new(struct tree);
cout<<"\n \t Enter the Root node:";
cin>>root->no;
root->left=NULL;
root->right=NULL;
while(root)
{
cout<<"\n \t Enter the Left child:";
cout<<root->no<<"Exist?(Y/N):";
cin>>ch;
if(tolower (ch)=='y')
{
cout<<"\n \t Enter the Left child of:";
cout<<root->no<<":";
temp=new(struct tree);
cin>>temp->no;
root->left=temp;
temp->left=NULL;
temp->right=NULL;
}
else
root->left=NULL;
cout<<"\n \t \t Is Right child of:";
cout<<root->no<<"Exist?(Y/N):";
cin>>ch;
if(tolower (ch)=='y')
{
cout<<"\n \t Enter the Right child of:";
cout<<root->no<<":";
temp=new(struct tree);
cin>>temp->no;
root->right=temp;
temp->left=NULL;
temp->right=NULL;
st[++top]=temp;
}
else
root->right=NULL;
if(root->left!=NULL)
root=root->left;
else
{
if(top==0)
root=NULL;
else
root=st[top--];
}
}
cout<<"\n";
}
void main()
{
treetrav t;
clrscr();
t.create();
cout<<"\n \t Preorder \t:\t";
t.preorder(t.start);
cout<<"\n \t Inorder \t:\t";
t.inorder(t.start);
cout<<"\n \t Postorder \t:\t";
t.postorder(t.start);
getch();
}
Stack Operations Using linked list
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* top = NULL;
void push(int val) {
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
newnode->data = val;
newnode->next = top;
top = newnode;
}
void pop() {
if(top==NULL)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< top->data <<endl;
top = top->next;
}
}
void display() {
struct Node* ptr;
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
cout<<"Stack elements are: ";
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
cout<<endl;
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
Quick Sort
#include<iostream.h>
#include<conio.h>
void swap(int arr[] , int pos1, int pos2){
int temp;
temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
int main()
{
int n,arr[25];;
cout << " enter the number of elements in array\n";
cin>>n;
cout<<”Enter the array Elements one by one\n”
Heap Sort
#include<iostream.h>
#include<conio.h>
void heapify(int arr[],int n,int i)
{
int t;
int la=i;
int l=2*i+1;
int r=2*i+2;
if(l<n&&arr[l]>arr[la])
la=l;
if(r<n && arr[r]>arr[la])
la=r;
if(la!=i)
{
t=arr[i];
arr[i]=arr[la];
arr[la]=t;
heapify(arr,n,la);
}
}
void heapsort(int arr[],int n)
{
int i,t;
for(i=n/2-1;i>=0;i--)
heapify(arr,n,i);
for(i=n-1;i>=0;i--)
{
t=arr[0];
arr[0]=arr[i];
arr[i]=t;
heapify(arr,i,0);
}
}
int main()
{
int arr[50],n,i;
cout<<"Enter the value of n\n";
cin>>n;
cout<<"Enter the elements of array:\n";
for(i=0;i<n;i++)
cin>>arr[i];
heapsort(arr,n);
cout<<"Sorted elements are:\n";
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
getch();
return 0;
}