Datastructure Programs
Datastructure Programs
2024–2025
NAME :
REG.NO. :
CLASS : II BCA-A
EXAMINERS
I.
II.
INDEX
1. List ADT
3. Infix to Postfix
#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
Node*next;
};
node->next=NULL;
return node;
}
void insertNewNode(Node**root,int data)
{
Node*node=newNode(data);
Node*ptr;
if(*root==NULL)
{
*root=node;
}
else
{
ptr= *root;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=node;
}
}
void printLinkedList(Node * root)
{
while(root!=NULL)
{
cout<<root->data<<"->";
root=root->next;
}
cout<<"NULL"<<endl;
}
Node*createLinkedList(int arr[], int n)
{
Node*root=NULL;
for(int i=0;i<n;i++)
{
insertNewNode(&root, arr[i]);
}
return root;
}
void main()
{
clrscr();
int arr[]={1,2,3,4,5}, n=5;
Node*root=createLinkedList(arr,n);
printLinkedList(root);
getch();
}
OUTPUT:
1->2->3->4->5->NULL
2)a)Program to implement stack ADT using singly linked list
#include <iostream.h>
#include <conio.h>
struct Node {
int data;
Node* next;
};
class Stack {
private:
Node* top;
public:
Stack() {
top = 0;
}
void push(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = top;
top = newNode;
cout << value <<" pushed to stack\n";
}
int pop() {
if (isEmpty()) {
cout <<"Stack is empty\n";
return top == 0;
}
void display() {
if (isEmpty()) {
while (temp != 0) {
cout << temp->data <<" ";
temp = temp->next;
}
Stack stack;
int choice, value;
clrscr();
while (1) {
case 1:
cout <<"Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
cout <<"Exiting...\n";
return;
default:
cout <<"Invalid choice. Please try again.\n";
}
}
}
OUTPUT:
1.Push
2.Pop
3.Display
4.Exit
Enter your choice:1
Enter value to push:0
0 pushed to stack
1.Push
2.Pop
3.Display
4.Exit
Enter your choice:1
Enter value to push:1
1 pushed to stack
1.Push
2.Pop
3.Display
4.Exit
1.Push
2.Pop
3.Display
4.Exit
1.Push
2.Pop
3.Display
4.Exit
Enter your choice:3
Stack elements: 1 0
1.Push
2.Pop
3.Display
4.Exit
Enter your choice:4
2)b)Program to implement queue ADT using singly linked list
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct Node {
int data;
Node* next;
};
class Queue {
private:
Node* front;
Node* rear;
public:
Queue() {
front = rear = NULL;
}
void enqueue(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}
int dequeue() {
if (front == NULL) {
cout <<"Queue is empty!"<< endl;
return -1;
}
rear = NULL;
}
delete temp;
return value;
}
void display() {
if (front == NULL) {
cout <<"Queue is empty!"<< endl;
return;
}
Node* temp = front;
cout <<"Queue elements: ";
while (temp != NULL) {
}
};
void main() {
clrscr();
Queue q;
int choice, value;
do {
case 1:
cout <<"Enter value to enqueue: ";
cin >> value;
q.enqueue(value);
break;
case 2:
value = q.dequeue();
if (value != -1) {
cout <<"Dequeued: "<< value << endl;
}
break;
case 3:
q.display();
break;
case 4:
cout <<"Exiting program."<< endl;
break;
default:
cout <<"Invalid choice! Please try again."<< endl;
}
} while (choice != 4);
getch();
}
OUTPUT:
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to enqueue: 1
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to enqueue: 2
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter the value to enqueue: 3
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Dequeued: 1
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#define MAX 50
return 1;
return 2;
return 0;
char stack[MAX];
int j = 0;
char ch = infix[i];
if (isdigit(ch))
postfix[j++] = ch;
}
else if (ch == '(')
stack[++top] = ch;
postfix[j++] = stack[top--];
top--;
else
postfix[j++] = stack[top--];
stack[++top] = ch;
postfix[j++] = stack[top--];
postfix[j] = '\0';
}
int stack[MAX];
char ch = postfix[i];
if (isdigit(ch))
stack[++top] = ch - '0';
else
if (top < 1)
return -1;
switch (ch) {
case '+':
break;
case '-':
stack[++top] = operand1 - operand2;
break;
case '*':
break;
case '/':
if (operand2 == 0)
return -1;
break;
default:
return -1;
if (top != 0)
return -1;
return stack[top];
}
int main()
clrscr();
cin.getline(infix, 50);
infixToPostfix(infix, postfix);
if (result != -1) {
getch();
return 0;
OUTPUT:
Evaluation result: 9
4)Program to implement priority queue ADT
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int priority;
int info;
struct node*link;
};
class Priority_Queue
{
private:
node*front;
public:
Priority_Queue()
{
front=NULL;
}
void insert(int item, int priority)
{
node*tmp, *q;
tmp=new node;
tmp->info=item;
tmp->priority=priority;
if(front==NULL||priority<front->priority)
{
tmp->link=front;
front=tmp;
}
else
{
q=front;
while(q->link!=NULL && q->link->priority<=priority)
q=q->link;
tmp->link=q->link;
q->link=tmp;
}
}
void del()
{
node*tmp;
if(front==NULL)
cout<<"Queue Underflow\n";
else
{
tmp=front;
}
void display()
{
node*ptr;
ptr=front;
if(front==NULL)
cout<<"Queue is empty\n";
else
{
cout<<"Queue is:\n";
cout<<"Priority Item\n";
while(ptr!=NULL)
{
cout<<ptr->priority<<""<<ptr->info<<endl;
ptr=ptr->link;
}
}
}
};
void main()
{
clrscr();
int choice, item, priority;
Priority_Queue pq;
do
{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input the item value to be added in the queue:";
cin>>item;
case 2:
pq.del();
break;
case 3:
pq.display();
break;
case 4:
break;
default:
cout<<"Wrong choice\n";
}
}
while(choice!=4);
getch();
}
OUTPUT:
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice:1
Input the item value to be added in the queue:5
Enter its priority:1
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice:1
Input the item value to be added in the queue:4
Enter its priority:2
1.Insert
2.Delete
3.Display
4.Quit
1.Insert
2.Delete
3.Display
4.Quit
1.Insert
2.Delete
3.Display
4.Quit
1.Insert
2.Delete
3.Display
4.Quit
3 3
4 2
5 1
1.Insert
2.Delete
3.Display
4.Quit
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice:4
5)a)Program to insert an element in a binary search tree
#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
Node*left;
Node*right;
};
Node*createNode(int data)
{
Node*newNode=new Node();
if(!newNode)
{
cout<<"Memory error\n";
return NULL;
}
newNode->data=data;
newNode->left=newNode->right=NULL;
return newNode;
}
Node*insertNode(Node*root, int data)
{
if(root==NULL)
{
root=createNode(data);
return root;
}
if(data<root->data)
{
root->left=insertNode(root->left, data);
}
else if(data>root->data)
{
root->right=insertNode(root->right, data);
}
return root;
}
void printTree(Node*root)
{
if(root==NULL)
return;
printTree(root->left);
cout<<root->data<<"";
printTree(root->right);
}
int main()
{
clrscr();
Node*root=NULL;
root=insertNode(root,50);
root=insertNode(root,30);
root=insertNode(root,20);
root=insertNode(root,40);
root=insertNode(root,70);
root=insertNode(root,60);
root=insertNode(root,80);
OUTPUT:
int data;
Node*left;
Node*right;
};
Node*createNode(int data)
{
Node*newNode=new Node();
if(!newNode)
{
cout<<"Memory error\n";
return NULL;
}
newNode->data=data;
newNode->left=newNode->right=NULL;
return newNode;
}
Node*insertNode(Node*root, int data)
{
if(root==NULL)
{
root=createNode(data);
return root;
}
if(data<root->data)
{
root->left=insertNode(root->left,data);
}
else if (data>root->data)
{
root->right=insertNode(root->right,data);
}
return root;
}
Node*findMin(Node*root)
{
while(root->left!=NULL)
{
root=root->left;
}
return root;
}
Node*deleteNode(Node*root, int data)
{
if(root==NULL)
return root;
if(data<root->data)
{
root->left=deleteNode(root->left,data);
}
else if(data>root->data)
{
root->right=deleteNode(root->right,data);
}
else
{
if(root->left==NULL && root->right==NULL)
{
delete root;
root=NULL;
}
else if(root->left==NULL)
{
Node*temp=root;
root=root->right;
delete temp;
}
else if(root->right==NULL)
{
Node*temp=root;
root=root->left;
delete temp;
}
else
{
Node*temp=findMin(root->right);
root->data=temp->data;
root->right=deleteNode(root->right,temp->data);
}
}
return root;
}
void printTree(Node*root)
{
if(root==NULL)
return;
printTree(root->left);
cout<<root->data<<"";
printTree(root->right);
}
int main()
{
clrscr();
Node*root=NULL;
root=createNode(50);
root=insertNode(root,30);
root=insertNode(root,20);
root=insertNode(root,40);
root=insertNode(root,70);
root=insertNode(root,60);
root=insertNode(root,80);
cout<<"Binary search tree before deletion:";
printTree(root);
cout<<endl;
root=deleteNode(root,20);
cout<<"Binary search tree after deletion:";
printTree(root);
cout<<endl;
getch();
}
OUTPUT:
Binary search tree before deletion: 20 30 40 50 60 70 80
Binary search tree after deletion: 30 40 50 60 70 80
5)c)Program to search a key element in a binary search tree
#include<iostream.h>
#include<conio.h>
struct Node
{
int data;
Node*left;
Node*right;
};
Node*createNode(int data)
{
Node*newNode=new Node();
if(!newNode)
{
cout<<"Memory error\n";
return NULL;
}
newNode->data=data;
newNode->left=newNode->right=NULL;
return newNode;
}
Node*insertNode(Node*root, int data)
{
if(root==NULL)
{
root=createNode(data);
return root;
}
if(data<root->data)
{
root->left=insertNode(root->left, data);
}
else if(data>root->data)
{
root->right=insertNode(root->right, data);
}
return root;
}
int searchNode(Node*root, int data)
{
if(root==NULL)
{
return 0;
}
if(data==root->data)
{
return 1;
}
if(data<root->data)
{
return searchNode(root->left, data);
}
else
{
return searchNode(root->right, data);
}
}
int main()
{
clrscr();
Node*root=NULL;
root=insertNode(root,50);
root=insertNode(root,30);
root=insertNode(root,20);
root=insertNode(root,40);
root=insertNode(root,70);
root=insertNode(root,60);
root=insertNode(root,80);
int searchValue=40;
else
{
cout<<"Element "<<searchValue<<" not found in the binary search tree."<<endl;
}
getch();
}
OUTPUT:
Element 40 found in the binary search tree.
6)a)Program to perform insertion into an AVL-Tree
#include <iostream.h>
#include <conio.h>
struct Node
{
int key;
Node* left;
Node* right;
int height;
};
int max(int a, int b)
{
return (a > b) ? a : b;
}
Node* createNode(int key)
{
Node* newNode = new Node;
newNode->key = key;
{
Node* temp = node->left;
Node* tempRight = temp->right;
temp->right = node;
node->left = tempRight;
updateHeight(node);
updateHeight(temp);
return temp;
}
updateHeight(temp);
return temp;
}
Node* rebalance(Node* node)
{
int balance = getBalance(node);
if (balance > 1)
{
Else
{
node->left = rotateLeft(node->left);
node = rotateRight(node);
}
}
else if (balance < -1)
{
if (getHeight(node->right->right) >= getHeight(node->right->left))
{
node = rotateLeft(node);
}
else
{
node->right = rotateRight(node->right);
node = rotateLeft(node);
}
}
return node;
}
Node* insertNode(Node* node, int key)
{
if (node == NULL)
{
return createNode(key);
}
if (key < node->key)
{
node->left = insertNode(node->left, key);
}
else if (key > node->key)
{
node->right = insertNode(node->right, key);
}
else
{
return node;
}
updateHeight(node);
node = rebalance(node);
return node;
}
inorderTraversal(node->left);
cout << node->key << " ";
inorderTraversal(node->right);
}
}
int main()
{
clrscr();
{
root = insertNode(root, keys[i]);
}
cout << "Inorder Traversal: ";
inorderTraversal(root);
cout << endl;
getch();
return 0;
}
OUTPUT:
Inorder Traversal: 10 20 25 30 40 50
6)b)Program to perform deletion from an AVL-Tree
#include <iostream.h>
#include <conio.h>
struct Node
{
int key;
Node* left;
Node* right;
int height;
};
int height(Node* node)
{
return node ? node->height : 0;
}
int max(int a, int b)
{
return (a > b) ? a : b;
}
Node* rightRotate(Node* y)
{
Node* x = y->left;
y->left = x->right;
x->right = y;
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
}
Node* leftRotate(Node* x)
{
Node* y = x->right;
x->right = y->left;
y->left = x;
newNode->key = key;
newNode->left = NULL;
newNode->right = NULL;
newNode->height = 1;
return newNode;
}
if (key < node->key)
{
}
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && key < node->right->key)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
{
root->left = deleteNode(root->left, key);
}
else if (key > root->key)
{
root->right = deleteNode(root->right, key);
}
else
{
if (!root->left || !root->right)
{
Node* temp = root->left ? root->left : root->right;
delete root;
return temp;
}
Node* temp = root->right;
while (temp->left)
{
temp = temp->left;
}
root->key = temp->key;
}
if (balance < -1 && getBalance(root->right) > 0)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
inOrder(root->left);
cout << root->key << " ";
inOrder(root->right);
}
}
int main()
{
clrscr();
OUTPUT:
In-order traversal of the AVL tree is:
10 20 25 30 40
#include<iostream.h>
#include<conio.h>
int graph[MAX][MAX] = {
{0, 1, 1, 0, 0},
{1, 0, 0, 1, 1},
{1, 0, 0, 0, 1},
{0, 1, 0, 0, 0},
{0, 1, 1, 0, 0}
};
visited[start] = 1;
queue[rear++] = start;
visited[i] = 1;
queue[rear++] = i;
visited[start] = 1;
DFS(i, visited);
int main()
clrscr();
BFS(0);
DFS(0, visited);
getch();
OUTPUT:
0 1 1 0 0
1 0 0 1 1
1 0 0 0 1
0 1 0 0 0
0 1 1 0 0
01234
01342
8)a)Program for implementing linear search method
#include<iostream.h>
#include<conio.h>
int linear_search(int arr[], int n, int target)
{
for(int i=0;i<n;i++)
{
if(arr[i]==target)
{
return i;
}
}
return -1;
}
void main()
{
clrscr();
int arr[]={3,6,1,8,2,4};
int n=sizeof(arr)/sizeof(arr[0]);
int target=8;
int result=linear_search(arr,n,target);
if(result!=-1)
{
cout<<"Element "<<target<<" found at index "<<result<<endl;
}
else
{
cout<<"Element"<<target<<"not found in the array"<<endl;
}
getch();
}
OUTPUT:
Element 8 found at index 3
8)b)Program for implementing binary search method
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,arr[10],num,first,last,middle;
cout<<"Enter 10 elements (in ascending order):";
for(i=0;i<10;i++)
cin>>arr[i];
cout<<"\n Enter element to be search:";
cin>>num;
first=0;
last=9;
middle=(first+last)/2;
while(first<=last)
{
if(arr[middle]<num)
first=middle+1;
else if(arr[middle]==num)
{
cout<<"\n The number "<<num<<" found at position "<<middle+1;
break;
}
else
last=middle-1;
middle=(first+last)/2;
}
if(first>last)
cout<<"\n The number "<<num<<" is not found in given array";
cout<<endl;
getch();
OUTPUT:
11
13
15
17
19
21
Enter element to be searched:7
The number 7 found at position 4
9)a)Program for implementing bubble sort
#include<iostream.h>
#include<conio.h>
void bubbleSort(int arr[], int n)
{
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
swapped=1;
}
}
if(!swapped)
break;
}
}
cout<<endl;
}
int main()
{
clrscr();
int arr[]={64, 34, 25, 12, 22, 11, 90};
int n=sizeof(arr)/sizeof(arr[0]);
cout<<"Original array:";
printArray(arr, n);
bubbleSort(arr, n);
cout<<"Sorted array:";
printArray(arr, n);
getch();
}
OUTPUT:
Original array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
9)b)Program for implementing selection sort
#include<iostream.h>
#include<conio.h>
void swap(int*xp, int*yp)
{
int temp=*xp;
*xp=*yp;
*yp=temp;
}
{
min_idx=i;
for(j=i+1; j<n; j++)
if(arr[j]<arr[min_idx])
min_idx=j;
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)
{
int i;
for(i=0; i<size; i++)
cout<<arr[i]<<"";
cout<<endl;
}
int main()
{
clrscr();
int arr[]={64, 25, 12, 22, 11};
int n=sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
cout<<"Sorted array: ";
printArray(arr, n);
getch();
}
OUTPUT:
Sorted array: 11 12 22 25 64
9)c)Program for implementing insertion sort
#include<iostream.h>
#include<conio.h>
void insertionSort(int arr[], int n)
{
for(int i=1; i<n; i++)
{
int key=arr[i];
int j=i-1;
while(j>=0&&arr[j]>key)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
}
cout<<endl;
}
int main()
{
clrscr();
int arr[]={12, 11, 13, 5, 6};
int n=sizeof(arr)/sizeof(arr[0]);
cout<<"Original array:";
printArray(arr, n);
insertionSort(arr, n);
cout<<"Sorted array:";
printArray(arr, n);
getch();
}
OUTPUT:
Original array: 12 11 13 5 6
Sorted array: 5 6 11 12 13
9)d)Program for implementing radix sort
#include <iostream.h>
#include <conio.h>
#define MAX 100
void countingSort(int arr[], int n, int exp)
{
int output[MAX];
int count[10] = {0};
int i;
}
for (i = 1; i < 10; i++)
{
count[i] += count[i - 1];
}
count[index]--;
}
for (i = 0; i < n; i++)
{
arr[i] = output[i];
}
}
void radixsort(int arr[], int n)
{
int max = arr[0];
max = arr[i];
}
}
for (int exp = 1; max / exp > 0; exp *= 10)
{
countingSort(arr, n, exp);
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] <<"";
}
cout << endl;
}
void main()
{
clrscr();
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
printArray(arr, n);
getch();
}
OUTPUT:
Unsorted array: 170 45 75 90 802 24 2 66
Sorted array: 2 24 45 66 75 90 170 802