DSA ANS
DSA ANS
#include <iostream>
using namespace std;
struct node{
int n;
node* x = NULL;
};
node* h = NULL;
void print(){
while(h!=NULL){
cout<<h->n<<" ";
h = h->x;
}
}
int main(){
int n;
while(cin>>n){
insert(n);
}
print();
}
------------------------------------------------------------------
2. Doubly Linked List
#include <iostream>
using namespace std;
struct node{
int n;
node* x;
node* y = NULL;
};
node* h = NULL;
node* t = NULL;
void pf(){
cout<<"Doubly linked list forwards : ";
while(h!=NULL){
cout<<h->n<<" ";
h = h->y;
}
}
void pr(){
cout<<"\nDoubly linked list backwards : ";
while(t!=NULL){
cout<<t->n<<" ";
t = t->x;
}
}
int main(){
int n;
while(cin>>n){
insert(n);
}
pf();
pr();
}
-----------------------------------------------------------------------------------
-------
3. Stack using Array
#include <iostream>
using namespace std;
int arr[100];
int top = -1;
void insert(int n){
if(top >= 99){
return;
}
arr[++top] = n;
}
int main(){
int n,a;
cin>>n;
cout<<"Stack Elements\n";
for(int i=0;i<n;i++){
cin>>a;
insert(a);
}
for(int i=top;i>=0;i--){
cout<<arr[i]<<"\n";
}
}
-----------------------------------------------------------------------------------
----------
4. Stack using Linked List
#include <iostream>
using namespace std;
struct node{
int n;
node* x;
};
node* h = NULL;
void print(){
cout<<"Stack Elements\n";
while(h!=NULL){
cout<<h->n<<"\n";
h = h->x;
}
}
int main(){
int n;
char ch;
while(cin>>n){
push(n);
cin>>ch;
if(ch!='Y')
break;
}
print();
}
-----------------------------------------------------------------------------------
---
5. Infix to Postfix
#include<iostream>
#include<stack>
using namespace std;
int priority(char c){
if(c=='+' || c=='-')
return 1;
if(c=='*' || c=='/')
return 2;
if(c=='^')
return 3;
return 0;
}
while(expr[i] !='\0'){
if(expr[i]>='a' && expr[i]<='z' || expr[i]>'A' && expr[i]<='Z'){
postfix += expr[i];
i++;
}
else if(expr[i]=='('){
s.push(expr[i]);
i++;
}
else if(expr[i]==')'){
while(s.top()!='('){
postfix += s.top();
s.pop();
}
s.pop();
i++;
}
else{
while(!s.empty() && priority(expr[i]) <= priority(s.top())){
postfix += s.top();
s.pop();
}
s.push(expr[i]);
i++;
}
}
while(!s.empty()){
postfix += s.top();
s.pop();
}
cout<<postfix;
}
int main(){
string expr;
cin>>expr;
string postfix;
convert(expr);
}
-----------------------------------------------------------------------------------
---
6. Delete at end of DLL
#include <iostream>
using namespace std;
struct node{
int n;
node* x;
node* y = NULL;
};
node* h = NULL;
node* t = NULL;
void print(){
while(h!=NULL){
cout<<h->n<<" ";
h = h->y;
}
}
void delatlast(){
if(t->x!=NULL){
t = t->x;
t->y = NULL;
}
}
int main(){
int n,a,m;
cin>>n;
for(int i=0;i<n;i++){
cin>>a;
insert(a);
}
print();
cin>>m;
for(int i=0;i<m;i++){
delatlast();
}
print();
}
-----------------------------------------------------------------------------------
---
7. Queue using Linked List
#include <iostream>
using namespace std;
struct node{
int n;
node* x;
node* y = NULL;
};
node* h = NULL;
node* t = NULL;
void front(){
cout<<h->n<<" ";
}
void rear(){
cout<<t->n<<" ";
}
void dequeue(){
h = h->y;
h->x = NULL;
}
int main(){
int n,a;
cin>>n;
for(int i=0;i<n;i++){
cin>>a;
enqueue(a);
}
front();
rear();
dequeue();
front();
rear();
}
------------------------------------------------------------------------------
8. Packet Collision
#include <iostream>
using namespace std;
int main(){
int m,n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cin>>m;
for(int i=0;i<=n-m;i++){
int mx = 0;
for(int j=i;j<i+m;j++){
mx = max(mx,arr[j]);
}
cout<<mx<<" ";
}
}
------------------------------------------------------------------------------
9. Binary Search Tree
#include <iostream>
using namespace std;
struct node{
int n;
node* l = NULL;
node* r = NULL;
};
#include<iostream>
#include<vector>
using namespace std;
void addedge(vector<int>adj[],int u,int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void print(vector<int>adj[],int a)
{
for(int i=0;i<a;i++)
{
cout<<i;
for(int j=0;j<adj[i].size();j++)
{
cout<<" -> "<<adj[i][j];
}
cout<<endl;
}
}
int main()
{
int V,a,b;
cin>>V;
vector<int>adj[V];
int E;
cin>>E;
for(int i=0;i<E;i++)
{
cin>>a>>b;
addedge(adj,a,b);
}
print(adj,V);
}
---------------------------------------------------------------------------------
11. Subset using Hashing
#include<iostream>
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
int arr1[m];
int arr2[n];
for(int i=0;i<m;i++){
cin>>arr1[i];
}
int c=0;
for(int i=0;i<n;i++){
cin>>arr2[i];
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(arr2[i]==arr1[j])
c++;
}
}
if(c==n)
{
cout<<"Subset";
}
else
{
cout<<"Not a subset";
}
}
---------------------------------------------------------------------------------
12. Queue - Insertion and Deletion
#include <iostream>
#include <queue>
using namespace std;
int main(){
queue<int> q;
int n,m,a;
cin>>n;
for(int i=0;i<n;i++){
cin>>a;
q.push(a);
}
cin>>m;
for(int i=0;i<m;i++){
cout<<"Element deleted from queue is : "<<q.front()<<"\n";
q.pop();
}
}