DS File 2
DS File 2
AIM :-
Write a program to implement traversal, insertion and deletion of elements in a linear array.
CODE :-
#include<iostream> using
namespace std;
int main() {
const int capacity = 100;
// Maximum capacity of the arrayint
arr[capacity] ;
int n;
cout<<"Enter size of array: ";cin>>n;
cout<<"Enter elements of array: ";for(int
i=0;i<n;i++)
{
cin>>arr[i];
}
int choice;
int key, pos, x;
while (true) {
cout << "\nChoose an option:" << endl;cout << "1.
Traversal" << endl;
cout << "2. Insert at the end" << endl;
cout << "3. Insert at any position" << endl;cout << "4. Delete an
element" << endl;
cout << "5. Exit" << endl;cin >>
choice;
default:
cout << "Invalid choice. Please try again."<< endl;break;
}
}
return 0;
}
OUTPUT:-
EXPERIMENT-2(A)
AIM :-
Program to fetch a substring from a string, find its position and length of substring.
CODE :-
#include <iostream> #include
<string> using namespace
std;
int main() {
string inputString;
cout << "Enter the input string:";getline(cin,
inputString);
string substring;
cout << "Enter the substring to search:";cin >> substring;
size_t position = inputString.find(substring);
if (position != string::npos) {
// Calculate the length of the substringsize_t length =
substring.length();
cout << "Input String: " << inputString << endl;cout << "Substring: "
<< substring << endl;
cout << "Position of Substring: " << position << endl;cout << "Length of
Substring: " << length << endl;
}
else {
cout << "Substring not found in the input string." << endl;
}
return 0;
}
OUTPUT:-
EXPERIMENT-2(B)
AIM :-
Program to replace one string from another.
CODE :-
#include <iostream> #include
<string> using namespace
std;
int main(){
string inputString; string
oldSubstring;string
newSubstring;
return 0;
}
OUTPUT:-
EXPERIMENT-2(C)
AIM :-
Program for concatenation of two string.
CODE :-
#include <iostream> #include
<string> using namespace
std;
int main() {
string firstString; string secondString;
OUTPUT:-
EXPERIMENT-3(A)
AIM :-
Create a Stack and perform Pop, Push and Traverse operations on the stack using Arrays.
CODE :-
#include<bits/stdc++.h>using
namespace std;
cout<<"MENU - "<<endl;
fn1: cout<<"1. push"<<endl;cout<<"2.
pop"<<endl; cout<<"3. display"<<endl;
cout<<"4. exit"<<endl;
int choice ;
cout<<"Enter your choice - ";cin>>choice;
switch(choice){case
1:{
int x;
cout<<"Enter the number to be pushed to the stack - ";cin>>x;
push( x);
display();
cout<<endl;goto
fn1;
}
case 2:{
cout<<"The number popped out is - "<<pop()<<endl;display();
cout<<endl;goto
fn1;
}
case 3:{
cout<<"The elements of the stack are - "<<endl;display();
cout<<endl;goto
fn1;
}
case 4:{
return 0;
}
}
}
OUTPUT:-
EXPERIMENT-3(B)
AIM :-
Write a program to convert an infix expression to postfix conversion.
CODE :-
#include <iostream> #include
<stack> #include <string>
using namespace std;
int main() {
string infixExpression;
cout << "Enter an infix expression: ";getline(cin,
infixExpression);
return 0;
}
OUTPUT:-
EXPERIMENT-3(C)
AIM :-
To implement balanced parentheses using stacks.
CODE :-
#include <iostream>
#include <stack> #include
<string>
return 0;
}
OUTPUT:-
EXPERIMENT-4(A)
AIM :-
To implement insertion, deletion,traversing and display using circular queue.
CODE :-
#include <iostream> using
namespace std;
class CircularQueue
{
private:
int front, rear;
int queue[MAX_SIZE];
public:
CircularQueue()
{
front = rear = -1;
}
bool isFull()
{
return (front == 0 && rear == MAX_SIZE - 1) || (front == rear + 1);
}
bool isEmpty()
{
return front == -1;
}
int dequeue()
{
int item;
if (isEmpty())
{
cout << "Queue is empty. Cannot delete." << endl;return -1;
}
else
{
item = queue[front];if (front
== rear)
{
front = rear = -1;
}
else if (front == MAX_SIZE - 1)
{
front = 0;
}
else
{
front++;
}
return item;
}
}
void display()
{
if (isEmpty())
{
cout << "Queue is empty." << endl;
}
else
{
cout << "Queue elements: ";if (front <=
rear)
{
for (int i = front; i <= rear; i++)
{
cout << queue[i] << " ";
}
}
else
{
for (int i = front; i < MAX_SIZE; i++)
{
cout << queue[i] << " ";
}
for (int i = 0; i <= rear; i++)
{
cout << queue[i] << " ";
}
}
cout << endl;
}
}
};
int main()
{
CircularQueue queue;int
choice, item;
while (true)
{
cout << "Circular Queue Menu:" << endl; cout << "1.
Enqueue" << endl;
cout << "2. Dequeue" << endl; cout << "3.
Display" << endl; cout << "4. Exit" <<
endl;
cout << "Enter your choice:";cin >> choice;
switch (choice)
{
case 1:
cout << "Enter an element to enqueue: ";cin >> item;
queue.enqueue(item);break;
case 2:
item = queue.dequeue();if (item !=
-1)
{
cout << "Deleted element: " << item << endl;
}
break;
case 3:
queue.display();break;
case 4:
cout << "Exiting the program." << endl;return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
OUTPUT:-
EXPERIMENT-4(B)
AIM :-
To implement insertion, deletion ,traversing and display using priority queue.
CODE :-
#include <iostream> #include
<queue> using namespace
std;
int main()
{
priority_queue<int> pq;
while (true)
{
case 2:
if (pq.empty())
{
endl;
case 3:
cout << "Priority Queue elements: ";while (!
pq.empty())
{
cout << pq.top() << " ";pq.pop();
}
cout << endl;break;
case 4:
cout << "Exiting the program." << endl;return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
OUTPUT:-
EXPERIMENT-5(A)
AIM :-
Write a program to implement insertion, searching and deletion of elements in a singly LL.
CODE :-
#include <iostream> using
namespace std;struct Node
{
int data;
Node *next;
};
class LinkedList
{
private:
Node *head;
public:
LinkedList()
{
head = nullptr;
}
if (head->data == value)
{
Node *temp = head; head
= head->next;delete temp;
cout << "Node with value " << value << " deleted." << endl;return;
}
void displayList()
{
Node *current = head; if (current
== nullptr)
{
cout << "List is empty." << endl;return;
}
~LinkedList()
{
while (head != nullptr)
{
Node *temp = head; head
= head->next;delete temp;
}
}
};
int main()
{
LinkedList list;
int choice, value;
do
{
cout << "Menu:\n";
cout << "1. Insert Node\n";
switch (choice)
{
case 1:
cout << "Enter value to insert: ";cin >> value;
list.insertNode(value);
break;
case 2:
cout << "Enter value to delete: ";cin >> value;
list.deleteNode(value);
break;
case 3:
cout << "Enter value to search: ";cin >> value;
list.searchNode(value);
break;
case 4:
list.displayList();break;
case 5:
cout << "Exiting the program." << endl;break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 5);
return 0;
}
OUTPUT:-
EXPERIMENT-5(B)
AIM :-
Write a program to implement insertion, searching and deletion of elements in a doubly LL.
CODE :-
#include <iostream> using
namespace std;struct Node
{
int data;
Node *prev;
Node *next;
};
class DoublyLinkedList
{
private:
Node *head;
Node *tail;
public:
DoublyLinkedList()
{
head = nullptr;tail =
nullptr;
}
if (head == nullptr)
{
head = newNode;tail
= newNode;
}
else
{
newNode->next = head; head-
>prev = newNode; head =
newNode;
}
}
void deleteNode(int value)
{
Node *current = head;
while (current != nullptr){
if (current->data == value)
{
if (current == head)
{
head = current->next;if (head !
= nullptr)
{
head->prev = nullptr;
}
}
else if (current == tail)
{
tail = current->prev;if (tail !=
nullptr)
{
tail->next = nullptr;
}
}
else
{
current->prev->next = current->next;current->next-
>prev = current->prev;
}
delete current;
cout << "Node with value " << value << " deleted." << endl;
return;
}
current = current->next;
}
cout << "Node with value " << value << " not found." << endl;
}
void displayList()
{
Node *current = head; if (current
== nullptr)
{
cout << "List is empty." << endl;return;
}
cout << "Doubly Linked List (from head to tail): ";while (current != nullptr)
{
cout << current->data << " -> ";current =
current->next;
}
cout << "NULL" << endl;
}
}
;
int main()
{
DoublyLinkedList list;int
choice, value;
do
{
cout << "Menu:\n";
cout << "1. Insert Node\n"; cout << "2.
Delete Node\n"; cout << "3. Search
Node\n"; cout << "4. Display List\n";cout
<< "5. Exit\n";
cout << "Enter your choice: ";cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value to insert: ";cin >> value;
list.insertNode(value);
break;
case 2:
cout << "Enter value to delete: ";cin >> value;
list.deleteNode(value);
break;
case 3:
cout << "Enter value to search: ";cin >> value;
list.searchNode(value);
break;
case 4:
list.displayList();break;
case 5:
cout << "Exiting the program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 5);
return 0;
}
OUTPUT:-
EXPERIMENT-5(C)
AIM :-
Write a program to implement insertion, searching and deletion of elements in a circular LL.
CODE :-
#include <iostream> using
namespace std;struct Node
{
int data;
Node *next;
};
class CircularLinkedList
{
private:
Node *head;
public:
CircularLinkedList()
{
head = nullptr;
}
if (head->data == value)
{
Node *current = head;
while (current->next != head)
{
current = current->next;
}
current->next = head->next;if (head ==
head->next)
{
head = nullptr;
}
else
{
head = head->next;
}
delete current;
cout << "Node with value " << value << " deleted." << endl;return;
}
if (current != head)
{
prev->next = current->next;delete
current;
cout << "Node with value " << value << " deleted." << endl;
}
else
{
cout << "Node with value " << value << " not found." << endl;
}
}
cout << "Node with value " << value << " not found." << endl;
}
void displayList()
{
if (head == nullptr)
{
cout << "List is empty." << endl;return;
}
int main()
{
CircularLinkedList list;int choice,
value;
do
{
cout << "Menu:\n";
cout << "1. Insert Node\n"; cout << "2.
Delete Node\n"; cout << "3. Search
Node\n"; cout << "4. Display List\n";cout
<< "5. Exit\n";
cout << "Enter your choice: ";cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value to insert: ";cin >> value;
list.insertNode(value);break;
case 2:
cout << "Enter value to delete: ";cin >> value;
list.deleteNode(value);
break;
case 3:
cout << "Enter value to search: ";cin >> value;
list.searchNode(value);
break;
case 4:
list.displayList();break;
case 5:
cout << "Exiting the program." << endl;break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 5);
return 0;
}
OUTPUT:-
EXPERIMENT-6
AIM :-
Program to implement Stack using Linked List
CODE :-
#include <iostream> using
namespace std;struct Node
{
int data;
Node *next;
};
class Stack
{
private:
Node *top;int
size;
int capacity;
public:
Stack(int maxCapacity)
{
top = nullptr;size =
0;
capacity = maxCapacity;
}
bool isFull()
{
return size >= capacity;
}
bool isEmpty()
{
return size == 0;
}
void pop()
{
if (isEmpty())
{
cout << "Stack is empty. Pop operation not possible." << endl;return;
}
int main()
{
int capacity, choice, value, topValue;
cout << "Enter the maximum capacity of the stack: ";cin >> capacity;
Stack stack(capacity);do
{
cout << "Menu:\n"; cout <<
"1. Push\n";cout << "2.
Pop\n";
cout << "3. Get Top\n";cout <<
"4. Exit\n";
cout << "Enter your choice: ";cin >> choice;
switch (choice)
{
case 1:
if (!stack.isFull())
{
cout << "Enter value to push: ";cin >> value;
stack.push(value);
}
else
{
endl;
}
break;
case 2:
stack.pop();break;
case 3:
if (stack.getTop(topValue))
{
cout << "Top element: " << topValue << endl;
}
break;
case 4:
cout << "Exiting the program." << endl;break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 4);return 0;
}
OUTPUT:-
EXPERIMENT-7
AIM :-
Program to implement Queue using Linked List
CODE :-
class Queue
{
private:
Node *front;
Node *rear;
public:
Queue()
{
front = nullptr;rear =
nullptr;
}
bool isEmpty()
{
return front == nullptr;
}
if (isEmpty())
{
front = newNode;rear =
newNode;
}
else
{
rear->next = newNode;rear =
newNode;
}
cout << "Enqueued: " << value << endl;
}
void dequeue()
{
if (isEmpty())
{
endl;
int getFront()
{
if (isEmpty())
{
cout << "Queue is empty. No front element." << endl;return -1; // Return -1 to
indicate an empty queue.
}
return front->data;
}
};
int main()
{
Queue queue;
int choice, value, frontValue;
do
{
cout << "Menu:\n";
cout << "1. Enqueue\n"; cout << "2.
Dequeue\n"; cout << "3. Get
Front\n";cout << "4. Exit\n";
cout << "Enter your choice: ";cin >> choice;
switch (choice)
{
case 1:
cout << "Enter value to enqueue: ";cin >> value;
queue.enqueue(value);break;
case 2:
queue.dequeue();break;
case 3:
frontValue = queue.getFront();if
(frontValue != -1)
{
cout << "Front element: " << frontValue << endl;
}
break;
case 4:
cout << "Exiting the program." << endl;break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 4);
return 0;
}
OUTPUT:-
EXPERIMENT-8(A)
AIM :-
Insertion, Deletion and Traversal in Binary Search Tree
CODE :-
#include <iostream> using
namespace std;
struct Node
{
int data;
Node *left;
Node *right;
Node(int val) : data(val), left(NULL), right(NULL) {}
};
return root;
}
return root;
}
return root;
}
int main()
{
Node *root = NULL;
root = recursiveInsert(root, 50); root =
recursiveInsert(root, 30); root =
recursiveInsert(root, 70); root =
recursiveInsert(root, 20); root =
recursiveInsert(root, 40); root =
recursiveInsert(root, 60); root =
recursiveInsert(root, 80);
return 0;
}
OUTPUT:-
EXPERIMENT-8(B)
AIM :-
Insertion, Deletion and Traversal in Threaded Binary Tree
CODE :-
#include <iostream> using
namespace std;
class ThreadedBinarySearchTree {private:
struct Node {
int data;
Node* left;
Node* right;
bool isThreaded;
};
Node* root;
public:
ThreadedBinarySearchTree() {root =
nullptr;
}
return root;
}
void inOrderTraversal() {
Node* current = findLeftMostNode(root);while (current !
= nullptr) {
std::cout << current->data << " "; current =
findInOrderSuccessor(current);
}
std::cout << std::endl;
}
tbst.deleteNode(3);
cout << "In-order traversal after deleting 3: ";tbst.inOrderTraversal();
tbst.deleteNode(7);
cout << "In-order traversal after deleting 7: ";tbst.inOrderTraversal();
return 0;
}
OUTPUT:-
EXPERIMENT-9(A)
AIM :-
Sorting - Insertion sort.
CODE :-
#include <iostream> using
namespace std;
size);
return 0;
}
OUTPUT:-
EXPERIMENT-9(B)
AIM :-
Sorting - Quick sort.
CODE :-
#include <iostream> using
namespace std;
int partition(int *array, int low, int high) {int pivot = array[high];
int i = low - 1;
return i + 1;
}
void quickSort(int *array, int low, int high) {if (low < high) {
int pivotIndex = partition(array, low, high);
int main() {
int array[] = {5, 3, 2, 1, 4};
int size = sizeof(array) / sizeof(array[0]);quickSort(array, 0, size -
1);
return 0;
}
OUTPUT:-
EXPERIMENT-9(C)
AIM :-
Sorting - Merge sort.
CODE :-
#include <iostream> using
namespace std;
void merge(int *array, int low, int mid, int high) {int leftArraySize = mid -
low + 1;
int rightArraySize = high - mid;
int i = 0; int j = 0;
int k = low;
void mergeSort(int *array, int low, int high) {if (low < high) {
int mid = (low + high) / 2;
int main() {
int array[] = {5, 3, 2, 1, 4};
int size = sizeof(array) / sizeof(array[0]);mergeSort(array, 0, size -
1);
OUTPUT:-
EXPERIMENT-10(A)
class Graph {
private:
void dfs(int node, vector<int> adj[], int vis[], vector<int> &ls) {
vis[node] = 1;
ls.push_back(node);
// traverse all its neighbours
for(auto it : adj[node]) {
// if the neighbour is not visited
if(!vis[it]) {
dfs(it, adj, vis, ls);
}
}
}
public:
// Function to return a list containing the DFS traversal of the graph.
vector<int> dfsOfGraph(int V, vector<int> adj[]) {
int vis[V] = {0};
int start = 0;
vector<int> ls;
// call dfs for starting node
dfs(start, adj, vis, ls);
return ls;
}
};
int main()
{
vector <int> adj[5];
addEdge(adj, 0, 2);
addEdge(adj, 2, 4);
addEdge(adj, 0, 1);
addEdge(adj, 0, 3);
Graph g;
vector <int> ans = g.dfsOfGraph(5, adj);
printAns(ans);
return 0;
}
OUTPUT:-
EXPERIMENT-10(B)
class Graph
{
public:
// Function to return Breadth First Traversal of given graph.
vector<int> bfsOfGraph(int V, vector<int> adj[])
{
int vis[V] = {0};
vis[0] = 1;
queue<int> q;
q.push(0);
vector<int> bfs;
while (!q.empty())
{
if (!vis[it])
{
vis[it] = 1;
q.push(it);
}
}
}
return bfs;
}
};
int main()
{
vector<int> adj[6];
addEdge(adj, 0, 1);
addEdge(adj, 1, 2);
addEdge(adj, 1, 3);
addEdge(adj, 0, 4);
Graph g;
vector<int> ans = g.bfsOfGraph(5, adj);
printAns(ans);
return 0;
}
OUTPUT: