C++ DSA CODE
C++ DSA CODE
#include<iostream>
using namespace std;
class stack
{
private:
int stk[10];
int size;
int top;
public:
void initialize()
{
size=5;
top=-1;
}
void push();
void pop();
void display();
};
void stack::push()
{
int value;
cout<<"enter the element to be pushed"<<endl;
cin>>value;
if(top==size)
{
cout<<"stack is full"<<endl;
}
else
{
top=top+1;
stk[top]=value;
cout<<"the element "<<value<<" is inserted at the top of stack"<<endl;
}
}
void stack::pop()
{
if(top==-1)
{
cout<<"stack is empty"<<endl;
}
else
{
int value;
value=stk[top];
top=top-1;
cout<<"the value popped is "<<value<<endl;
}
}
void stack::display()
{
if(top==-1)
{
cout<<"stack is empty"<<endl;
}
else
{
cout<<"the present stack elements are "<<endl;
for(int i=top;i>=0;i--)
{
cout<<stk[i]<<endl;
}
}
}
main()
{
stack s;
s.initialize();
int rpt=1,choice;
while(rpt)
{
cout<<"********************************"<<endl;
cout<<"enter 1 to push()"<<endl;
cout<<"enter 2 to pop()"<<endl;
cout<<"enter 3 to display()"<<endl;
cout<<"********************************"<<endl;
cout<<"enter ur choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
}
cout<<"do u want to repeat? if yes press 1, else press 0"<<endl;
cin>>rpt;
}
}
}
//queue using arrays
#include<iostream>
using namespace std;
class queue
{
private:
int q[10];
int size;
int front, rear;
public:
queue()
{
size=4;
front=-1;rear=-1;
}
void insert();
void remove();
void display();
};
void queue::insert()
{
if(rear>=size)
{
cout<<"queue overflows"<<endl;
}
else
{ int value;
cout<<"enter the value to be inserted"<<endl;
cin>>value;
rear=rear+1;
q[rear]=value;
cout<<"the value "<<value<<" is inserted in queue from the rear end"<<endl;
}
}
void queue::remove()
{
if(front==rear)
{
cout<<"queue underflows"<<endl;
}
else
{
front=front+1;
cout<<"the removed value from the queue is="<<q[front]<<endl;
}
}
void queue::display()
{
if(front==rear)
{
cout<<"queue underflows"<<endl;
}
else
{
cout<<"the queue elements are "<<endl;
for(int i=front+1;i<=rear;i++)
{
cout<<q[i]<<" ";
}
cout<<endl;
}
}
main()
{
queue q1;
int choice,rpt=1;
while(rpt)
{
cout<<"**********************************"<<endl;
cout<<"enter 1 to insert"<<endl;
cout<<"enter 2 to delete"<<endl;
cout<<"enter 3 to display"<<endl;
cout<<"**********************************"<<endl;
cout<<"enter ur choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
q1.insert();
break;
case 2:
q1.remove();
break;
case 3:
q1.display();
break;
}
cout<<"do u want to repeat? if yes press 1, else press 0"<<endl;
cin>>rpt;
}
}
}
TOWER OF HANOI
#include <iostream>
using namespace std;
//main program
int main()
{
int n;
return 0;
}
Balanced Brackets
// C++ program to check for balanced brackets.
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
string expr = "{()}[]";
// Function call
if (areBracketsBalanced(expr))
cout << "Balanced";
else
cout << "Not Balanced";
return 0;
}
// Index in bucket
int bi = n * arr[i];
b[bi].push_back(arr[i]);
}
// Driver program
int main()
{
int arr[] = { 60 ,20 ,40 ,70, 30, 10};
int n = sizeof(arr) / sizeof(arr[0]);
//heapify algorithm
// the loop must go reverse you will get after analyzing manually
// (i=n/2 -1) because other nodes/ ele's are leaf nodes
// (i=n/2 -1) for 0 based indexing
// (i=n/2) for 1 based indexing
for(int i=n/2 -1;i>=0;i--){
heapify(arr,n,i);
}
heapSort(arr, n);
return 0;
}
//code by Prajwal Chougale