Data Structure Using CPP

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 233

Data structure and STL using c++

Linked list
1) Append an Linked List
Write a c++ program to create a linked list program by using list implementation.
you need get the input and append it into list continuously until the user enters a
negative value.

Input Format:

The input should be numbers (until user enters a negative value)

Output Format:

The output should be numbers in the list in separate line.

Sample Input:

-1

Sample Output:
1

Ans

#include <iostream>

using namespace std;

struct node{

int data;

struct node *next;

}*head=NULL;

void insert(int data){

struct node *newnode = (struct node *) malloc(sizeof(struct node));

newnode->data = data;

newnode->next = NULL;

if(head==NULL){

head = newnode;

else{

struct node *temp;

temp=head;

while(temp->data!=data){

if(temp->next == NULL){

temp->next = newnode;
return;

temp = temp->next;

return;

void print(){

struct node *temp = head;

while(temp!=NULL){

cout<<temp->data<<"\n";

temp = temp->next;

int main(){

int data;

char k[100];

while(1)

cin>>data;

if(data<0)

if(head==NULL)

cout<<"List is empty"<<endl;
}

break;

int l=(int)data;

insert(l);

print();

return 0;

2) Insert at the beginning


Write a c++ program to insert the value at the beginning of the list. Get the value
continuously from the user until user enters a negative value.

Input Format:

The input should be numbers (until user enters a negative value)

Output Format:

The output should be numbers in the list in separate line.

Sample Input:

3
4

-1

Sample Output:

Ans

#include <bits/stdc++.h>

using namespace std;

struct Node {

int data;

struct Node* next;

};

Node* reverse(struct Node* head, int k)

Node* prev = NULL;

Node* curr = head;

Node* temp = NULL;

Node* tail = NULL;

Node* newHead = NULL;

Node* join = NULL;

int t = 0;
while (curr) {

t = k;

join = curr;

prev = NULL;

while (curr && t--) {

temp = curr->next;

curr->next = prev;

prev = curr;

curr = temp;

if (!newHead)

newHead = prev;

if (tail)

tail->next = prev;

tail = join;

return newHead;

void push(Node** head_ref, int new_data)

Node* new_node = new Node();

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

}
void printList(Node* node)

while (node != NULL) {

cout << node->data <<endl;

node = node->next;

int main()

Node* head = NULL;

int data;

while(1){

cin>>data;

if(data<0){

break;

push(&head,data);

int k = data;

printList(head);

head = reverse(head, k);

return (0);

3) Insert at the position - before


Write a c++ program to insert the value at the before
given position at the list. Get the value continuously from
the user until user enters a negative value.

Input Format:

The input should be numbers (until user enters a


negative value)

Output Format:

The output should be numbers in the list in separate line.

Sample Input:

-1

10
Sample Output:

Before inserting the value:

Enter the position and value to be inserted:

After inserting the value:

10

4
Ans

#include<iostream>

using namespace std;

struct node

{
int data;

struct node * link;

};

struct node * head=NULL;

void append(struct node ** headadd, int data)

struct node * temp;

temp = * headadd;

struct node* newnode=(struct node*) malloc(sizeof (struct node));

newnode->data=data;

newnode->link=NULL;

if(*headadd==NULL)

*headadd=newnode;

else

while(temp->link!=NULL)

temp=temp->link;

temp->link=newnode;

void display(struct node* temp)

while(temp!=NULL)
{

cout<<temp->data<<endl;

temp=temp->link;

void display2(struct node* temp)

while(temp!=NULL)

cout<<temp->data<<endl;

temp=temp->link;

void add(int pos,int data)

struct node* temp;

struct node* temp1;

temp=head;

if(1==pos)

struct node* newdata=(struct node*)malloc(sizeof(struct node));

head=newdata;

newdata->data=data;

newdata->link=temp;

}
else

for(int i=2;i<=pos;i++)

if(i==pos)

struct node* newdata=(struct node*)malloc(sizeof(struct node));

temp1=temp->link;

temp->link=newdata;

newdata->data=data;

newdata->link=temp1;

else

temp=temp->link;

}}

int main()

{
head=NULL;

cout<<"Before inserting the value:\n";

int data;

do

cin>>data;

if(data>0)

append(&head,data);

while (data>0);

display(head);

cout<<"Enter the position and value to be inserted:\n";

int pos,value;

cin>>pos>>value;

cout<<"After inserting the value:\n";

add(pos,value);

display2(head);

return 0;

4) Insert at the position - After


Write a c++ program to insert the given value after the
given position.

Input Format:
The input should be numbers (until user enters a
negative value)

Output Format:

The output should be numbers in the list in separate line.

Sample Input:

-1

10

Sample Output:

Before inserting the value:


1

Enter the position and value to be inserted:

After inserting the value:

10

4
Ans

#include<iostream>

#include<stdlib.h>

using namespace std;

class node

public:

int data;

node *next;
};

node *root = NULL;

void append(int data)

node *newnode;

newnode = new node();

newnode -> data = data;

newnode -> next = NULL;

if(root == NULL)

root = newnode;

else

node*temp;

temp = root;

while(temp -> next != NULL)

temp=temp->next;

temp->next = newnode;

void insert(int pos,int data)

int i;

node* newnode = new node();


node *temp=root;

newnode -> data = data;

for(i=1;i<pos;i++)

temp=temp->next;

newnode->next=temp->next;

temp->next = newnode;

void display()

node *temp;

temp = root;

if(temp == NULL)

cout<<"List is Empty"<<endl;

else

while(temp!= NULL)

cout<<temp->data<<endl;

temp=temp->next;

int main()

{
int data;

do{

cin>>data;

if(data<0)

break;

append(data);

}while(data>0);

cout<<"Before inserting the value:"<<endl;

display();

cout<<"Enter the position and value to be inserted:"<<endl;

int pos,da;

cin>>pos>>da;

insert(pos,da);

cout<<"After inserting the value:"<<endl;

display();

return 0;

5) Delete at the beginning


Write a c++ program to delete the given list from the
beginning.

Sample Input:

1
2

-1

Sample Output:

Before Deletion:

After 1 deletion:

After 2 deletion:

3
4

After 3 deletion:

After 4 deletion:

No value to delete
Ans

#include<iostream>

#include<stdlib.h>

using namespace std;

class Node

public:

int data;

Node *next;

};

//Node *root;

void create(int d);

void display();

void del(int s);

Node *root = NULL;

int main()

int d,s=0;
do

cin>>d;

if(d<0)

break;

create(d);

s++;

}while(1);

cout<<"Before deletion:"<<endl;

display();

cin>>s;

for(int i=1;i<=s;i++)

cout<<"After "<<i<<" deletion:"<<endl;

del(s);

display();

return 0;

void create(int d)

Node *new_node;

new_node = new Node();

new_node->data = d;

new_node->next = NULL;
if(root == NULL)

root = new_node;

else

Node*temp;

temp = root;

while(temp->next != NULL)

temp=temp->next;

temp->next = new_node;

void display()

Node *temp;

temp = root;

if(temp == NULL)

cout<<"No value to delete"<<endl;

else

{
while(temp!= NULL)

cout<<temp->data<<endl;

temp=temp->next;

void del(int s)

Node *temp = root;

root=root->next;

free(temp);

6) Delete from the end


Write a c++ program to delete the value of the list from
the end.

Sample Input:

4
-1

Sample Output:

Before deletion:

After 1 deletion:

After 2 deletion:

After 3 deletion:

1
After 4 deletion:

No value to delete
Ans

#include<iostream>

#include<stdlib.h>

using namespace std;

class Node

public:

int data;

Node *next;

};

//Node *root;

void create(int d);

void display();

int del();

Node *root = NULL;

int main()

int d;

do

cin>>d;

if(d<0)
break;

create(d);

}while(1);

cout<<"Before deletion:"<<endl;

display();

int i=1;

while(del())

cout<<"After "<<i<<" deletion:"<<endl;

display();

i++;

return 0;

void create(int d)

Node *new_node;

new_node = new Node();

new_node->data = d;

new_node->next = NULL;

if(root == NULL)

root = new_node;

else
{

Node*temp;

temp = root;

while(temp->next != NULL)

temp=temp->next;

temp->next = new_node;

void display()

Node *temp;

temp = root;

if(temp == NULL)

cout<<"No value to delete"<<endl;

else

while(temp!= NULL)

cout<<temp->data<<endl;

temp=temp->next;

}
}

int del()

Node *temp,*p;

if(root == NULL)

return 0;

else if(root -> next == NULL)

root = NULL;

free(root);

return 1;

else

temp = root;

while(temp -> next != NULL)

p=temp;

temp = temp -> next;

p -> next = NULL;

free(temp);

return 1;

}
}

7) Delete the value from the list


Write a c++ Program to delete the given value from the
list.

Sample Input:

-1

Sample Output:

Before deletion:

2
3

Enter the value to be deleted:

After deletion:

5
Ans

#include <iostream>

using namespace std;

class Node

public:

int data;

Node *next;

};

void append(Node** head, int newdata)

Node* newnode = new Node();


Node *temp=*head;

newnode->data = newdata;

newnode->next = NULL;

if(*head==NULL)

*head = newnode;

else{

while(temp->next!=NULL)

temp=temp->next;

temp->next=newnode;

void delet(Node** head,int a)

if(*head==NULL)

return ;

Node *last=*head,*prev=NULL;

if(last->data==a)

*head=last->next;

else{

do {

prev=last;

last=last->next;

} while(last!=NULL&&last->data!=a);

if(last==NULL)

return ;
prev->next=last->next;

delete(last);

void printList(Node *head)

while (head != NULL)

cout<<head->data<<endl;

head = head->next;

int main()

Node* head = NULL;

int d,a;

do{

cin>>d;

if(d>0)

append(&head,d);

}while(d>0);

cout<<"Before deletion:"<<endl;

printList(head);
cout<<"Enter the value to be deleted:"<<endl;

cin>>a;

delet(&head,a);

cout<<"After deletion:"<<endl;

printList(head);

return 0;

8) Reverse a linked list


Write a c++ Program to print the given list in reverse
order.

Sample Input:

-1

Sample Output:
Before Reversing the list:

After Reversing the list:

1
Ans

#include<iostream>

#include<stdlib.h>

using namespace std;

class Node

public:

int data;

Node *next;

};
void append(int d);

void display();

void reverse();

Node *root = NULL;

int main()

int d;

do{

cin>>d;

if(d>0)

append(d);

else

break;

}while(1);

cout<<"Before Reversing the list:"<<endl;

display();

reverse();

cout<<"After Reversing the list:"<<endl;

display();

return 0;

}
void append(int d)

Node *newnode;

newnode = new Node();

newnode->data = d;

newnode->next = NULL;

if(root == NULL)

root = newnode;

else

Node*temp;

temp = root;

while(temp->next != NULL)

temp=temp->next;

temp->next = newnode;

void display()

Node *temp;

temp = root;
if(temp == NULL)

cout<<"List is Empty"<<endl;

else

while(temp!= NULL)

cout<<temp->data<<endl;

temp=temp->next;

void reverse()

Node* current = root;

Node *prev = NULL;

Node *temp = NULL;

while (current != NULL)

temp = current->next;

current->next = prev;

prev = current;

current = temp;

}
root = prev;

9) Delete Duplicates
Write a c++ Program to delete the duplicate elements
from the given list.

Sample Input:

-1

Sample Output:

Linked list before removal of duplicates

1
2

Linked list after removal of duplicates

4
Ans

#include <iostream>

using namespace std;

class Node

public:

int data;

Node *next;

};

void append(Node** head_ref, int new_data)

{
Node* new_node = new Node();

Node *last=*head_ref;

new_node->data = new_data;

new_node->next = NULL;

if(*head_ref==NULL)

*head_ref = new_node;

else{

while(last->next!=NULL)

last=last->next;

last->next=new_node;

void remov(Node** head_ref){

Node *last=*head_ref;

Node *prev=NULL;

while(last!=NULL){

Node *c=*head_ref;

while(c!=last){

if(c->data!=last->data)

c=c->next;

else{

prev->next=last->next;

delete(last);

last=prev->next;
break;

if(c==last){

prev=last;

last=last->next;

void printList(Node *node)

while (node != NULL)

cout<<node->data<<endl;

node = node->next;

int main()

Node* head = NULL;

int a;

do{

cin>>a;

if(a<0)

break;
append(&head,a);

}while(1);

cout<<"Linked list before removal of duplicates"<<endl;

printList(head);

cout<<"Linked list after removal of duplicates"<<endl;

remov(&head);

printList(head);

return 0;

10) Doubly linked list implementation


Write a c++ Program to insert the given values into a
doubly linked list.

Sample Input:

-1
Sample Output:

4
Ans

#include<iostream>

using namespace std;

class Node

public:

int data;

Node *next,*prev;

};

void append(Node** head_ref, int new_data)

Node* new_node = new Node();

Node *last=*head_ref;

new_node->data = new_data;

new_node->next = new_node->prev=NULL;

if(*head_ref==NULL)

*head_ref = new_node;
else{

while(last->next!=NULL)

last=last->next;

last->next=new_node;

new_node->prev=last;

void printList(Node *node)

while (node != NULL)

cout<<node->data<<endl;

node = node->next;

int main()

Node *head=NULL;

int a;

do{

cin>>a;

if(a<0)

break;

else

append(&head,a);
}while(1);

printList(head);

return 0;

11) Doubly linked list - Reverse


Write a c++ Program to display the list in both original as
well as reverse order of doubly linked list.

Sample Input:

-1

Sample Output:

Original order:

2
3

Reverse order:

1
Ans:

#include<iostream>

using namespace std;

class Node

public:

int data;

Node *next,*prev;

};

void append(Node** head, int newdata)

Node* newnode = new Node();

Node *last=*head;

newnode->data = newdata;

newnode->next = newnode->prev=NULL;
if(*head==NULL)

*head = newnode;

else{

while(last->next!=NULL)

last=last->next;

last->next=newnode;

newnode->prev=last;

void h(Node** head){

Node *p,*t,*node=*head;

while (node!= NULL) {

p=node;

node = node->next;

t=p->prev;

p->prev=p->next;

p->next=t;

*head=p;

void printList(Node *node)

while (node != NULL)

cout<<node->data<<endl;
node = node->next;

int main()

Node *head=NULL;

int a;

do{

cin>>a;

if(a<0)

break;

else

append(&head,a);

}while(1);

cout<<"Original order:"<<endl;

printList(head);

h(&head);

cout<<"Reverse order:"<<endl;

printList(head);

return 0;

12) Add a node after a given node - doubly


linked list
Write a C++ Program to insert a new value after the given
node in doubly linked list.

Sample Input:

-1

Sample Output:

Before inserting:

4
Enter the value and position

After inserting:

4
Ans

#include<iostream>

using namespace std;

class Node

public:

int data;

Node *next,*prev;

};

void append(Node** head, int newdata)

Node* newnode = new Node();

Node *last=*head;

newnode->data = newdata;

newnode->next = newnode->prev=NULL;
if(*head==NULL)

*head = newnode;

else{

while(last->next!=NULL)

last=last->next;

last->next=newnode;

newnode->prev=last;

void insert(Node** head,int posi, int newdata){

Node* newnode = new Node();

Node *last=*head;

newnode->data = newdata;

if(posi==0){

newnode->prev=NULL;

newnode->next = last;

if(last!=NULL)

last->prev=newnode;

*head=newnode;

return;

for(int i=1;i<posi;i++)

last=last->next;

if(last!=NULL){

newnode->prev=last;
newnode->next=last->next;

if(newnode->next!=NULL)

newnode->next->prev=newnode;

last->next=newnode;

void printList(Node *node)

while (node != NULL)

cout<<node->data<<endl;

node = node->next;

int main()

Node *head=NULL;

int a;

do{

cin>>a;

if(a<0)

break;

else

append(&head,a);

}while(1);
cout<<"Before inserting:"<<endl;

printList(head);

int p;

cout<<"Enter the value and position"<<endl;

cin>>p>>a;

insert(&head,p,a);

cout<<"After inserting:"<<endl;

printList(head);

return 0;

13) Circular linked list - implementation


Write a C++ Program to implement circular linked list. If a
user enters a negative value, the last node must be
pointing to first node.

Sample Input:

-1
Sample Output:

4
Ans

#include <iostream>

using namespace std;

class Node

public:

int data;

Node *next;

};

void append(Node** head, int newdata)

Node* newnode = new Node();

Node *last = *head;

newnode->data = newdata;

newnode->next = *head;

if (*head == NULL)
{

*head = newnode;

newnode->next=newnode;

return;

while (last->next != *head)

last = last->next;

last->next = newnode;

return;

void printList(Node *node)

Node *a=node;

do

cout<<node->data<<endl;

node = node->next;

} while (node != a) ;

int main()

Node* head = NULL;

int a;

do{
cin>>a;

if(a<0)

break;

append(&head,a);

}while(1);

printList(head);

return 0;

14) Linked list - Searching an element


Write a c++ Program to search an element from the list.

Sample Input:

-1

Sample Output:
Enter the value to be searched:

3 is present in the list.


Ans

#include <iostream>

using namespace std;

class Node

public:

int data;

Node *next;

};

void append(Node** head, int newdata)

Node* newnode = new Node();

Node *last = *head;

newnode->data = newdata;

newnode->next = NULL;

if (*head == NULL)

*head = newnode;

return;

while (last->next != NULL)


last = last->next;

last->next = newnode;

return;

int List(Node *node,int a)

while (node != NULL)

if(node->data==a)

return 1;

node = node->next;

return 0;

int main()

Node* head = NULL;

int a;

do{

cin>>a;

if(a<0)

break;

append(&head,a);

}while(1);
cout<<"Enter the value to be searched:"<<endl;

cin>>a;

cout<<a;

a= List(head,a);

if(a)

cout<<" is present in the list";

else

cout<<" is not present in the list";

return 0;

15) Linked list - find second last element


Write a C++ Program to find the second last element in
the list. if there is no second last element print "There is
no second last element in the list", otherwise print the
value.

Sample Input:

-1
Sample Output:

3 is the second last element in the list.


Ans

#include <iostream>

using namespace std;

class Node

public:

int data;

Node *next;

};

void append(Node** head, int newdata)

Node* newnode = new Node();

Node *last = *head;

newnode->data = newdata;

newnode->next = NULL;

if (*head == NULL)

*head = newnode;

return;

}
while (last->next != NULL)

last = last->next;

last->next = newnode;

return;

void printList(Node *node)

Node *p=NULL;

while (node->next != NULL)

p=node;

node = node->next;

if(p!=NULL)

cout<<p->data <<" is the second last element in the list";

else

cout<<"There is no second last element in the list";

int main()

Node* head = NULL;

int a;

do{

cin>>a;
if(a<0)

break;

append(&head,a);

}while(1);

printList(head);

return 0;

16) Linked list - nth node from the end


Write a C++ Program to find the nth node of a list from
the end. If there is no nth node print "There is no nth
node in the list", otherwise print the nth node element.

Sample Input:

-1

2
Sample Output:

Enter the nth node:

3 is the nth node element in the list


Ans

#include <iostream>

using namespace std;

class node

public:

int data;

node *next;

}*head, *tail, *temp;

void insert(int value)

node *newnode=new node();

newnode->data=value;

newnode->next=NULL;

if(head==NULL)

head=tail=newnode;

else

newnode->next=head;

head=newnode;
}

void display(int node,int count)

temp=head;

if(node>count)

cout<<"There is no nth node in the list";

else

for(int i=2;node>=i;i++)

temp=temp->next;

cout<<temp->data<<" is the nth node element in the list";

int main()

head = tail = temp = NULL;

int value,node,count=0;

do{

cin>>value;

if(value>0)

count++;
insert(value);

}while(value>0);

cout<<"Enter the nth node:\n";

cin>>node;

display(node,count);

return 0;

17) Linked list - append without duplicates


Write a C++ Program to insert the given values into a list
without duplicates.

Sample Input:

-1
Sample Output:

3
Ans #include <iostream>

using namespace std;

class Node

public:

int data;

Node *next;

};

void append(Node** head, int newdata)

Node* newnode = new Node();

Node *last = *head;

newnode->data = newdata;

newnode->next = NULL;

if (*head == NULL)

*head = newnode;
return;

while (last->next != NULL)

last = last->next;

last->next = newnode;

return;

void check(Node** head, int newdata) {

Node *node = *head;

while (node != NULL)

if(node->data==newdata)

return;

node = node->next;

append(head,newdata);

void printList(Node *node)

while (node != NULL)

cout<<node->data<<endl;

node = node->next;

}
int main()

Node* head = NULL;

int a;

do{

cin>>a;

if(a<0)

break;

check(&head,a);

}while(1);

printList(head);

return 0;

18) Linked list - count no of nodes in linked


list
Write a C++ Program to count the number of nodes in a
list.

Sample Input:

2
3

-1

Sample Output:

There are 12 values present in the list and the values are

123456789012
Ans

#include <iostream>

using namespace std;


class Node

public:

int data;

Node *next;

};

void append(Node** head, int newdata)

Node* newnode = new Node();

Node *last = *head;

newnode->data = newdata;

newnode->next = NULL;

if (*head == NULL)

*head = newnode;

return;

while (last->next != NULL)

last = last->next;

last->next = newnode;

return;

void printList(Node *node)

{
while (node != NULL)

cout<<node->data<<" ";

node = node->next;

int main()

Node* head = NULL;

int a,i=0;

do{

cin>>a;

if(a<0)

break;

append(&head,a);

i++;

}while(1);

cout<<"There are "<<i<<" values present in the list and the values are"<<endl;

printList(head);

return 0;

19) Linked list - find maximum element in


linked list
Write a C++ Program to find the maximum number in a
list.

Sample Input:

-1

Sample Output:

7
Ans

#include <iostream>

using namespace std;

class Node

public:
int data;

Node *next;

};

void append(Node** head, int newdata)

Node* newnode = new Node();

Node *last = *head;

newnode->data = newdata;

newnode->next = NULL;

if (*head == NULL)

*head = newnode;

return;

while (last->next != NULL)

last = last->next;

last->next = newnode;

return;

void printList(Node *node)

int m=0;

if(node!=NULL){

while (node != NULL)


{

if(node->data>m)

m=node->data;

node = node->next;

cout<<m;

int main()

Node* head = NULL;

int a;

do{

cin>>a;

if(a<0)

break;

append(&head,a);

}while(1);

printList(head);

return 0;

}
20) Linked list - Maximum of sum of two
numbers
Write a C++ Program to to find the maximum of sum of
two numbers in a list.

Sample Input:

-1

Sample Output:

7
Ans

#include <iostream>

using namespace std;

class node

public:
int data;

node *next;

}*head, *tail, *temp;

void insert(int value)

node *newnode=new node();

newnode->data=value;

if(head==NULL)

head=tail=newnode;

else

tail->next=newnode;

tail=newnode;

tail->next=NULL;

void display()

int max1=0, max2;

temp=head;

while(temp!=NULL)

if(max1<temp->data)
{

max2=max1;

max1=temp->data;

else if(max2<temp->data)

max2=temp->data;

temp=temp->next;

cout<<max1+max2;

int main()

head=tail=temp=NULL;

int value;

do{

cin>>value;

if(value>0)

insert(value);

}while(value>0);

display();

return 0;

21) Linked list - Delete second last


Write a C++ Program to delete the second last element
from the list. If deletion of second last element is not
possible, print "Deletion of second last element is not
possible", Otherwise print the list after deleting the
second last element.

Sample Input:

-1

Sample Output:

Before deleting:

1234

After deleting:

124
Ans

#include <iostream>
using namespace std;

struct node

int data;

struct node *next;

} *head, *tail;

void insert(int value)

struct node *newnode=(struct node*)malloc(sizeof(struct node));

newnode->data=value;

newnode->next=NULL;

if(head==NULL)

head=tail=newnode;

else

tail->next=newnode;

tail=newnode;

void display()

struct node*temp=head;
while(temp!=NULL)

cout<<temp->data<<" ";

temp=temp->next;

void deletion()

struct node* pre=NULL;

struct node* temp=head;

if(head->next==tail)

head=tail;

free(temp);

else

while(temp->next!=tail)

pre=temp;

temp=temp->next;

pre->next=tail;

free(temp);
}

int main()

head=NULL;

int value;

do{

cin>>value;

if(value>-1)

insert(value);

}while(value>-1);

if(head==tail)

cout<<"Deletion of second last element is not possible";

else

cout<<"Before deleting:\n";

display();

cout<<"\nAfter deleting:\n";

deletion();

display();

return 0;
}

STACK QNS

1) Stack using array - Insertion


Write a C++ program to implement a stack using array. if
there is no value, print "Stack is empty" and if top is
greater than the given n value, print "Stack Overflow".

Sample Input:

12345

Sample Output:

Stack elements are:

54321

Ans
#include<iostream>
#include<stdlib.h>
using namespace std;
int stack[100];
int top=-1;
void push(int ele,int n);
void traverse();
int main()
{
int n,i,ele;
cin>>n;
for(i=0;i<n;i++)
{
cin>>ele;
push(ele,n);
}
traverse();
return 0;
}
void push(int ele,int n)
{
if(top == n-1)
{
cout<<"Stack is Overflow"<<endl;
}
else
{
top++;
stack[top]=ele;
}
}
void traverse()
{
if(top == -1)
{
cout<<"Stack is empty"<<endl;
}
else
{
cout<<"Stack elements are:"<<endl;
for(int i=top;i>=0;i--)
{
cout<<stack[i]<<" ";
}
}
}

2) Stack using array - Deletion


Write a C++ program to delete the element from the
stack. If there is no value, print "Stack is empty and if
there are more than n value print "Stack overflow" and
while deleting, if top is -1 print "Stack underflow".

Sample Input:

123

Sample Output:

Before pop starts:


Stack elements are:

321

Enter the number of values to be popped:

After pop starts:

pop number: 1

The popped element is 3

Stack elements are:

21

pop number: 2

The popped element is 2

Stack elements are:

pop number: 3

The popped element is 1

Stack is empty

pop number: 4

Stack Underflow
Ans
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
if(n==0){
cout<<"Stack is empty";
return 0;
}
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int p;
cin>>p;
cout<<"Before pop starts:\nStack elements are:"<<endl;
for (int i=n-1;i>=0;i--)
cout<<a[i]<<" ";
cout<<endl<<"Enter the number of values to be popped:\
nAfter pop starts:";
n=n-1;
for(int i=1;i<=p;i++){
cout<<endl<<"pop number: "<<i<<endl;
if(n<=-1){
cout<<"Stack Underflow";
return 0;
}
cout<<"The popped element is "<<a[n--]<<endl;
if(n==-1){
cout<<"Stack is empty";
continue;
}

cout<<"Stack elements are:"<<endl;


for (int i=n;i>=0;i--)
cout<<a[i]<<" ";
}
return 0;
}

3) Stack using linked list - Insertion


Write a C++ Program to implement stack using linked list.

Sample Input:

-1

Sample Output:

4321

Ans
#include<iostream>
#include<stdlib.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
Node *root = NULL;
void append(int d)
{
Node* newnode = new Node();
newnode->data = d;
newnode->next = root;
root = newnode;
//return;
}
void display()
{
Node *temp = root;
while (temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
int main()
{
int d;
do{
cin>>d;
if(d<0)
break;
append(d);
}while(1);
display();
return 0;
}

4) Stack using linked list - Deletion


Write a C++ program to delete the elements from the
stack using linked list implementation.

Sample Input:

-1

Sample Output:

Before deleting:

4321

Deleted element is 4

Deleted element is 3

Deleted element is 2
Deleted element is 1

Stack is empty

Ans
#include<iostream>
#include<stdlib.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
Node *root = NULL;
void append(int d)
{
Node* newnode = new Node();
newnode->data = d;
newnode->next = root;
root = newnode;
}
void display()
{
Node *temp = root;
while (temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
int delet()
{
if(root == NULL)
return 0;
cout<<endl<<"Deleted element is "<<root->data;
root = root->next;
return 1;
}
int main()
{
int d;
do{
cin>>d;
if(d<0)
break;
append(d);
}while(1);
cout<<"Before deleting:"<<endl;
display();
while(delet());
{
}
cout<<endl<<"Stack is empty";
return 0;
}
5) Find top most element in stack
Write a C++ program to find top most element in stack. If
the first value of the stack is greater than second one,
then insert the greatest one to the new stack.

Sample Input:

-1

Sample Output:

Before maximizing:

512

After maximizing:

225

Ans
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};

void append(Node **head,int data)


{
Node *newnode=new Node();
newnode->data = data;
newnode -> next =*head;
*head = newnode;
}

void printList(Node *head)


{
while(head != NULL)
{
cout<<head->data<<" ";
head = head -> next;
}
}

void maxi(Node *head ,Node **h)


{
while(head != NULL)
{
if(head -> next != NULL && head -> next -> data > head ->
data)
append(h,head->next->data);
else
append(h,head -> data);
head = head -> next;
}
}
int main()
{
Node *head = NULL,*h=NULL;
int d;
do{
cin>>d;
if(d>0)
append(&head,d);
}while(d>0);
cout<<"Before maximizing:"<<endl;
printList(head);
cout<<endl<<"After maximizing:"<<endl;
maxi(head,&h);
printList(h);
return 0;
}

6) Find the size of stack


Write a C++ program to find the size of stack.

Sample Input:

-1

Sample Output:

24

Ans
#include<iostream>
#include<cstdlib>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
void insert(Node **headadd,int data)
{
Node *temp, *newnode;
temp = *headadd;
newnode = new Node();
newnode->data = data;
newnode->next = NULL;
if(*headadd == NULL)
*headadd = newnode;
else
{
*headadd = newnode;
newnode->next = temp;
}
}
int main()
{
Node *head = NULL;
int data,count=0;
do
{
cin>>data;
if(data>0)
{
count++;
insert(&head,data);
}
}while(data>0);
cout<<sizeof(head)*count;
return 0;
}

7) implement two stack using array


Write a c++ program to implement two stacks using
single array implementation.
Sample Input:

Sample Output:

Enter the value to be pushed in stack 1:

Enter the value to be pushed in stack 2:

Enter the value to be pushed in stack 1:

Enter the value to be pushed in stack 2:

Stack 1:

31

Stack 2:

42

Ans
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n];
int t1=-1,t2=n;
for(int i=0;i<(n+1)/2;i++){
cout<<"Enter the value to be pushed in stack 1:"<<endl;
cin>>a[++t1];
cout<<"Enter the value to be pushed in stack 2:"<<endl;
if(t2>t1+1)
cin>>a[--t2];
}
cout<<"Stack 1:"<<endl;
for(int i=t1;i>=0;i--)
cout<<a[i]<<" ";
cout<<endl<<"Stack 2:"<<endl;
for(int i=t2;i<n;i++)
cout<<a[i]<<" ";
//type your code
return 0;
}

8) reverse a string using stack


Write a C++ Program to reverse the given string using
stack.

Sample Input:

face

Sample Output:

ecaf

Ans
#include<iostream>
#include<stdlib.h>
using namespace std;
class Node
{
public:
char data;
Node *next;
};
Node *root = NULL;
void append(int d)
{
Node* newnode = new Node();
newnode->data = d;
newnode->next = root;
root = newnode;
}
void printList()
{
Node *temp = root;
while ( temp != NULL)
{
cout<<temp->data;
temp = temp->next;
}
}

int main()
{
string a;
cin>>a;
int i=0;
do{
if(a[i]=='\0')
break;
append(a[i]);
i++;
}while(1);
printList();
return 0;
}

9) Balancing parenthesis
Write a C++ Program to check whether the given
parenthesis is balanced or not.

Sample Input:

{()}[]

Sample Output:

Balanced

Ans
#include<iostream>
#include<stdlib.h>
using namespace std;
class Node
{
public:
char data;
Node *next;
};
Node *root = NULL;
void append(int d)
{
Node* newnode = new Node();
newnode->data = d;
newnode->next = root;
root = newnode;
}
int main()
{
int i=0;
string a;
cin>>a;
do{
if(a[i]=='\0')
break;
if((root!=NULL)&&(a[i]==')'&&root->data=='(')||
(a[i]=='}'&&root->data=='{')||(a[i]==']'&&root->data=='['))
root = root->next;
else
append(a[i]);
i++;
}while(1);
if(root == NULL)
cout<<"Balanced";
else
cout<<"Not Balanced";
return 0;
}
Complete stack expression evaluation qn
QUEUE QNS

1) Queue using array


Write a C++ program to implement Queue using array.

Sample Input:

Sample Output:

Insert the element in queue :

Insert the element in queue :

Insert the element in queue :

Insert the element in queue :

Insert the element in queue :

Queue elements are :

12345
Ans:
#include<iostream>
#include<stdlib.h>
using namespace std;
int queue[100];
void insert(int ele,int n);
void display();
int rear = - 1;
int front = - 1;
int main()
{
int n,i,ele;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Insert the element in queue :\n";
cin>>ele;
insert(ele,n);
}
display();
return 0;
}
void insert(int ele,int n)
{
if(rear == n - 1)
{
cout<<"Queue is Full";
}
else if(front == -1 && rear == -1)
{
front++;
rear++;
queue[rear]=ele;
}
else
{
rear++;
queue[rear] = ele;
}
}
void display()
{
int i;
if((front == - 1 && rear == -1) ||(front>rear))
cout<<"\nQueue is empty\n";
else
{
cout<<"Queue elements are :\n";
for(i = front; i <= rear; i++)
cout<<queue[i]<<" ";
}
}

