0% found this document useful (0 votes)
13 views

Data Structure Lab report

Uploaded by

mmh.rikon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Data Structure Lab report

Uploaded by

mmh.rikon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Data Structure Lab - 520202

INDEX

No Problem Page Date

Write a program to search an element from an array


01. 01
using linear search algorithm.

Write a program to search an element from an array


02. 2-3
using binary search algorithm.

Write a program to calculate factorial of a number


03. 4
using recursion.

Write a program to implement Tower of Hanoi problem


04. 5
by using the recursive function.

Write a program to replace a substring by another


05. 6
substring into a text.

Write a program to sort an array using bubble sort


06. 7
algorithm.

Write a program to sort an array using quick sort


07. 8-9
algorithm.

Write a program to implement all operations in an


08. 10-11
array, (Insertion and deletion).

Write a program to implement all operations in linked


09. 12-14
list, (Insertion and deletion).

Write a program to implement all operations in stack,


10. 15-16
(Insertion and deletion).

Write a program to implement all operations in queue,


11. 17-18
(Insertion and deletion).

12. Write a program to traverse a binary tree in any order. 19-20


Data Structure Lab | Page |1

1. Problem: Write a program to search an element from an array using


linear search algorithm.
Source Code:
#include <iostream>
using namespace std;
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
int main() {
int n, target;
cout << "Number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the target element: ";
cin >> target;

int result = linearSearch(arr, n, target);


if (result != -1) {
cout << "Element found at index: " << result << endl;
}
else {
cout << "Element not found." << endl;
}
return 0;
}

Input:
Number of elements: 5
Enter the elements: 10 20 30 40 50
Enter the target element: 20

Output:
Element found at index: 1
Data Structure Lab | Page |2

2. Problem: Write a program to search an element from an array using


binary search algorithm.
Source Code:
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int size, int target) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main()
{
int n, target;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
sort(arr, arr+n);
cout << "The array after sorting: ";
for (int i = 0; i < n; ++i) {
cout << arr[i] <<" ";
}
cout << endl;

cout << "Enter the target element: ";


cin >> target;

int result = binarySearch(arr, n, target);


if (result != -1) {
cout << "Element found at index: " << result << endl;
}
else {
cout << "Element not found." << endl;
}
return 0;
}
Data Structure Lab | Page |3

Input & Output:


Enter the number of elements: 5
Enter the elements: 9 2 6 5 7
The array after sorting: 2 5 6 7 9
Enter the target element: 6
Element found at index: 2
Data Structure Lab | Page |4

3. Problem: Write a program to calculate factorial of a number using


recursion.
Source Code:
#include <iostream>
using namespace std;

int factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
return n * factorial(n - 1);
}
int main()
{
int n;
cout << "Enter a number: ";
cin >> n;

cout << "Factorial of " << n << " is: " << factorial(n) << endl;

return 0;
}

Input:
Enter a number: 9

Output:
Factorial of 9 is: 362880
Data Structure Lab | Page |5

4. Problem: Write a program to implement Tower of Hanoi problem by


using the recursive function.
Source Code:
#include <iostream>
using namespace std;

void towerOfHanoi(int n, char from, char to, char aux)


{
if (n == 1)
{
cout << "Move disk 1 from " << from << " to " << to << endl;
return;
}
towerOfHanoi(n - 1, from, aux, to);

cout << "Move disk " << n << " from " << from << " to " << to << endl;

towerOfHanoi(n - 1, aux, to, from);


}

int main()
{
int n;
cout << "Enter the number of disks: ";
cin >> n;

towerOfHanoi(n, 'A', 'C', 'B');

return 0;
}

Input:
Enter the number of disks: 3

Output:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Data Structure Lab | Page |6

5. Problem: Write a program to replace a substring by another substring


into a text.
Source Code:
#include <iostream>
#include <string>
using namespace std;

string replaceSubstring(const string &text, const string &toReplace, const string


&replacement)
{
string result = text;
size_t pos = result.find(toReplace);
while (pos != string::npos)
{
result.replace(pos, toReplace.length(), replacement);
pos = result.find(toReplace, pos + replacement.length());
}
return result;
}

int main()
{
string text, toReplace, replacement;
cout << "Enter the text: ";
getline(cin, text);

cout << "Enter the substring to replace: ";


getline(cin, toReplace);

cout << "Enter the replacement substring: ";


getline(cin, replacement);

string result = replaceSubstring(text, toReplace, replacement);

cout << "\nResulting text: " << result << endl;

return 0;
}

Input & Output:


Enter the text: Coding is just a painful distraction.
Enter the substring to replace: a painful distraction.
Enter the replacement substring: Wubba lubba dub dub!

