DS Lab File 3rd Sem
DS Lab File 3rd Sem
07701182023
1
Mehnaaz Ansari
07701182023
INDEX
S.No. List of Experiments Date Remarks
2
Mehnaaz Ansari
07701182023
3
Mehnaaz Ansari
07701182023
4
Mehnaaz Ansari
07701182023
EXPERIMENT-1
Language- cpp
Compiler- vs code
AIM: Write a C program to implement Array data structure with following operations
a) Traversal
b) Insertion
c) Deletion
d) Sorting
e) Searching (linear Search)
SOURCE CODE:
#include <iostream>
using namespace std;
// Traversal
void traverse(int arr[], int size)
{
if (size == 0)
{
cout << "Array is empty!" << endl;
return;
}
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
// Insertion
void insert(int arr[], int &size, int element, int index)
{
if (size >= MAX_CAPACITY)
{
cout << "Array is full, cannot insert!" << endl;
return;
}
if (index < 0 || index > size)
{
cout << "Invalid index!" << endl;
return;
}
for (int i = size; i > index; i--)
{
5
Mehnaaz Ansari
07701182023
// Deletion
void remove(int arr[], int &size, int index)
{
if (index < 0 || index >= size)
{
cout << "Invalid index!" << endl;
return;
}
for (int i = index; i < size - 1; i++)
{
arr[i] = arr[i + 1];
}
size--;
}
6
Mehnaaz Ansari
07701182023
int main()
{
int arr[MAX_CAPACITY];
int size = 0;
// Insertion
insert(arr, size, 10, 0);
insert(arr, size, 20, 1);
insert(arr, size, 30, 2);
insert(arr, size, 40, 3);
insert(arr, size, 50, 4);
// Deletion
remove(arr, size, 2);
cout << "Array after deletion: ";
traverse(arr, size);
// Sorting
insert(arr, size, 35, 2);
sort(arr, size);
cout << "Array after sorting: ";
traverse(arr, size);
// Searching
int index = search(arr, size, 40);
if (index != -1)
{
cout << "Element 40 found at index: " << index << endl;
}
else
{
cout << "Element not found!" << endl;
}
return 0;
}
OUTPUT:
7
Mehnaaz Ansari
07701182023
EXPERIMENT-2
SOURCE CODE:
#include <iostream>
using namespace std;
void addMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
8
Mehnaaz Ansari
07701182023
void subtractMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}
void multiplyMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows1, int
cols1, int cols2)
{
for (int i = 0; i < rows1; i++)
{
for (int j = 0; j < cols2; j++)
{
result[i][j] = 0;
for (int k = 0; k < cols1; k++)
{
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
int main()
{
int matrix1[10][10], matrix2[10][10], result[10][10];
int rows1, cols1, rows2, cols2;
int choice;
cout << "Enter rows and columns for first matrix: ";
cin >> rows1 >> cols1;
inputMatrix(matrix1, rows1, cols1);
cout << "Enter rows and columns for second matrix: ";
9
Mehnaaz Ansari
07701182023
switch (choice)
{
case 1:
if (rows1 == rows2 && cols1 == cols2)
{
addMatrices(matrix1, matrix2, result, rows1, cols1);
cout << "Resultant Matrix after Addition:\n";
displayMatrix(result, rows1, cols1);
}
else
{
cout << "Matrices must have the same dimensions for addition!" << endl;
}
break;
case 2:
if (rows1 == rows2 && cols1 == cols2)
{
subtractMatrices(matrix1, matrix2, result, rows1, cols1);
cout << "Resultant Matrix after Subtraction:\n";
displayMatrix(result, rows1, cols1);
}
else
{
cout << "Matrices must have the same dimensions for subtraction!" << endl;
}
break;
case 3:
if (cols1 == rows2)
{
multiplyMatrices(matrix1, matrix2, result, rows1, cols1, cols2);
cout << "Resultant Matrix after Multiplication:\n";
displayMatrix(result, rows1, cols2);
}
else
{
cout << "Number of columns in first matrix must equal number of rows in second
matrix for multiplication!" << endl;
}
10
Mehnaaz Ansari
07701182023
break;
case 4:
transposeMatrix(matrix1, result, rows1, cols1);
cout << "Transpose of the first matrix:\n";
displayMatrix(result, cols1, rows1);
default:
cout << "Invalid choice!" << endl;
}
return 0;
}
OUTPUT:
11
Mehnaaz Ansari
07701182023
EXPERIMENT-3
SOURCE CODE:
#include <iostream>
#include <string>
using namespace std;
return count;
}
12
Mehnaaz Ansari
07701182023
int main()
{
string str1, str2, word;
int choice;
switch (choice)
{
case 1:
cout << "Enter the first string: ";
getline(cin, str1);
cout << "Enter the second string: ";
getline(cin, str2);
cout << "Concatenated String: " << concatenateStrings(str1, str2) << endl;
break;
case 2:
cout << "Enter the string to be reversed: ";
getline(cin, str1);
cout << "Reversed String: " << reverseString(str1) << endl;
break;
case 3:
cout << "Enter the string: ";
getline(cin, str1);
cout << "Enter the word to find: ";
getline(cin, word);
cout << "The word '" << word << "' occurs " << countOccurrences(str1, word) << "
times in the string." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
return 0;
}
OUTPUT:
13
Mehnaaz Ansari
07701182023
14
Mehnaaz Ansari
07701182023
EXPERIMENT-4
AIM: Write a C program to perform following operations on a Single Linked List data
structure
a) Traversal
b) Insertion
1.Insertion after a particular node
2.Insertion before a particular node
c) Deletion
d) Reversal of a Liked List by revering the links
SOURCE CODE:
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
if (current != nullptr)
{
Node *newNode = new Node{data, current->next};
current->next = newNode;
}
else
15
Mehnaaz Ansari
07701182023
{
cout << "Node with data " << target << " not found!" << endl;
}
}
if (head->data == target)
{
Node *newNode = new Node{data, head};
head = newNode;
return;
}
if (current->next != nullptr)
{
Node *newNode = new Node{data, current->next};
current->next = newNode;
}
else
{
cout << "Node with data " << target << " not found!" << endl;
}
}
if (head->data == data)
{
Node *temp = head;
head = head->next;
delete temp;
return;
16
Mehnaaz Ansari
07701182023
if (current->next != nullptr)
{
Node *temp = current->next;
current->next = current->next->next;
delete temp;
}
else
{
cout << "Node with data " << data << " not found!" << endl;
}
}
head = prev;
}
int main()
{
Node *head = nullptr;
// Inserting 25 after 20
17
Mehnaaz Ansari
07701182023
// Inserting 15 before 20
insertBefore(head, 20, 15);
cout << "List after inserting 15 before 20:" << endl;
traverseList(head);
// Deleting 25
deleteNode(head, 25);
cout << "List after deleting 25:" << endl;
traverseList(head);
return 0;
}
OUTPUT:
18
Mehnaaz Ansari
07701182023
EXPERIMENT-5
AIM: Write a C program to add two Polynomial Equations using Linked List
SOURCE CODE:
#include <iostream>
using namespace std;
struct Node
{
int coeff;
int exp;
Node *next;
};
19
Mehnaaz Ansari
07701182023
20
Mehnaaz Ansari
07701182023
return result;
}
int main()
{
Node *poly1 = nullptr;
Node *poly2 = nullptr;
return 0;
}
OUTPUT:
21
Mehnaaz Ansari
07701182023
EXPERIMENT-6
Language- C++
Compiler- VS code
SOURCE CODE:
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node *prev;
Node(int data)
{
this->data = data;
this->next = nullptr;
this->prev = nullptr;
}
};
class DoublyLinkedList
{
private:
Node *head;
public:
DoublyLinkedList()
{
head = nullptr;
}
void traverseForward()
{
Node *temp = head;
while (temp != nullptr)
{
cout << temp->data << " -> ";
temp = temp->next;
22
Mehnaaz Ansari
07701182023
}
cout << "nullptr" << endl;
}
void traverseBackward()
{
if (head == nullptr)
return;
temp->next = newNode;
23
Mehnaaz Ansari
07701182023
newNode->prev = temp;
}
if (temp == nullptr)
{
cout << "Node with data " << data << " not found!" << endl;
return;
}
if (temp->prev != nullptr)
{
temp->prev->next = temp->next;
}
else
{
head = temp->next;
}
if (temp->next != nullptr)
{
temp->next->prev = temp->prev;
}
delete temp;
}
};
int main()
{
DoublyLinkedList dll;
24
Mehnaaz Ansari
07701182023
return 0;
}
OUTPUT:
25
Mehnaaz Ansari
07701182023
EXPERIMENT-7
Language- C++
Compiler- VS code
SOURCE CODE:
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = nullptr;
}
};
class CircularLinkedList
{
private:
Node *head;
public:
CircularLinkedList()
{
head = nullptr;
}
26
Mehnaaz Ansari
07701182023
27
Mehnaaz Ansari
07701182023
}
}
temp->next = head->next;
delete head;
head = temp->next;
return;
}
if (temp == head)
{
cout << "Node with value " << data << " not found." << endl;
return;
}
28
Mehnaaz Ansari
07701182023
prev->next = temp->next;
delete temp;
}
};
int main()
{
CircularLinkedList cll;
cout << "Circular Linked List after deletion (Traversal):" << endl;
cll.traverse(); // Output: 5 -> 10 -> HEAD
return 0;
}
OUTPUT:
29
Mehnaaz Ansari
07701182023
EXPERIMENT-8
Language- C++
Compiler- VS code
class Stack
{
private:
int top;
int arr[5]; // Array to hold stack elements with a fixed size of 5
public:
Stack()
{
top = -1; // Initialize top to -1 indicating that the stack is empty
for (int i = 0; i < 5; i++)
{
arr[i] = 0; // Initialize all elements of the array to 0
}
}
bool isEmpty()
{
return top == -1;
}
bool isFull()
{
return top == 4; // Since the array size is 5, the last index is 4
}
30
Mehnaaz Ansari
07701182023
int pop()
{
if (isEmpty())
{
cout << "Stack underflow" << endl;
return 0;
}
else
{
int popValue = arr[top]; // Store the value to be popped
arr[top] = 0; // Reset the position in the array
top--; // Decrement the top index
cout << popValue << " popped from stack." << endl;
return popValue; // Return the popped value
}
}
int count()
{
return (top + 1); // Number of elements in the stack
}
31
Mehnaaz Ansari
07701182023
void display()
{
cout << "All values in the Stack are: " << endl;
for (int i = top; i >= 0; i--)
{
cout << arr[i] << endl; // Print stack elements from top to bottom
}
}
};
int main()
{
Stack s1;
s1.push(10);
s1.push(20);
s1.push(30);
s1.push(40);
s1.push(50);
s1.push(60); // Stack overflow test
s1.display();
s1.pop();
s1.display();
cout << "Element at position 1 is: " << s1.peek(1) << endl; // Peek at position 1
return 0;
}
OUTPUT:
32
Mehnaaz Ansari
07701182023
33
Mehnaaz Ansari
07701182023
EXPERIMENT-9
Language- C++
Compiler- VS code
SOURCE CODE:
#include <iostream>
using namespace std;
Node(int val)
{
data = val; // Initialize data with the passed value
next = nullptr; // Initialize next pointer as null
}
};
public:
Stack()
{
top = nullptr; // Initialize top as null, indicating an empty stack
}
34
Mehnaaz Ansari
07701182023
35
Mehnaaz Ansari
07701182023
int main()
{
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.display();
return 0;
}
OUTPUT:
36
Mehnaaz Ansari
07701182023
EXPERIMENT-10
Language- C++
Compiler- VS code
SOURCE CODE:
OUTPUT:
37
Mehnaaz Ansari
07701182023
EXPERIMENT-11
Language- C++
Compiler- VS code
AIM:
SOURCE CODE:
OUTPUT:
38
Mehnaaz Ansari
07701182023
EXPERIMENT-12
Language- C++
Compiler- VS code
AIM:
SOURCE CODE:
OUTPUT:
39
Mehnaaz Ansari
07701182023
EXPERIMENT-13
Language- C++
Compiler- VS code
AIM:
SOURCE CODE:
OUTPUT:
40
Mehnaaz Ansari
07701182023
EXPERIMENT-14
Language- C++
Compiler- VS code
AIM:
SOURCE CODE:
OUTPUT:
41
Mehnaaz Ansari
07701182023
EXPERIMENT-15
Language- C++
Compiler- VS code
AIM:
SOURCE CODE:
OUTPUT:
42