2) Queue using linked list


Write a C++ program to implement queue using linked
list.

Sample Input:

-1

Sample Output:

1234
Ans

#include<iostream>

#include<stdlib.h>

using namespace std;

class Node{

public:

int data;

Node *next;

};
void append(Node **headadd,int data);

void display(Node *head);

int main()

Node *head=NULL;

int data;

do

cin>>data;

if(data>0)

append(&head,data);

}while(data>0);

display(head);

void append(Node **headadd,int data)

Node *temp,*newnode;

temp=*headadd;

newnode=(Node*)malloc(sizeof(Node));

newnode->data=data;

newnode->next=NULL;

if(*headadd==NULL)

*headadd=newnode;

else{
while(temp->next!=NULL){

temp=temp->next;

temp->next=newnode;

void display(Node *head)

while(head!=NULL)

cout<<head->data<<" ";

head = head->next;

3) implementation of queue using stacks


Write a C++ program to implement queue using stack.

Sample Input:

12345

Sample Output:
Enter the size of stack:

Enter the stack values:

Queue elements are:

12345
Ans

#include<iostream>

#include<stdlib.h>

using namespace std;

class Node

public:

int data;

Node *next;

};

Node *head = NULL;

void append(int data)

Node* newnode = new Node();

newnode->data = data;

newnode->next = head;

head = newnode;

}
void display()

cout<<"Queue elements are:"<<endl;

while(head != NULL)

cout<<head -> data<<" ";

head = head -> next;

void reverse()

Node* current = head;

Node *prev = NULL;

Node *temp = NULL;

while (current != NULL)

temp = current->next;

current->next = prev;

prev = current;

current = temp;

head = prev;

}
int main()

int n,data;

cout<<"Enter the size of stack:"<<endl;

cin>>n;

cout<<"Enter the stack values:"<<endl;

for(int i=0;i<n;i++)

cin>>data;

if(data>0)

append(data);

reverse();

display();

return 0;

4) Priority queue
Write a C++ program to implement the priority queue.

Sample Input:

62435
Sample Output:

Enter the number of values to be inserted:

Enter the values to be inserted in priority queue:

The priority queue elements are:

65432
Ans

#include<iostream>

#include<stdlib.h>

using namespace std;

class Node{

public:

int data;

Node *next;

};

void append(Node **headadd,int data);

void display(Node *head);

int main()

Node *head=NULL;

int data,n,c=0;

cout<<"Enter the number of values to be inserted:"<<endl;

cin>>n;

cout<<"Enter the values to be inserted in priority queue:"<<endl;


while(c<n)

cin>>data;

c++;

if(data>0)

append(&head,data);

display(head);

void append(Node **headadd,int data)

Node *temp;

if(*headadd==NULL||data>(*headadd)->data)

temp=(Node*)malloc(sizeof(Node));

temp->data=data;

temp->next=*headadd;

*headadd=temp;

else

temp=*headadd;

while(temp!=NULL)

{
if(temp->data>=data &&(temp->next==NULL || temp->next->data<data))

Node*temp1=(Node*)malloc(sizeof(Node));

temp1->data=data;

temp1->next=temp->next;

temp->next=temp1;

return;

temp=temp->next;

void display(Node *head)

cout<<"The priority queue elements are:"<<endl;

while(head!=NULL)

cout<<head->data<<" ";

head = head->next;

5) Reverse a queue
Write a C++ program to display the queue in reverse
order. If queue doesn't have a value print "Queue is
empty".

Sample Input:

-1

Sample Output:

Before reversing:

1234

After reversing:

4321
Ans

#include<iostream>

#include<stdlib.h>

using namespace std;


class Node

public:

int data;

Node *next;

};

void append(int d);

void display();

void reverse();

Node *head = NULL;

int main()

int d;

do{

cin>>d;

if(d>0)

append(d);

else

break;

}while(d>0);
if(head == NULL)

cout<<"Queue is empty"<<endl;

else

cout<<"Before reversing:"<<endl;

display();

reverse();

cout<<"After reversing:"<<endl;

display();

return 0;

void append(int d)

Node *newnode;

newnode = (Node*)malloc(sizeof(Node));

newnode->data = d;

newnode->next = NULL;

if(head == NULL)

head = newnode;

else
{

Node*temp;

temp = head;

while(temp->next != NULL)

temp=temp->next;

temp->next = newnode;

void display()

Node *temp;

temp = head;

while(temp!= NULL)

cout<<temp->data<<" ";

temp=temp->next;

cout<<endl;

}
void reverse()

Node* current = head;

Node *prev = NULL;

Node *temp = NULL;

while (current != NULL)

temp = current->next;

current->next = prev;

prev = current;

current = temp;

head = prev;

TREE QUESTIONS
1) Implementation of binary search tree
Write a C++ Program to implement binary search tree.

Sample Input:

2
1

10

-1

Sample Output:

Tree values are:

1 2 3 4 6 7 8 9 10

Ans
#include<iostream>

#include<cstdlib>

using namespace std;

struct btree

int data;

struct btree * left;


struct btree * right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));

temp -> data = n;

temp->right = temp->left = NULL;

void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){

insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

void inorder(struct btree * t)


{

if(root == NULL){

cout<<"No element";

return;

if(t->left!=NULL)

inorder(t->left);

cout<< t->data<<" ";

if(t->right != NULL)

inorder(t->right);

void preorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

}
cout<< t->data<<" ";

if(t->left!=NULL)

preorder(t->left);

if(t->right != NULL)

preorder(t->right);

void postorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

if(t->left!=NULL)

postorder(t->left);

if(t->right != NULL)

postorder(t->right);

cout<< t->data<<" ";

int height(struct btree *t)

if(t == NULL)

return 0;

}
else

int lheight = height(t->left);

int rheight = height(t->right);

if(lheight>rheight)

return lheight+1;

else

return rheight+1;

void printgivenlevel(struct btree *t, int i)

if(t == NULL)

return;

if(i == 1)

cout<< t->data << " ";

else if(i > 1)


{

printgivenlevel(t->left, i-1);

printgivenlevel(t->right, i-1);

void levelorder(struct btree * t)

int h = height(t);

int i;

for(i = 1; i<=h ; i++)

printgivenlevel(t , i);

int main()

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

{
create();

if(root == NULL)

root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

create(input);

if(root == NULL)

root = temp;

else

insert(root);

cout<<"Tree values are:"<<endl;

inorder(root);

2) search an element in BST


Write a C++ Program to search an given element from
binary search tree.

Sample Input:

10

15

-1

10

Sample Output:
Enter the element to be searched:

10 is present in the BST


Ans

#include<iostream>

#include<cstdlib>

using namespace std;

struct btree

int data;

struct btree * left;

struct btree * right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));

temp -> data = n;

temp->right = temp->left = NULL;

void insert(struct btree * t)

{
if(t->data < temp -> data && t-> right != NULL){

insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

void inorder(struct btree * t)

if(root == NULL){

cout<<"No element";

return;

if(t->left!=NULL)

{
inorder(t->left);

cout<< t->data<<" ";

if(t->right != NULL)

inorder(t->right);

void preorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

cout<< t->data<<" ";

if(t->left!=NULL)

preorder(t->left);

if(t->right != NULL)

preorder(t->right);
}

void postorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

if(t->left!=NULL)

postorder(t->left);

if(t->right != NULL)

postorder(t->right);

cout<< t->data<<" ";

int height(struct btree *t)

if(t == NULL)

return 0;

else

{
int lheight = height(t->left);

int rheight = height(t->right);

if(lheight>rheight)

return lheight+1;

else

return rheight+1;

void printgivenlevel(struct btree *t, int i)

if(t == NULL)

return;

if(i == 1)

cout<< t->data << " ";

}
else if(i > 1)

printgivenlevel(t->left, i-1);

printgivenlevel(t->right, i-1);

bool ifNodeExists(struct btree* btree, int key)

if (btree == NULL)

return false;

if (btree->data == key)

return true;

/* then recur on left sutree */

bool res1 = ifNodeExists(btree->left, key);

if(res1) return true; // node found, no need to look further

/* node is not found in left, so recur on right subtree */


bool res2 = ifNodeExists(btree->right, key);

return res2;

void levelorder(struct btree * t)

int h = height(t);

int i;

for(i = 1; i<=h ; i++)

printgivenlevel(t , i);

int main()

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();
if(root == NULL)

root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

create(input);

if(root == NULL)

root = temp;

else

insert(root);

int key;

cout<<"Enter the element to be searched:"<<endl;

cin>>key;
if (ifNodeExists(root, key))

cout <<key <<" is present in the BST";

else

cout <<key <<" is not present in the BST";

3) Inorder traversal
Write a C++ program to display the elements of tree in
inorder traversal.

Sample Input:

10

7
8

-1

Sample Output:

Inorder Traversal:

1 2 3 4 6 7 8 9 10
Ans

#include<iostream>

#include<cstdlib>

using namespace std;

struct btree

int data;

struct btree * left;

struct btree * right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));


temp -> data = n;

temp->right = temp->left = NULL;

void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){

insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

void inorder(struct btree * t)

if(root == NULL){

cout<<"No element";
return;

if(t->left!=NULL)

inorder(t->left);

cout<< t->data<<" ";

if(t->right != NULL)

inorder(t->right);

void preorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

}
cout<< t->data<<" ";

if(t->left!=NULL)

preorder(t->left);

if(t->right != NULL)

preorder(t->right);

void postorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

if(t->left!=NULL)

postorder(t->left);

if(t->right != NULL)

postorder(t->right);

cout<< t->data<<" ";

int height(struct btree *t)

if(t == NULL)
{

return 0;

else

int lheight = height(t->left);

int rheight = height(t->right);

if(lheight>rheight)

return lheight+1;

else

return rheight+1;

void printgivenlevel(struct btree *t, int i)

if(t == NULL)

return;
}

if(i == 1)

cout<< t->data << " ";

else if(i > 1)

printgivenlevel(t->left, i-1);

printgivenlevel(t->right, i-1);

void levelorder(struct btree * t)

int h = height(t);

int i;

for(i = 1; i<=h ; i++)

printgivenlevel(t , i);

}
}

int main()

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();

if(root == NULL)

root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

{
create(input);

if(root == NULL)

root = temp;

else

insert(root);

cout<<"Inorder Traversal:"<<endl;

inorder(root);

4) Preorder traversal
Write a C++ program to display the elements of tree in
preorder traversal.

Sample Input:

4
2

-1

Sample Output:

Preorder Traversal:

63124
Ans

#include<iostream>

#include<cstdlib>

using namespace std;

struct btree

int data;

struct btree * left;

struct btree * right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));


temp -> data = n;

temp->right = temp->left = NULL;

void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){

insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

void inorder(struct btree * t)

if(root == NULL){

cout<<"No element";
return;

if(t->left!=NULL)

inorder(t->left);

cout<< t->data<<" ";

if(t->right != NULL)

inorder(t->right);

void preorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

}
cout<< t->data<<" ";

if(t->left!=NULL)

preorder(t->left);

if(t->right != NULL)

preorder(t->right);

void postorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

if(t->left!=NULL)

postorder(t->left);

if(t->right != NULL)

postorder(t->right);

cout<< t->data<<" ";

int height(struct btree *t)

if(t == NULL)
{

return 0;

else

int lheight = height(t->left);

int rheight = height(t->right);

if(lheight>rheight)

return lheight+1;

else

return rheight+1;

void printgivenlevel(struct btree *t, int i)

if(t == NULL)

return;
}

if(i == 1)

cout<< t->data << " ";

else if(i > 1)

printgivenlevel(t->left, i-1);

printgivenlevel(t->right, i-1);

void levelorder(struct btree * t)

int h = height(t);

int i;

for(i = 1; i<=h ; i++)

printgivenlevel(t , i);

}
}

int main()

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();

if(root == NULL)

root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

{
create(input);

if(root == NULL)

root = temp;

else

insert(root);

cout<<"Preorder Traversal:"<<endl;

preorder(root);

5) Post order traversal


Write a C++ program to display the elements of tree in
post order traversal.

Sample Input:

4
2

-1

Sample Output:

Post order Traversal:

21436
Ans

#include<iostream>

#include<cstdlib>

using namespace std;

struct btree

int data;

struct btree * left;

struct btree * right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));


temp -> data = n;

temp->right = temp->left = NULL;

void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){

insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

void inorder(struct btree * t)

if(root == NULL){

cout<<"No element";
return;

if(t->left!=NULL)

inorder(t->left);

cout<< t->data<<" ";

if(t->right != NULL)

inorder(t->right);

void preorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

}
cout<< t->data<<" ";

if(t->left!=NULL)

preorder(t->left);

if(t->right != NULL)

preorder(t->right);

void postorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

if(t->left!=NULL)

postorder(t->left);

if(t->right != NULL)

postorder(t->right);

cout<< t->data<<" ";

int height(struct btree *t)

if(t == NULL)
{

return 0;

else

int lheight = height(t->left);

int rheight = height(t->right);

if(lheight>rheight)

return lheight+1;

else

return rheight+1;

void printgivenlevel(struct btree *t, int i)

if(t == NULL)

return;
}

if(i == 1)

cout<< t->data << " ";

else if(i > 1)

printgivenlevel(t->left, i-1);

printgivenlevel(t->right, i-1);

void levelorder(struct btree * t)

int h = height(t);

int i;

for(i = 1; i<=h ; i++)

printgivenlevel(t , i);

}
}

int main()

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();

if(root == NULL)

root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

{
create(input);

if(root == NULL)

root = temp;

else

insert(root);

cout<<"Post order Traversal:"<<endl;

postorder(root);

6) Level order traversal


Write a C++ program to display the elements of tree in
level order traversal.

Sample Input:

4
2

-1

Sample Output:

Level order Traversal:

63142
Ans

#include<iostream>

#include<cstdlib>

using namespace std;

struct btree

int data;

struct btree * left;

struct btree * right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));


temp -> data = n;

temp->right = temp->left = NULL;

void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){

insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

void inorder(struct btree * t)

if(root == NULL){

cout<<"No element";
return;

if(t->left!=NULL)

inorder(t->left);

cout<< t->data<<" ";

if(t->right != NULL)

inorder(t->right);

void preorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

}
cout<< t->data<<" ";

if(t->left!=NULL)

preorder(t->left);

if(t->right != NULL)

preorder(t->right);

void postorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

if(t->left!=NULL)

postorder(t->left);

if(t->right != NULL)

postorder(t->right);

cout<< t->data<<" ";

int height(struct btree *t)

if(t == NULL)
{

return 0;

else

int lheight = height(t->left);

int rheight = height(t->right);

if(lheight>rheight)

return lheight+1;

else

return rheight+1;

void printgivenlevel(struct btree *t, int i)

if(t == NULL)

return;
}

if(i == 1)

cout<< t->data << " ";

else if(i > 1)

printgivenlevel(t->left, i-1);

printgivenlevel(t->right, i-1);

void levelorder(struct btree * t)

int h = height(t);

int i;

for(i = 1; i<=h ; i++)

printgivenlevel(t , i);

}
}

int main()

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();

if(root == NULL)

root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

{
create(input);

if(root == NULL)

root = temp;

else

insert(root);

cout<<"Level order Traversal:"<<endl;

levelorder(root);

7) Binary tree - Height


Write a C++ Program to find the depth or height of the
tree.

Sample Input:

2
-1

Sample Output:

Height of the tree is 4


Ans

#include<iostream>

#include<cstdlib>

using namespace std;

struct btree

int data;

struct btree * left;

struct btree * right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));

temp -> data = n;

temp->right = temp->left = NULL;

}
void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){

insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

void inorder(struct btree * t)

if(root == NULL){

cout<<"No element";

return;

}
if(t->left!=NULL)

inorder(t->left);

cout<< t->data<<" ";

if(t->right != NULL)

inorder(t->right);

void preorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

cout<< t->data<<" ";

if(t->left!=NULL)

preorder(t->left);
if(t->right != NULL)

preorder(t->right);

void postorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

if(t->left!=NULL)

postorder(t->left);

if(t->right != NULL)

postorder(t->right);

cout<< t->data<<" ";

int height(struct btree *t)

if(t == NULL)

return 0;

}
else

int lheight = height(t->left);

int rheight = height(t->right);

if(lheight>rheight)

return lheight+1;

else

return rheight+1;

void printgivenlevel(struct btree *t, int i)

if(t == NULL)

return;

if(i == 1)
{

cout<< t->data << " ";

else if(i > 1)

printgivenlevel(t->left, i-1);

printgivenlevel(t->right, i-1);

void levelorder(struct btree * t)

int h = height(t);

int i;

for(i = 1; i<=h ; i++)

printgivenlevel(t , i);

}
int main()

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();

if(root == NULL)

root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

create(input);

if(root == NULL)
root = temp;

else

insert(root);

cout<<"Height of the tree is ";

cout<<height(root);

8) Binary tree - Find Max


Write a C++ program to find the maximum element in
the tree.

Sample Input:

-1
Sample Output:

Maximum element is 6
Ans
#include <bits/stdc++.h>

using namespace std;

// A Tree node

struct btree {

int data;

struct btree *left, *right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));

temp -> data = n;

temp->right = temp->left = NULL;

void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){

insert(t->right);
}

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

// Utility function to create a new node

// Function to print a maximum and minimum element

// in a tree without recursion without stack

void printMinMax(btree* root)

if (root == NULL)

cout << "Tree is empty";

return;

}
btree* current = root;

btree* pre;

// Max variable for storing maximum value

int max_value = INT_MIN;

// Min variable for storing minimum value

int min_value = INT_MAX;

while (current != NULL)

// If left child does nor exists

if (current->left == NULL)

max_value = max(max_value, current->data);

min_value = min(min_value, current->data);

current = current->right;

else

{
// Find the inorder predecessor of current

pre = current->left;

while (pre->right != NULL && pre->right !=

current)

pre = pre->right;

// Make current as the right child

// of its inorder predecessor

if (pre->right == NULL)

pre->right = current;

current = current->left;

// Revert the changes made in the 'if' part to

// restore the original tree i.e., fix the

// right child of predecessor

else

pre->right = NULL;

max_value = max(max_value, current->data);

min_value = min(min_value, current->data);


current = current->right;

} // End of if condition pre->right == NULL

} // End of if condition current->left == NULL

} // End of while

// Finally print max and min value

cout << "Maximum element is " << max_value << endl;

//cout << "Min Value is : " << min_value << endl;

// Driver Code

int main()

/* 15

/\

19 11

/\

25 5

/\/\

17 3 23 24

Let us create Binary Tree as shown


above */

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();

if(root == NULL)

root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

create(input);

if(root == NULL)

root = temp;

else
insert(root);

// Function call for printing a max

// and min element in a tree

printMinMax(root);

return 0;

9) Binary Search tree - Sum


Write a C++ Program to sum all the elements in the tree.

Sample Input:

-1
Sample Output:

Sum of all nodes are 16


Ans

#include <bits/stdc++.h>

using namespace std;

struct btree {

int data;

struct btree *left, *right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));

temp -> data = n;

temp->right = temp->left = NULL;

void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){

insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){
t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

int addBT(btree* root)

if (root == NULL)

return 0;

return (root->data + addBT(root->left) + addBT(root->right));

int main()

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();

if(root == NULL)
root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

create(input);

if(root == NULL)

root = temp;

else

insert(root);

cout<<"Sum of all nodes are "<<addBT(root);

return 0;

10) Binary Search tree - Sum of leaf nodes


Write a C++ program to sum all the leaf node elements in
a tree.

Sample Input:

-1

Sample Output:

Sum of all leaf nodes are 6


Ans

#include <bits/stdc++.h>

using namespace std;

struct btree {

int data;

struct btree *left, *right;

};
struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));

temp -> data = n;

temp->right = temp->left = NULL;

void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){

insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

void leafSum(btree *root, int& sum){

if (!root)
return;

// add root data to sum if

// root is a leaf node

if (!root->left && !root->right)

sum += root->data;

// propagate recursively in left

// and right subtree

leafSum(root->left, sum);

leafSum(root->right, sum);

int main()

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();

if(root == NULL)

root = temp;

else
insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

create(input);

if(root == NULL)

root = temp;

else

insert(root);

int sum = 0;

leafSum(root, sum);

cout <<"Sum of all leaf nodes are "<< sum << endl;

return 0;

11) Binary Search tree - Common ancestor


Write a C++ program to find the common ancestor of
given two numbers in a tree. Let T be a rooted tree. The
lowest common ancestor between two nodes n1 and n2
is defined as the lowest node in T that has both n1 and
n2 as descendants (where we allow a node to be a
descendant of itself). The LCA of n1 and n2 in T is the
shared ancestor of n1 and n2 that is located farthest
from the root. Computation of lowest common ancestors
may be useful, for instance, as part of a procedure for
determining the distance between pairs of nodes in a
tree: the distance from n1 to n2 can be computed as the
distance from the root to n1, plus the distance from the
root to n2, minus twice the distance from the root to
their lowest common ancestor.

Sample Input:

-1
36

Sample Output:

Enter the value of n1 and n2:

Common ancestor of 3 and 6 is 6


Ans

#include <bits/stdc++.h>

using namespace std;

struct btree {

int data;

struct btree *left, *right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));

temp -> data = n;

temp->right = temp->left = NULL;

void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){


insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

struct btree *lca(struct btree* root, int n1, int n2)

while (root != NULL)

// If both n1 and n2 are smaller than root, then LCA lies in left

if (root->data > n1 && root->data > n2)

root = root->left;

// If both n1 and n2 are greater than root, then LCA lies in right

else if (root->data < n1 && root->data < n2)

root = root->right;
else break;

return root;

int main()

int n,input,n1,n2;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();

if(root == NULL)

root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

{
create(input);

if(root == NULL)

root = temp;

else

insert(root);

cout<<"Enter the value of n1 and n2:"<<endl;

cin>>n1;

cin>>n2;

struct btree *t = lca(root, n1, n2);

cout<<"Common ancestor of "<<n1<<" and "<<n2<<" is "<<t->data;

return 0;

12) Binary tree - Nodes count


Write a c++ program to count the number of nodes in a
tree.

Sample Input:

3
4

-1

Sample Output:

Number of nodes are 3


Ans

#include<iostream>

#include<stdlib.h>

using namespace std;

class node

public:

int data;

node* left;

node *right;

};

node *root=NULL;

void insert(int a){

node *newnode;

node *temp;

temp = root;
newnode = new node;

newnode->data = a;

newnode->left = NULL;

newnode->right = NULL;

if(temp == NULL)

root = newnode;

else

while(1)

while(temp->data > newnode->data && temp->left!=NULL)

temp = temp->left;

if(temp->data > newnode->data && temp->left== NULL)

temp->left=newnode;

break;

while(temp->data < newnode->data && temp->right!= NULL)

temp = temp->right;

if(temp->data < newnode->data && temp->right == NULL)

temp->right = newnode;
break;

int display(node *root)

node *temp = root;

if(temp == NULL)

//cout<<"No elements in the tree"<<endl;

return 0;

else

display(temp->left);

//cout<<temp->data<<" ";

display(temp->right);

int main()

int a,i=1;
do{

cin>>a;

if(a<0)

break;

insert(a);

i++;

}while(1);

cout<<"Number of nodes are "<<i/2;

return 0;

13) Binary Search tree - kth smallest


element
Write a C++ program to find the kth smallest element of
a tree.

Sample Input:

50

30

20

40

70
60

80

-1

Sample Output:

Enter the kth value:

Smallest kth value 50


Ans

#include <bits/stdc++.h>

using namespace std;

struct btree {

int data;

struct btree *left, *right;

};

struct btree * root, * temp;

void create(int n)

temp = (struct btree *)malloc(sizeof(struct btree ));

temp -> data = n;

temp->right = temp->left = NULL;


}

void insert(struct btree * t)