Resulting text: Coding is just Wubba lubba dub dub!


Data Structure Lab | Page |7

6. Problem: Write a program to sort an array using bubble sort algorithm.

Source Code:
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n)


{
for (int i=0; i < n-1; i++)
{
for (int j=0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main()
{
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}

bubbleSort(arr, n);

cout << "Sorted array: ";


for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

Input:
Enter the number of elements: 5
Enter the elements: 9 2 5 3 8

Output:
Sorted array: 2 3 5 8 9
Data Structure Lab | Page |8

7. Problem: Write a program to sort an array using quick sort algorithm.

Source Code:
#include <iostream>
using namespace std;
int partition(int arr[], int low, int high)
{
int pivot = arr[high], i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
++i;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
void quickSort(int arr[], int low, int high)
{
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main()
{
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

quickSort(arr, 0, n - 1);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Data Structure Lab | Page |9

Input:
Enter the number of elements: 6
Enter the elements: 9 2 5 3 8 6

Output:
Sorted array: 2 3 5 6 8 9
Data Structure Lab | Page | 10

8. Problem: Write a program to implement all operations in an array,


(Insertion and deletion).
Source Code:
#include <iostream>
using namespace std;

void insertElement(int arr[], int& n, int pos, int value) {


for (int i = n; i > pos; --i) {
arr[i] = arr[i - 1];
}
arr[pos] = value;
++n;
}

void deleteElement(int arr[], int& n, int pos) {


for (int i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
--n;
}

int main()
{
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[100];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

int pos, value;


cout << "Enter position to insert (0 to " << n << "): ";
cin >> pos;
cout << "Enter value to insert: ";
cin >> value;

if (pos >= 0 && pos <= n) {


insertElement(arr, n, pos, value);
cout << "Array after insertion: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
else {
cout << "Invalid position." << endl;
}
Data Structure Lab | Page | 11

cout << "Enter position to delete (0 to " << n - 1 << "): ";
cin >> pos;
if (pos >= 0 && pos < n) {
deleteElement(arr, n, pos);
cout << "Array after deletion: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
else {
cout << "Invalid position." << endl;
}

return 0;
}

Input & Output:


Enter the number of elements: 5
Enter the elements: 1 2 3 4 5
Enter position to insert (0 to 5): 5
Enter value to insert: 6
Array after insertion: 1 2 3 4 5 6
Enter position to delete (0 to 5): 2
Array after deletion: 1 2 4 5 6
Data Structure Lab | Page | 12

9. Problem: Write a program to implement all operations in linked list,


(Insertion and deletion).
Source Code:
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};

class LinkedList {
Node* head;
public:
LinkedList() {
head = nullptr;
}
void insertAtBeginning(int value) {
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
}
void insertAtEnd(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
void deleteFromBeginning() {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}
Node* temp = head;
head = head->next;
delete temp;
}
Data Structure Lab | Page | 13

void deleteFromEnd() {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}
if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
Node* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}
void display() {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};

int main() {
LinkedList list;
int choice, value;

cout << "Options->\n";


cout << "Enter 1 to insert at beginning\n";
cout << "2 to insert at end\n";
cout << "3 to delete from beginning\n";
cout << "4 to delete from end\n";
cout << "5 to display list\n";
cout << "6 to exit:\n";

while (true) {
cout << "Enter your choice: ";
cin >> choice;

if (choice == 1) {
cout << "Enter value to insert at beginning: ";
cin >> value;
Data Structure Lab | Page | 14

list.insertAtBeginning(value);
}
else if (choice == 2) {
cout << "Enter value to insert at end: ";
cin >> value;
list.insertAtEnd(value);
}
else if (choice == 3) {
list.deleteFromBeginning();
}
else if (choice == 4) {
list.deleteFromEnd();
}
else if (choice == 5) {
list.display();
}
else if (choice == 6) {
cout << "Exiting..." << endl;
break;
}
else {
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}

Input & Output:


Options->
Enter 1 to insert at beginning
2 to insert at end
3 to delete from beginning
4 to delete from end
5 to display list
6 to exit:
Enter your choice: 1
Enter value to insert at beginning: 20
Enter your choice: 2
Enter value to insert at end: 30
Enter your choice: 1
Enter value to insert at beginning: 10
Enter your choice: 5
10 -> 20 -> 30 -> NULL
Enter your choice: 3
Enter your choice: 4
Enter your choice: 2
Enter value to insert at end: 50
Enter your choice: 5
20 -> 50 -> NULL
Enter your choice: 6
Exiting...
Data Structure Lab | Page | 15

10. Problem: Write a program to implement all operations in stack,


(Insertion and deletion).
Source Code:
#include <iostream>
using namespace std;

class Stack {
int top;
int arr[100];
int maxSize;

public:
Stack(int size) {
top = -1;
maxSize = size;
}

void push(int value) {


if (top >= maxSize - 1) {
cout << "Stack overflow." << endl;
}
else {
arr[++top] = value;
cout << "Inserted " << value << " into the stack." << endl;
}
}
void pop() {
if (top < 0) {
cout << "Stack underflow." << endl;
}
else {
cout << "Deleted " << arr[top--] << " from the stack." << endl;
}
}

void display() {
if (top < 0) {
cout << "Stack is empty." << endl;
}
else {
cout << "Stack elements: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
Data Structure Lab | Page | 16

int main()
{
int n;
cout << "Enter the size of the stack: ";
cin >> n;

Stack stack(n);

cout << "Enter elements: ";


for(int i=0; i<n; i++)
{
int val;
cin >> val;
stack.push(val);
}
stack.display();

stack.pop();
stack.pop();
stack.push(70);
stack.push(80);
stack.push(90);
stack.display();

return 0;
}

Input:
Enter the size of the stack: 5
Enter elements: 10 20 30 40 50
Output:
Inserted 10 into the stack.
Inserted 20 into the stack.
Inserted 30 into the stack.
Inserted 40 into the stack.
Inserted 50 into the stack.
Stack elements: 50 40 30 20 10
Deleted 50 from the stack.
Deleted 40 from the stack.
Inserted 70 into the stack.
Inserted 80 into the stack.
Stack overflow.
Stack elements: 80 70 30 20 10
Data Structure Lab | Page | 17

11. Problem: Write a program to implement all operations in queue,


(Insertion and deletion).
Source Code:
#include <iostream>
using namespace std;

class Queue
{
private:
int front, rear, size;
int* queue;

public:
Queue(int s) {
size = s;
queue = new int[size];
front = -1;
rear = -1;
}

void enqueue(int value) {


if (rear == size - 1) {
cout << "Queue is full!" << endl;
}
else {
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = value;
cout << value << " enqueued to queue" << endl;
}
}
void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue is empty!" << endl;
}
else {
cout << queue[front] << " dequeued from queue" << endl;
front++;
}
}
void display() {
if (front == -1 || front > rear) {
cout << "Queue is empty!" << endl;
}
else {
cout << "Queue elements: ";
Data Structure Lab | Page | 18

for (int i = front; i <= rear; i++) {


cout << queue[i] << " ";
}
cout << endl;
}
}
bool isEmpty() {
return (front == -1 || front > rear);
}
bool isFull() {
return (rear == size - 1);
}
};
int main() {
int n;
cout << "Enter the size of the queue: ";
cin >> n;
Queue q(n);
cout << "Enter elements: ";
for(int i=0; i<n; i++){
int val;
cin >> val;
q.enqueue(val);
}
q.display();

q.dequeue();
q.dequeue();
q.display();

return 0;
}

Input:
Enter the size of the queue: 5
Enter elements: 10 20 30 40 50
Output:
10 enqueued to queue
20 enqueued to queue
30 enqueued to queue
40 enqueued to queue
50 enqueued to queue
Queue elements: 10 20 30 40 50
10 dequeued from queue
20 dequeued from queue
Queue elements: 30 40 50
Data Structure Lab | Page | 19

12. Problem: Write a program to traverse a binary tree in any order.

Source Code:
#include <iostream>
using namespace std;

struct Node {
int data;
Node* left;
Node* right;

Node(int value) {
data = value;
left = right = nullptr;
}
};

void preOrder(Node* root) {


if (root == nullptr) return;
cout << root->data << " ";
preOrder(root->left);
preOrder(root->right);
}
void inOrder(Node* root) {
if (root == nullptr) return;
inOrder(root->left);
cout << root->data << " ";
inOrder(root->right);
}
void postOrder(Node* root) {
if (root == nullptr) return;
postOrder(root->left);
postOrder(root->right);
cout << root->data << " ";
}

int main()
{
// Creating a simple binary tree
// 1
// / \
// 2 3
// / \
// 4 5
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
Data Structure Lab | Page | 20

cout << "Pre-order traversal: ";


preOrder(root);
cout << endl;

cout << "In-order traversal: ";


inOrder(root);
cout << endl;

cout << "Post-order traversal: ";


postOrder(root);
cout << endl;

return 0;
}

Output:
Pre-order traversal: 1 2 4 5 3
In-order traversal: 4 2 5 1 3
Post-order traversal: 4 5 2 3 1

You might also like