Data Structure Using CPP
Data Structure Using CPP
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:
Output Format:
Sample Input:
-1
Sample Output:
1
Ans
#include <iostream>
struct node{
int data;
}*head=NULL;
newnode->data = data;
newnode->next = NULL;
if(head==NULL){
head = newnode;
else{
temp=head;
while(temp->data!=data){
if(temp->next == NULL){
temp->next = newnode;
return;
temp = temp->next;
return;
void print(){
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;
Input Format:
Output Format:
Sample Input:
3
4
-1
Sample Output:
Ans
#include <bits/stdc++.h>
struct Node {
int data;
};
int t = 0;
while (curr) {
t = k;
join = curr;
prev = NULL;
temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
if (!newHead)
newHead = prev;
if (tail)
tail->next = prev;
tail = join;
return newHead;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* node)
node = node->next;
int main()
int data;
while(1){
cin>>data;
if(data<0){
break;
push(&head,data);
int k = data;
printList(head);
return (0);
Input Format:
Output Format:
Sample Input:
-1
10
Sample Output:
10
4
Ans
#include<iostream>
struct node
{
int data;
};
temp = * headadd;
newnode->data=data;
newnode->link=NULL;
if(*headadd==NULL)
*headadd=newnode;
else
while(temp->link!=NULL)
temp=temp->link;
temp->link=newnode;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->link;
while(temp!=NULL)
cout<<temp->data<<endl;
temp=temp->link;
temp=head;
if(1==pos)
head=newdata;
newdata->data=data;
newdata->link=temp;
}
else
for(int i=2;i<=pos;i++)
if(i==pos)
temp1=temp->link;
temp->link=newdata;
newdata->data=data;
newdata->link=temp1;
else
temp=temp->link;
}}
int main()
{
head=NULL;
int data;
do
cin>>data;
if(data>0)
append(&head,data);
while (data>0);
display(head);
int pos,value;
cin>>pos>>value;
add(pos,value);
display2(head);
return 0;
Input Format:
The input should be numbers (until user enters a
negative value)
Output Format:
Sample Input:
-1
10
Sample Output:
10
4
Ans
#include<iostream>
#include<stdlib.h>
class node
public:
int data;
node *next;
};
node *newnode;
if(root == NULL)
root = newnode;
else
node*temp;
temp = root;
temp=temp->next;
temp->next = newnode;
int i;
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);
display();
int pos,da;
cin>>pos>>da;
insert(pos,da);
display();
return 0;
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>
class Node
public:
int data;
Node *next;
};
//Node *root;
void display();
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++)
del(s);
display();
return 0;
void create(int d)
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)
else
{
while(temp!= NULL)
cout<<temp->data<<endl;
temp=temp->next;
void del(int s)
root=root->next;
free(temp);
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>
class Node
public:
int data;
Node *next;
};
//Node *root;
void display();
int del();
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())
display();
i++;
return 0;
void create(int d)
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)
else
while(temp!= NULL)
cout<<temp->data<<endl;
temp=temp->next;
}
}
int del()
Node *temp,*p;
if(root == NULL)
return 0;
root = NULL;
free(root);
return 1;
else
temp = root;
p=temp;
free(temp);
return 1;
}
}
Sample Input:
-1
Sample Output:
Before deletion:
2
3
After deletion:
5
Ans
#include <iostream>
class Node
public:
int data;
Node *next;
};
newnode->data = newdata;
newnode->next = NULL;
if(*head==NULL)
*head = newnode;
else{
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
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);
cout<<head->data<<endl;
head = head->next;
int main()
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;
Sample Input:
-1
Sample Output:
Before Reversing the list:
1
Ans
#include<iostream>
#include<stdlib.h>
class Node
public:
int data;
Node *next;
};
void append(int d);
void display();
void reverse();
int main()
int d;
do{
cin>>d;
if(d>0)
append(d);
else
break;
}while(1);
display();
reverse();
display();
return 0;
}
void append(int d)
Node *newnode;
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()
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:
1
2
4
Ans
#include <iostream>
class Node
public:
int data;
Node *next;
};
{
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;
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;
cout<<node->data<<endl;
node = node->next;
int main()
int a;
do{
cin>>a;
if(a<0)
break;
append(&head,a);
}while(1);
printList(head);
remov(&head);
printList(head);
return 0;
Sample Input:
-1
Sample Output:
4
Ans
#include<iostream>
class Node
public:
int data;
Node *next,*prev;
};
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;
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;
Sample Input:
-1
Sample Output:
Original order:
2
3
Reverse order:
1
Ans:
#include<iostream>
class Node
public:
int data;
Node *next,*prev;
};
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;
Node *p,*t,*node=*head;
p=node;
node = node->next;
t=p->prev;
p->prev=p->next;
p->next=t;
*head=p;
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;
Sample Input:
-1
Sample Output:
Before inserting:
4
Enter the value and position
After inserting:
4
Ans
#include<iostream>
class Node
public:
int data;
Node *next,*prev;
};
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;
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;
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;
cin>>p>>a;
insert(&head,p,a);
cout<<"After inserting:"<<endl;
printList(head);
return 0;
Sample Input:
-1
Sample Output:
4
Ans
#include <iostream>
class Node
public:
int data;
Node *next;
};
newnode->data = newdata;
newnode->next = *head;
if (*head == NULL)
{
*head = newnode;
newnode->next=newnode;
return;
last = last->next;
last->next = newnode;
return;
Node *a=node;
do
cout<<node->data<<endl;
node = node->next;
} while (node != a) ;
int main()
int a;
do{
cin>>a;
if(a<0)
break;
append(&head,a);
}while(1);
printList(head);
return 0;
Sample Input:
-1
Sample Output:
Enter the value to be searched:
#include <iostream>
class Node
public:
int data;
Node *next;
};
newnode->data = newdata;
newnode->next = NULL;
if (*head == NULL)
*head = newnode;
return;
last->next = newnode;
return;
if(node->data==a)
return 1;
node = node->next;
return 0;
int main()
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)
else
return 0;
Sample Input:
-1
Sample Output:
#include <iostream>
class Node
public:
int data;
Node *next;
};
newnode->data = newdata;
newnode->next = NULL;
if (*head == NULL)
*head = newnode;
return;
}
while (last->next != NULL)
last = last->next;
last->next = newnode;
return;
Node *p=NULL;
p=node;
node = node->next;
if(p!=NULL)
else
int main()
int a;
do{
cin>>a;
if(a<0)
break;
append(&head,a);
}while(1);
printList(head);
return 0;
Sample Input:
-1
2
Sample Output:
#include <iostream>
class node
public:
int data;
node *next;
newnode->data=value;
newnode->next=NULL;
if(head==NULL)
head=tail=newnode;
else
newnode->next=head;
head=newnode;
}
temp=head;
if(node>count)
else
for(int i=2;node>=i;i++)
temp=temp->next;
int main()
int value,node,count=0;
do{
cin>>value;
if(value>0)
count++;
insert(value);
}while(value>0);
cin>>node;
display(node,count);
return 0;
Sample Input:
-1
Sample Output:
3
Ans #include <iostream>
class Node
public:
int data;
Node *next;
};
newnode->data = newdata;
newnode->next = NULL;
if (*head == NULL)
*head = newnode;
return;
last = last->next;
last->next = newnode;
return;
if(node->data==newdata)
return;
node = node->next;
append(head,newdata);
cout<<node->data<<endl;
node = node->next;
}
int main()
int a;
do{
cin>>a;
if(a<0)
break;
check(&head,a);
}while(1);
printList(head);
return 0;
Sample Input:
2
3
-1
Sample Output:
There are 12 values present in the list and the values are
123456789012
Ans
#include <iostream>
public:
int data;
Node *next;
};
newnode->data = newdata;
newnode->next = NULL;
if (*head == NULL)
*head = newnode;
return;
last = last->next;
last->next = newnode;
return;
{
while (node != NULL)
cout<<node->data<<" ";
node = node->next;
int main()
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;
Sample Input:
-1
Sample Output:
7
Ans
#include <iostream>
class Node
public:
int data;
Node *next;
};
newnode->data = newdata;
newnode->next = NULL;
if (*head == NULL)
*head = newnode;
return;
last = last->next;
last->next = newnode;
return;
int m=0;
if(node!=NULL){
if(node->data>m)
m=node->data;
node = node->next;
cout<<m;
int main()
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>
class node
public:
int data;
node *next;
newnode->data=value;
if(head==NULL)
head=tail=newnode;
else
tail->next=newnode;
tail=newnode;
tail->next=NULL;
void display()
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;
Sample Input:
-1
Sample Output:
Before deleting:
1234
After deleting:
124
Ans
#include <iostream>
using namespace std;
struct node
int data;
} *head, *tail;
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()
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)
else
cout<<"Before deleting:\n";
display();
cout<<"\nAfter deleting:\n";
deletion();
display();
return 0;
}
STACK QNS
Sample Input:
12345
Sample Output:
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]<<" ";
}
}
}
Sample Input:
123
Sample Output:
321
pop number: 1
21
pop number: 2
pop number: 3
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;
}
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;
}
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;
};
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;
}
Sample Output:
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;
}
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
Sample Input:
Sample Output:
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]<<" ";
}
}
Sample Input:
-1
Sample Output:
1234
Ans
#include<iostream>
#include<stdlib.h>
class Node{
public:
int data;
Node *next;
};
void append(Node **headadd,int data);
int main()
Node *head=NULL;
int data;
do
cin>>data;
if(data>0)
append(&head,data);
}while(data>0);
display(head);
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;
while(head!=NULL)
cout<<head->data<<" ";
head = head->next;
Sample Input:
12345
Sample Output:
Enter the size of stack:
12345
Ans
#include<iostream>
#include<stdlib.h>
class Node
public:
int data;
Node *next;
};
newnode->data = data;
newnode->next = head;
head = newnode;
}
void display()
while(head != NULL)
void reverse()
temp = current->next;
current->next = prev;
prev = current;
current = temp;
head = prev;
}
int main()
int n,data;
cin>>n;
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:
65432
Ans
#include<iostream>
#include<stdlib.h>
class Node{
public:
int data;
Node *next;
};
int main()
Node *head=NULL;
int data,n,c=0;
cin>>n;
cin>>data;
c++;
if(data>0)
append(&head,data);
display(head);
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;
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>
public:
int data;
Node *next;
};
void display();
void reverse();
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()
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:
1 2 3 4 6 7 8 9 10
Ans
#include<iostream>
#include<cstdlib>
struct btree
int data;
};
void create(int n)
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;
if(root == NULL){
cout<<"No element";
return;
if(t->left!=NULL)
inorder(t->left);
if(t->right != NULL)
inorder(t->right);
if(root == NULL)
cout<<"No element";
return;
}
cout<< t->data<<" ";
if(t->left!=NULL)
preorder(t->left);
if(t->right != NULL)
preorder(t->right);
if(root == NULL)
cout<<"No element";
return;
if(t->left!=NULL)
postorder(t->left);
if(t->right != NULL)
postorder(t->right);
if(t == NULL)
return 0;
}
else
if(lheight>rheight)
return lheight+1;
else
return rheight+1;
if(t == NULL)
return;
if(i == 1)
printgivenlevel(t->left, i-1);
printgivenlevel(t->right, i-1);
int h = height(t);
int 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);
inorder(root);
Sample Input:
10
15
-1
10
Sample Output:
Enter the element to be searched:
#include<iostream>
#include<cstdlib>
struct btree
int data;
};
void create(int n)
{
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;
if(root == NULL){
cout<<"No element";
return;
if(t->left!=NULL)
{
inorder(t->left);
if(t->right != NULL)
inorder(t->right);
if(root == NULL)
cout<<"No element";
return;
if(t->left!=NULL)
preorder(t->left);
if(t->right != NULL)
preorder(t->right);
}
if(root == NULL)
cout<<"No element";
return;
if(t->left!=NULL)
postorder(t->left);
if(t->right != NULL)
postorder(t->right);
if(t == NULL)
return 0;
else
{
int lheight = height(t->left);
if(lheight>rheight)
return lheight+1;
else
return rheight+1;
if(t == NULL)
return;
if(i == 1)
}
else if(i > 1)
printgivenlevel(t->left, i-1);
printgivenlevel(t->right, i-1);
if (btree == NULL)
return false;
if (btree->data == key)
return true;
return res2;
int h = height(t);
int 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;
cin>>key;
if (ifNodeExists(root, key))
else
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>
struct btree
int data;
};
void create(int n)
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;
if(root == NULL){
cout<<"No element";
return;
if(t->left!=NULL)
inorder(t->left);
if(t->right != NULL)
inorder(t->right);
if(root == NULL)
cout<<"No element";
return;
}
cout<< t->data<<" ";
if(t->left!=NULL)
preorder(t->left);
if(t->right != NULL)
preorder(t->right);
if(root == NULL)
cout<<"No element";
return;
if(t->left!=NULL)
postorder(t->left);
if(t->right != NULL)
postorder(t->right);
if(t == NULL)
{
return 0;
else
if(lheight>rheight)
return lheight+1;
else
return rheight+1;
if(t == NULL)
return;
}
if(i == 1)
printgivenlevel(t->left, i-1);
printgivenlevel(t->right, i-1);
int h = height(t);
int 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>
struct btree
int data;
};
void create(int n)
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;
if(root == NULL){
cout<<"No element";
return;
if(t->left!=NULL)
inorder(t->left);
if(t->right != NULL)
inorder(t->right);
if(root == NULL)
cout<<"No element";
return;
}
cout<< t->data<<" ";
if(t->left!=NULL)
preorder(t->left);
if(t->right != NULL)
preorder(t->right);
if(root == NULL)
cout<<"No element";
return;
if(t->left!=NULL)
postorder(t->left);
if(t->right != NULL)
postorder(t->right);
if(t == NULL)
{
return 0;
else
if(lheight>rheight)
return lheight+1;
else
return rheight+1;
if(t == NULL)
return;
}
if(i == 1)
printgivenlevel(t->left, i-1);
printgivenlevel(t->right, i-1);
int h = height(t);
int 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);
Sample Input:
4
2
-1
Sample Output:
21436
Ans
#include<iostream>
#include<cstdlib>
struct btree
int data;
};
void create(int n)
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;
if(root == NULL){
cout<<"No element";
return;
if(t->left!=NULL)
inorder(t->left);
if(t->right != NULL)
inorder(t->right);
if(root == NULL)
cout<<"No element";
return;
}
cout<< t->data<<" ";
if(t->left!=NULL)
preorder(t->left);
if(t->right != NULL)
preorder(t->right);
if(root == NULL)
cout<<"No element";
return;
if(t->left!=NULL)
postorder(t->left);
if(t->right != NULL)
postorder(t->right);
if(t == NULL)
{
return 0;
else
if(lheight>rheight)
return lheight+1;
else
return rheight+1;
if(t == NULL)
return;
}
if(i == 1)
printgivenlevel(t->left, i-1);
printgivenlevel(t->right, i-1);
int h = height(t);
int 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);
postorder(root);
Sample Input:
4
2
-1
Sample Output:
63142
Ans
#include<iostream>
#include<cstdlib>
struct btree
int data;
};
void create(int n)
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;
if(root == NULL){
cout<<"No element";
return;
if(t->left!=NULL)
inorder(t->left);
if(t->right != NULL)
inorder(t->right);
if(root == NULL)
cout<<"No element";
return;
}
cout<< t->data<<" ";
if(t->left!=NULL)
preorder(t->left);
if(t->right != NULL)
preorder(t->right);
if(root == NULL)
cout<<"No element";
return;
if(t->left!=NULL)
postorder(t->left);
if(t->right != NULL)
postorder(t->right);
if(t == NULL)
{
return 0;
else
if(lheight>rheight)
return lheight+1;
else
return rheight+1;
if(t == NULL)
return;
}
if(i == 1)
printgivenlevel(t->left, i-1);
printgivenlevel(t->right, i-1);
int h = height(t);
int 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);
levelorder(root);
Sample Input:
2
-1
Sample Output:
#include<iostream>
#include<cstdlib>
struct btree
int data;
};
void create(int n)
}
void insert(struct btree * t)
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;
if(root == NULL){
cout<<"No element";
return;
}
if(t->left!=NULL)
inorder(t->left);
if(t->right != NULL)
inorder(t->right);
if(root == NULL)
cout<<"No element";
return;
if(t->left!=NULL)
preorder(t->left);
if(t->right != NULL)
preorder(t->right);
if(root == NULL)
cout<<"No element";
return;
if(t->left!=NULL)
postorder(t->left);
if(t->right != NULL)
postorder(t->right);
if(t == NULL)
return 0;
}
else
if(lheight>rheight)
return lheight+1;
else
return rheight+1;
if(t == NULL)
return;
if(i == 1)
{
printgivenlevel(t->left, i-1);
printgivenlevel(t->right, i-1);
int h = height(t);
int 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(root);
Sample Input:
-1
Sample Output:
Maximum element is 6
Ans
#include <bits/stdc++.h>
// A Tree node
struct btree {
int data;
};
void create(int n)
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;
if (root == NULL)
return;
}
btree* current = root;
btree* pre;
if (current->left == NULL)
current = current->right;
else
{
// Find the inorder predecessor of current
pre = current->left;
current)
pre = pre->right;
if (pre->right == NULL)
pre->right = current;
current = current->left;
else
pre->right = NULL;
} // End of while
// Driver Code
int main()
/* 15
/\
19 11
/\
25 5
/\/\
17 3 23 24
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);
printMinMax(root);
return 0;
Sample Input:
-1
Sample Output:
#include <bits/stdc++.h>
struct btree {
int data;
};
void create(int n)
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;
if (root == NULL)
return 0;
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);
return 0;
Sample Input:
-1
Sample Output:
#include <bits/stdc++.h>
struct btree {
int data;
};
struct btree * root, * temp;
void create(int n)
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;
if (!root)
return;
sum += root->data;
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;
Sample Input:
-1
36
Sample Output:
#include <bits/stdc++.h>
struct btree {
int data;
};
void create(int n)
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;
// If both n1 and n2 are smaller than root, then LCA lies in left
root = root->left;
// If both n1 and n2 are greater than root, then LCA lies in right
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);
cin>>n1;
cin>>n2;
return 0;
Sample Input:
3
4
-1
Sample Output:
#include<iostream>
#include<stdlib.h>
class node
public:
int data;
node* left;
node *right;
};
node *root=NULL;
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)
temp = temp->left;
temp->left=newnode;
break;
temp = temp->right;
temp->right = newnode;
break;
if(temp == NULL)
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);
return 0;
Sample Input:
50
30
20
40
70
60
80
-1
Sample Output:
#include <bits/stdc++.h>
struct btree {
int data;
};
void create(int n)
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;
// base case
if (root == NULL)
return NULL;
if (left != NULL)
return left;
count++;
if (count == k)
return root;
int count = 0;
if (res == NULL)
else
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;
printKthSmallest(root, k);
Sample Input:
6
7
-1
Sample Output:
C.No : sum
-3 : 8
-2 : 4
-1 : 11
0 : 12
1:3
2:7
Ans
#include<iostream>
#include<bits/stdc++.h>
public:
int dat;
node* l;
node *r;
}*root=NULL;
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;
}}
Map[hd] += node->dat;
verticalSumUtil(root, 0, Map);
for (it = Map.begin(); it != Map.end(); ++it)
int main()
int a;
do{
cin>>a;
if(a<0)
break;
insert(&root,a);
}while(1);
cout<<"C.No : sum"<<endl;
verticalSum(root);
return 0;
-1
Sample Output:
#include<iostream>
#include<bits/stdc++.h>
class node
public:
int dat;
node* l;
node *r;
}*root=NULL;
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 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);
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:
0000
0000
0000
0000
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));
scanf("%d",&node);
scanf("%d",&edge);
graph=(int**)malloc(sizeof(int*)*node);
for(int i=0;i<node;i++)
*(graph+i)=(int*)malloc(sizeof(int)*edge);
scanf("%s",dir);
if(strcmp(dir,"yes")==0)
f=1;
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;}
for(int i=0;i<node;i++){
for(int j=0;j<edge;j++)
printf("%d ",graph[i][j]);
printf("\n");}
return 0;
}
Sample Input:
12
23
34
24
Sample Output:
Adjacency List:
0--->
1--->2
2--->1 3 4
3--->2 4
4--->3 2
5--->
Ans
#include<iostream>
#include<bits/stdc++.h>
#include<vector>
adj[u].push_back(v);
adj[v].push_back(u);
}
void printGraph(vector<int> adj[], int v)
cout<<"Adjacency List:\n";
cout<<"\n";
int main()
int v,e;
cin>>v>>e;
vector<int> adj[v];
for(int i=1;i<=e;i++)
int s,e;
cin>>s>>e;
addEdge(adj,s,e);
printGraph(adj, v);
return 0;
Sample Input:
01
12
23
30
24
42
Sample Output:
Yes
Ans
#include <iostream>
#include <list>
#include <stack>
class Graph
int V;
list<int> *adj;
void DFSUtil(int v, bool visited[]);
public:
bool isSC();
Graph getTranspose();
};
visited[v] = true;
list<int>::iterator i;
if (!visited[*i])
DFSUtil(*i, visited);
Graph Graph::getTranspose()
Graph g(V);
list<int>::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i)
g.adj[*i].push_back(v);
return g;
adj[v].push_back(w);
bool Graph::isSC()
bool visited[V];
visited[i] = false;
DFSUtil(0, visited);
if (visited[i] == false)
return false;
Graph gr = getTranspose();
for(int i = 0; i < V; i++)
visited[i] = false;
gr.DFSUtil(0, visited);
if (visited[i] == false)
return false;
return true;
int main()
int n,e;
cin>>n>>e;
Graph g1(n);
for(int i=1;i<=e;i++){
int st,ed;
cin>>st>>ed;
g1.addEdge(st, ed);
return 0;
}