if(t->data < temp -> data && t-> right != NULL){

insert(t->right);

else if(t->data < temp -> data && t-> right == NULL){

t->right = temp;

else if(t->data > temp -> data && t-> left != NULL){

insert(t->left);

else if(t->data > temp -> data && t-> left == NULL){

t->left = temp;

btree* kthSmallest(btree* root, int& count, int k)

// base case

if (root == NULL)

return NULL;

// search in left subtree

btree* left = kthSmallest(root->left, count, k);


// if k'th smallest is found in left subtree, return it

if (left != NULL)

return left;

// if current element is k'th smallest, return it

count++;

if (count == k)

return root;

// else search in right subtree

return kthSmallest(root->right, count, k);

// Function to find k'th largest element in BST

void printKthSmallest(btree* root, int k)

// maintain index to count number of nodes processed so far

int count = 0;

btree* res = kthSmallest(root, count, k);

if (res == NULL)

cout << "There are less than k nodes in the BST";

else

cout << "Smallest kth value " << res->data;


}

int main()

int n,input;

//cin>>n;

root = 0;

/*for(int i=0;i<n;i++)

create();

if(root == NULL)

root = temp;

else

insert(root);

}*/

while(1)

cin>>input;

if(input<0)

break;

else

create(input);

if(root == NULL)

root = temp;
else

insert(root);

int k;

cin>>k;

cout<<"Enter the kth value:"<<endl;

printKthSmallest(root, k);

14) Binary tree - Compute column number


Write a C++ program to count the number of column in a
tree.

Sample Input:

6
7

-1

Sample Output:

The column wise sum values

C.No : sum

-3 : 8

-2 : 4

-1 : 11

0 : 12

1:3

2:7
Ans

#include<iostream>

#include<bits/stdc++.h>

using namespace std;


class node{

public:

int dat;

node* l;

node *r;

}*root=NULL;

void insert( node **r,int dat){

static int i=1;

node *n;

node *t=*r;

n=new node;

n->dat=dat;

n->l=NULL;

n->r=NULL;

if(*r==NULL){

*r=n;

else{

i++;

int nu[10],c=i,j=0;

while(c>1){

nu[j++]=(c%2);

c/=2;

}
for(int k=j-1;k>0;k--){

if(nu[k]==0)

t=t->l;

else

t=t->r;

if(nu[0]==0)

t->l=n;

else

t->r=n;

}}

void verticalSumUtil(node *node, int hd, map<int, int> &Map)

if (node == NULL) return;

verticalSumUtil(node->l, hd-1, Map);

Map[hd] += node->dat;

verticalSumUtil(node->r, hd+1, Map);

void verticalSum(node *root)

map < int, int> Map;

map < int, int> :: iterator it;

verticalSumUtil(root, 0, Map);
for (it = Map.begin(); it != Map.end(); ++it)

cout << (it->first) << " : "

<< it->second << endl;

int main()

int a;

do{

cin>>a;

if(a<0)

break;

insert(&root,a);

}while(1);

cout<<"The column wise sum values"<<endl;

cout<<"C.No : sum"<<endl;

verticalSum(root);

return 0;

15) Binary tree - Diameter


Write a c++ program to find the diameter of a binary tree.
Sample Input:

-1

Sample Output:

Diameter of the given binary tree is 4


Ans

#include<iostream>

#include<bits/stdc++.h>

using namespace std;

class node

public:

int dat;

node* l;

node *r;
}*root=NULL;

void insert( node **r,int dat){

static int i=1;

node *n;

node *t=*r;

n=new node;

n->dat=dat;

n->l=NULL;

n->r=NULL;

if(*r==NULL){

*r=n;

else{

i++;

int nu[10],c=i,j=0;

while(c>1){

nu[j++]=(c%2);

c/=2;

for(int k=j-1;k>0;k--){

if(nu[k]==0)

t=t->l;

else

t=t->r;
}

if(nu[0]==0)

t->l=n;

else

t->r=n;

}}

int verticalSum(node *root)

{ int a=0,b=0;

node *t=root;

while(t!=NULL){

a++;

t=t->l;

while(root!=NULL){

b++;

root=root->r;

return a+b-1;

int main()

int a;

do{
cin>>a;

if(a<0)

break;

insert(&root,a);

}while(1);

cout<<"Diameter of the given binary tree is "<<verticalSum(root);

return 0;

GRAPH PROBLEMS
1) Graph - Adjacency Matrix
Representation
Write a C++ program to implement graph using
adjacency Matrix.

Sample Input:

yes

126
237

348

249

Sample Output:

Please enter the number of nodes in the graph

Please enter the number of edges in the graph

Is the graph directed

Adjacency Matrix Representation:

0000

0000

0000

0000

Enter the start node, end node and weight of edge no 1

Enter the start node, end node and weight of edge no 2

Enter the start node, end node and weight of edge no 3

Enter the start node, end node and weight of edge no 4


Adjacency Matrix Representation:

0600

0079

0008

0000
Ans

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main()

int **graph,node,edge,f=0,sn,en,we;

char *dir;

dir=(char*)malloc(sizeof(char));

printf("Please enter the number of nodes in the graph\n");

scanf("%d",&node);

printf("Please enter the number of edges in the graph\n");

scanf("%d",&edge);

graph=(int**)malloc(sizeof(int*)*node);

for(int i=0;i<node;i++)
*(graph+i)=(int*)malloc(sizeof(int)*edge);

printf("Is the graph directed\n");

scanf("%s",dir);

if(strcmp(dir,"yes")==0)

f=1;

printf("Adjacency Matrix Representation:\n");

for(int i=0;i<node;i++){

for(int j=0;j<edge;j++)

printf("%d ",graph[i][j]);

printf("\n");}

for(int i=0;i<edge;i++){

printf("Enter the start node, end node and weight of edge no %d\n",i+1);

scanf("%d %d %d",&sn,&en,&we);

graph[sn-1][en-1]=we;

if(f==0)

graph[en-1][sn-1]=we;}

printf("Adjacency Matrix Representation:\n");

for(int i=0;i<node;i++){

for(int j=0;j<edge;j++)

printf("%d ",graph[i][j]);

printf("\n");}

return 0;
}

2) Graph - Adjacency List Representation


Write a C++ program to implement graph using
adjacency list representation.

Sample Input:

12

23

34

24

Sample Output:

Enter the number of vertices:

Enter the number of edges:

Enter the Start node and End node of edge 1

Enter the Start node and End node of edge 2


Enter the Start node and End node of edge 3

Enter the Start node and End node of edge 4

Adjacency List:

0--->

1--->2

2--->1 3 4

3--->2 4

4--->3 2

5--->
Ans

#include<iostream>

using namespace std;

#include<bits/stdc++.h>

#include<vector>

void addEdge(vector<int> adj[], int u, int v)

adj[u].push_back(v);

adj[v].push_back(u);

}
void printGraph(vector<int> adj[], int v)

cout<<"Adjacency List:\n";

for (int i = 0; i < v; i++)

cout << i<<"--->";

for ( vector<int> :: iterator x=adj[i].begin();x!=adj[i].end();x++)

cout << *x<<" ";

cout<<"\n";

int main()

cout<<"Enter the number of vertices:\nEnter the number of edges:\n";

int v,e;

cin>>v>>e;

vector<int> adj[v];

for(int i=1;i<=e;i++)

cout<<"Enter the Start node and End node of edge "<<i<<endl;

int s,e;

cin>>s>>e;
addEdge(adj,s,e);

printGraph(adj, v);

return 0;

3) Graph - Connected graph


Write a C++ program to check whether the graph is
strongly connected or not.

Sample Input:

01

12

23

30

24

42
Sample Output:

Enter the number of vertices:

Enter the number of edges:

Enter the Start node and End node of edge 1

Enter the Start node and End node of edge 2

Enter the Start node and End node of edge 3

Enter the Start node and End node of edge 4

Enter the Start node and End node of edge 5

Enter the Start node and End node of edge 6

Yes
Ans

#include <iostream>

#include <list>

#include <stack>

using namespace std;

class Graph

int V;

list<int> *adj;
void DFSUtil(int v, bool visited[]);

public:

Graph(int V) { this->V = V; adj = new list<int>[V];}

~Graph() { delete [] adj; }

void addEdge(int v, int w);

bool isSC();

Graph getTranspose();

};

void Graph::DFSUtil(int v, bool visited[])

visited[v] = true;

list<int>::iterator i;

for (i = adj[v].begin(); i != adj[v].end(); ++i)

if (!visited[*i])

DFSUtil(*i, visited);

Graph Graph::getTranspose()

Graph g(V);

for (int v = 0; v < V; v++)

list<int>::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i)

g.adj[*i].push_back(v);

return g;

void Graph::addEdge(int v, int w)

adj[v].push_back(w);

bool Graph::isSC()

bool visited[V];

for (int i = 0; i < V; i++)

visited[i] = false;

DFSUtil(0, visited);

for (int i = 0; i < V; i++)

if (visited[i] == false)

return false;

Graph gr = getTranspose();
for(int i = 0; i < V; i++)

visited[i] = false;

gr.DFSUtil(0, visited);

for (int i = 0; i < V; i++)

if (visited[i] == false)

return false;

return true;

int main()

int n,e;

cout<<"Enter the number of vertices:\nEnter the number of edges:\n";

cin>>n>>e;

Graph g1(n);

for(int i=1;i<=e;i++){

cout<<"Enter the Start node and End node of edge "<<i<<endl;

int st,ed;

cin>>st>>ed;

g1.addEdge(st, ed);

g1.isSC()? cout << "Yes\n" : cout << "No\n";

return 0;
}

You might also like