Data Structure Programs
Data Structure Programs
DATA STRUCTURES
1
S.No Name of Program Page No.
2
1.//Insertion, deletion and traversing in array
#include<iostream>
#include<conio.h>
int main()
int array[30],choice,location,item,i,length,position;
cin>>length;
for(i=0;i<=length-1;i++)
{ cin>>array[i];
again:
cin>>choice;
switch(choice)
{ case 1: traversing(array,length);
goto again;
case 2: searching(array,item,length);
3
goto again;
cin>>item>>position;
insertion(array,length,item,position);
for(i=0;i<=length;i++)
{cout<<array[i]<<endl;
goto again;
cin>>position;
length =deletion(array,length,position);
for(i=0;i<length;i++)
{cout<<array[i]<<"\n";}
goto again;
case 5: break;
goto again;
getch();
{ for(int i=0;i<=length-1;i++)
{cout<<array[i]<<" ";}
4
int searching(int array[],int item,int length)
{ int i;
cin>>item;
for( i=0;i<=length-1;i++)
{ if ( array[i]==item)
{ location=i;
if (location==-1)
{ int j=length-1;
while(j>=position)
{ array[j+1]=array[j];
j=j-1;
array[position]=item;
length=length+1;
5
int deletion(int array[],int length,int position)
while(position<length)
{ array[position]=array[position+1];
position=position+1;
length=length-1;
return length;
6
#include<stdio.h>
#define MAX 5
int a[MAX],top=-1;
void push();
void pop();
void display();
int peek();
int main()
int ch;
printf("3. DISPLAY\n");
printf("4. END\n");
printf("5. PEEK");
while(1)
printf("\n\nEnter Choice:");
scanf("%d",&ch);
switch(ch)
case 1:
push();
7
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
case 5:
peek();
break;
8
void push()
int data;
if(top==MAX-1)
printf("\nstack is full\n");
else
printf("enter element\n");
scanf("%d",&data);
top++;
a[top]=data;
void pop()
if(top==-1)
printf("stack is empty\n");
else
9
top--;
void display()
int i;
if (top>=0)
for (i=top;i>=0;i--)
printf("\n%d",a[i]);
else
int peek()
if(top<0)
printf("Stack is empty\n");
else
10
{
return a[top];
11
3.//Converting from Infix to postfix
#include<iostream>
#include<bits/stdc++.h>
int prec(char c)
if(c=='^')
return 3;
if(c=='*'&&c=='/')
return 2;
if (c=='+'&&c=='-')
return 1;
else
return -1;
void infixToPostfix(string s)
stack<char> st;
st.push('N');
int l = s.length();
string ns;
if((s[i] >= 'a' && s[i] <= 'z')||(s[i] >= 'A' && s[i] <= 'Z'))
ns+=s[i];
12
st.push('(');
char c = st.top();
st.pop();
ns += c;
if(st.top() == '(')
char c = st.top();
st.pop();
} else{
char c = st.top();
st.pop();
ns += c;
st.push(s[i]);
while(st.top() != 'N')
char c = st.top();
13
st.pop();
ns += c;
int main()
infixToPostfix(exp);
return 0;
};
14
4.//Insertion and Deletion in Queues
#include <iostream>
#include<conio.h>
class Queue {
private:
int item, i;
int arr_queue[MAX_SIZE];
int rear;
int front;
public:
Queue() {
rear = 0;
front = 0;
void insert() {
if (rear == MAX_SIZE)
else {
cin>>item;
cout << "\n## Position : " << rear + 1 << " , Insert Value : " << item;
arr_queue[rear++] = item;
void deleteData() {
15
if (front == rear)
else {
cout << "\n## Position : " << front << " , Remove Value :" << arr_queue[front];
front++;
}}
void display() {
cout << "\nPosition : " << i << " , Value : " << arr_queue[i];
}};
int main() {
Queue obj;
cin>>choice;
switch (choice) {
case 1:
obj.insert();
break;
case 2:
obj.deleteData();
break;
case 3:
obj.display();
16
break;
default:
exit_p = 0;
break; }
} while (exit_p);
return 0; }
5.//Circular Queue
17
#include<iostream>
class node
public:
node()
next = NULL;
int data;
node *next;
}*front=NULL,*rear=NULL,*n,*temp,*temp1;
class cqueue
public:
void insertion();
void deletion();
void display();
};
int main()
cqueue cqobj;
int ch;
do
18
cout<<"\nMain Menu";
cin>>ch;
switch(ch)
case 1:
cqobj.insertion();
cqobj.display();
break;
case 2:
cqobj.deletion();
break;
case 3:
cqobj.display();
break;
case 4:
break;
default:
}while(ch!=4);
return 0;
void cqueue::insertion()
n=new node[sizeof(node)];
19
cin>>n->data;
if(front==NULL)
front=n;
else
rear->next=n;
rear=n;
rear->next=front;
void cqueue::deletion()
int x;
temp=front;
if(front==NULL)
else
if(front==rear)
x=front->data;
delete(temp);
front=NULL;
20
rear=NULL;
else
x=temp->data;
front=front->next;
rear->next=front;
delete(temp);
display();
void cqueue::display()
temp=front;
temp1=NULL;
if(front==NULL)
else
while(temp!=temp1)
cout<<temp->data<<" ";
21
temp=temp->next;
temp1=front;
22
6..//Insertion and Deletion in Linked List
#include<iostream>
#include<cstdio>
#include<cstdlib>
struct node
int info;
}*start;
class single_llist
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void display();
single_llist()
start = NULL;
};
int main()
23
{
single_llist sl;
start = NULL;
while (1)
cout<<"4.Delete a Node"<<endl;
cout<<"5.Display LL"<<endl;
cout<<"6.Exit "<<endl;
cin>>choice;
switch(choice)
{case 1:
sl.insert_begin();
cout<<endl;
break;
case 2:
sl.insert_last();
cout<<endl;
break;
case 3:
24
sl.insert_pos();
cout<<endl;
break;
case 4:
sl.delete_pos();
break;
case 5:
cout<<"Display LL"<<endl;
sl.display();
cout<<endl;
break;
case 6:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
//Creating Node
if (temp == NULL)
25
{
return 0;
else
temp->info = value;
temp->next = NULL;
return temp;
void single_llist::insert_begin()
int value;
cin>>value;
temp = create_node(value);
if (start == NULL)
start = temp;
start->next = NULL;
else
p = start;
26
start = temp;
start->next = p;
void single_llist::insert_last()
int value;
cin>>value;
temp = create_node(value);
s = start;
s = s->next;
temp->next = NULL;
s->next = temp;
void single_llist::insert_pos()
27
{
cin>>value;
temp = create_node(value);
cin>>pos;
int i;
s = start;
while (s != NULL)
s = s->next;
counter++;
if (pos == 1)
if (start == NULL)
start = temp;
start->next = NULL;
else
ptr = start;
start = temp;
start->next = ptr;
28
}
s = start;
ptr = s;
s = s->next;
ptr->next = temp;
temp->next = s;
else
void single_llist::delete_pos()
if (start == NULL)
cout<<"List is empty"<<endl;
return;
29
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
s = start;
if (pos == 1)
start = s->next;
else
while (s != NULL)
s = s->next;
counter++;
s = start;
ptr = s;
s = s->next;
ptr->next = s->next;
else
30
cout<<"Position out of range"<<endl;
free(s);
cout<<"Element Deleted"<<endl;
void single_llist::display()
if (start == NULL)
return;
temp = start;
cout<<temp->info<<"->";
temp = temp->next;
cout<<"NULL"<<endl;
31
32
7.//Traversing binary tree
#include <iostream>
struct node{
int value;
node *left;
node *right;
};
class btree{
public:
btree();
~btree();
void destroy_tree();
void inorder_print();
void postorder_print();
void preorder_print();
private:
33
void postorder_print(node *leaf);
node *root;
};
btree::btree(){
root = NULL;
btree::~btree(){
destroy_tree();
if(leaf != NULL){
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
if(leaf->left != NULL){
34
insert(key, leaf->left);
}else{
leaf->left->value = key;
leaf->left->left = NULL;
leaf->left->right = NULL;
if(leaf->right != NULL){
insert(key, leaf->right);
}else{
leaf->right->value = key;
leaf->right->right = NULL;
leaf->right->left = NULL;
if(root != NULL){
insert(key, root);
}else{
root->value = key;
root->left = NULL;
35
root->right = NULL;
if(leaf != NULL){
if(key == leaf->value){
return leaf;
}else{
}else{
return NULL;
void btree::destroy_tree(){
destroy_tree(root);
36
void btree::inorder_print(){
inorder_print(root);
if(leaf != NULL){
inorder_print(leaf->left);
inorder_print(leaf->right);
void btree::postorder_print(){
postorder_print(root);
if(leaf != NULL){
inorder_print(leaf->left);
inorder_print(leaf->right);
void btree::preorder_print(){
37
preorder_print(root);
if(leaf != NULL){
inorder_print(leaf->left);
inorder_print(leaf->right);
int main(){
//btree tree;
tree->insert(10);
tree->insert(6);
tree->insert(14);
tree->insert(5);
tree->insert(8);
tree->insert(11);
tree->insert(18);
cout<<"Preorder Traversal"<<"\n";
tree->preorder_print();
38
cout<<"\n"<<"Inorder Traversal"<<"\n";
tree->inorder_print();
cout<<"\n"<<"Postorder Traversal"<<"\n";
tree->postorder_print();
delete tree;
39
8.//Binary search
#include<iostream>
#include<conio.h>
int main()
cin>>n;
cin>>arr[i];
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
first = middle + 1;
40
{
break;
else
last = middle - 1;
getch();
41
9.//Linear Search
#include<iostream>
#include<conio.h>
int main()
cin>>n;
cin>>arr[i];
cin>>num;
if(arr[i]==num)
c=1;
pos=i+1;
break;
42
}
if(c==0)
else
getch();
43
10.// Merge sorting
#include <iostream>
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
temp[k] = a[i];
k++;
i++;
else
temp[k] = a[j];
k++;
j++;
44
temp[k] = a[i];
k++;
i++;
temp[k] = a[j];
k++;
j++;
a[i] = temp[i-low];
int mid;
mid=(low+high)/2;
45
}
int main()
int n, i;
cin>>n;
int arr[n];
cin>>arr[i];
cout<<"->"<<arr[i];
return 0;}
46
11.//Bubble sort
#include<iostream>
#include<conio.h>
int main()
cin>>n;
cin>>arr[i];
if (arr[j]>arr[j+1])
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]= temp;
47
}
cout<<arr[i]<<" ";
getch();
48
12.//Quick sort
#include<iostream>
int i;
for (i=0;i<size;i++) {
cout<<arr[i]<<" ";
int temp;
temp = arr[idx1];
arr[idx1] = arr[idx2];
arr[idx2] = temp;
int k = arr[end_idx];
49
int i = start_idx - 1;
int j;
for (j=start_idx;j<end_idx;j++) {
if (arr[j] <= k) {
i++;
swap(arr,i,j);
swap(arr,i+1,end_idx);
return i+1;
50
int main() {
displayArray(arr,size);
quickSort(arr,0,size-1);
displayArray(arr,size);
cout<<endl;
return 0;
51
13.//Insertion sort
#include<iostream>
#include<conio.h>
int main(){
cin>>size;
for(i=0;i<size;i++)
cin>>a[i];
for(i=1;i<size;i++)
temp = a[i];
j = i-1;
a[j+1] = a[j];
j--;
52
}
a[j+1] = temp;
for(i=0;i<size;i++)
cout<<"\n"<<a[i];
return 0;
53
14.//Breadth First Search
#include<iostream>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
int main()
int m;
//clrscr();
cin >> n;
cin >> m;
cin >>i>>j;
cost[i][j]=1;
cin >>v;
visited[v]=1;
k=1;
while(k<n)
54
{
visit[j]=1;
qu[rare++]=j;
v=qu[front++];
k++;
visit[v]=0;
visited[v]=1;
getch();
return 0;
55
15.//Depth Breadth Search
#include<iostream>
struct node{
int info;
} *root=NULL;
node *getnode(){
node *root;
root=new node;
return root;
if(root==NULL)
root= getnode();
root->left=root->right=NULL;
root->info=digit;
else
56
if(root->info>=digit){
root->left=insert(root->left,digit);
else
root->right=insert(root->right,digit);
return(root);
}}
if(root==NULL)
return;
cout<<root->info<<" ";
dfs(root->left);
dfs(root->right);
if(root==NULL){
return;}
space +=3;
display(root->right,space);
cout<<"\n";
cout<<" ";
57
cout<<root->info;
display(root->left,space);
int main(){
root=insert(root,78);
insert(root,52);
insert(root,123);
insert(root,137);
insert(root,60);
insert(root,63);
insert(root,56);
dfs(root);
display(root,0);}
58
16.// Traversing and finding depth of Binary Search Tree and display
#include<iostream>
struct node{
int info;
} *root=NULL;
node *getnode(){
node *root;
root=new node;
return root;
if(root==NULL)
root= getnode();
root->left=root->right=NULL;
root->info=digit;
else
59
{
if(root->info>=digit){
root->left=insert(root->left,digit);
else
root->right=insert(root->right,digit);
return(root);
}}
if(root==NULL)
return;
cout<<root->info<<" ";
preorder(root->left);
preorder(root->right);
if(root==NULL)
return;
inorder(root->left);
cout<<root->info<<" ";
inorder(root->right);
60
void postorder(struct node* root){
if(root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<root->info<<" ";
if(root==NULL){
return;
space +=3;
display(root->right,space);
cout<<"\n";
cout<<" ";
cout<<root->info;
display(root->left,space);}
if(root==NULL)
return 0;
else{
int ldepth=depth(root->left);
61
int rdepth=depth(root->right);
int main(){
root=insert(root,78);
insert(root,52);
insert(root,123);
insert(root,137);
insert(root,60);
insert(root,63);
insert(root,56);
preorder(root);
inorder(root);
postorder(root);
display(root,0);
return 0;
62
17.//Binary Tree Display
#include<iostream>
#define COUNT 10
struct Node{
int data;
};
node->data = data;
return node;
// Base case
if (root == NULL)
return;
space += COUNT;
63
// Process right child first
print2DUtil(root->right, space);
// count
cout<<"\n";
cout<<" ";
cout<<root->data<<"\n";
print2DUtil(root->left, space);
print2DUtil(root, 0);
int main()
root->left = newNode(2);
root->right = newNode(3);
64
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
//root->left->left->left = newNode(8);
root->left->left->right = newNode(9);
root->left->right->left = newNode(10);
root->left->right->right = newNode(11);
root->right->left->left = newNode(12);
root->right->left->right = newNode(13);
root->right->right->left = newNode(14);
root->right->right->right = newNode(15);
print2D(root);
return 0;}
65
18.//BST insertion and deletion
#include<iostream>
bool c=false;
struct node
int data;
node* left;
node* right;
};
newNode->data=data;
newNode->left=NULL;
newNode->right=NULL;
return newNode;
if (root != NULL)
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
66
}
node* findMin(node*root)
while(root->left!=NULL)
root=root->left;
return root;
if (root == NULL)
return getNode(data);
return root;
{ if(root==NULL)
return false;
return true;
67
Search(root->left,value);
Search(root->right,value);
c=Search(root,value);
if(root==NULL)
return root;
root->left= Delete(root->left,value);
root->right= Delete(root->right,value);
// Node deletion
else
if(root->left==NULL&&root->right==NULL)
delete root;
root=NULL;
68
return root;
else if(root->left==NULL)
root=root->right;
delete temp;
return root;
else if(root->right==NULL)
root=root->left;
delete temp;
return root;
//case 3: 2 child
else
{ struct node*temp=findMin(root->right);
root->data=temp->data;
root->right=Delete(root->right,temp->data);
}}
return root;}
int main()
node* root=NULL;
69
root=Insert(root,20);
Insert(root,15);
Insert(root,25);
Insert(root,18);
Insert(root,10);
Insert(root,16);
Insert(root,19);
// Insert(root,17);
//Node insertion
root=Insert(root,17);
cout<<"\nNode Inserted"<<endl;
cout<<"\nInorder: ";
inorder(root);
cout<<endl<<endl;
//Deletion of node
cout<<"Inorder: ";
inorder(root);
cout<<endl<<endl;
Delete(root,15);
if(c)
70
cout<<"Node Deleted"<<endl;
cout<<"Inorder: ";
inorder(root);
cout<<endl;
else
return 0;
71