0% found this document useful (0 votes)
23 views28 pages

Ds Lab Report

The document contains a series of lab reports for a Data Structures Laboratory course, authored by Jubair Munshi, detailing various programming tasks in C++. Each lab report covers different topics such as string operations, word processing, linear arrays, linked lists, stacks, and queues, along with corresponding C++ code implementations. The reports are submitted to Dr. Md. Golam Moazzam and include submission dates and student identification information.

Uploaded by

munshijubair7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views28 pages

Ds Lab Report

The document contains a series of lab reports for a Data Structures Laboratory course, authored by Jubair Munshi, detailing various programming tasks in C++. Each lab report covers different topics such as string operations, word processing, linear arrays, linked lists, stacks, and queues, along with corresponding C++ code implementations. The reports are submitted to Dr. Md. Golam Moazzam and include submission dates and student identification information.

Uploaded by

munshijubair7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

LAB REPORT

Data Structures Laboratory


Course Code : CSE 156

Topic: Lab 1- String operations

Submitted To
Dr. Md. Golam Moazzam, B.Sc(JU), MS(JU), PhD(JU)
Professor, Department of Computer Science and Engineering
Jahangirnagar University

Submitted By
Student Name: Jubair Munshi
Student ID No: 331
Registration no: 20230657066
Course Title: Data Structures Laboratory
Course Code: CSE-156
Submission Date: 18.11.2024
C++ Code:
#include <iostream>
#include <string>
using namespace std;

int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);

int choice;
do {
cout << "\nString Operations Menu:\n";
cout << "1. Substring\n2. Indexing\n3. Concatenation\n4. Length\n5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
int start, length;
cout << "Enter starting index and length: ";
cin >> start >> length;
cout << "Substring: " << str.substr(start, length) << endl;
break;
}
case 2: {
int index;
cout << "Enter index: ";
cin >> index;
cout << "Character at index " << index << ": " << str[index] << endl;
break;
}
case 3: {
string extra;
cout << "Enter string to concatenate: ";
cin.ignore();
getline(cin, extra);
str += extra;
cout << "Concatenated String: " << str << endl;
break;
}
case 4:
cout << "Length of string: " << str.length() << endl;
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 5);

return 0;
}
Output:
LAB REPORT
Data Structures Laboratory
Course Code : CSE 156

Topic: Lab 2- Word Processing

Submitted To
Dr. Md. Golam Moazzam, B.Sc(JU), MS(JU), PhD(JU)
Professor, Department of Computer Science and Engineering
Jahangirnagar University

Submitted By
Student Name: Jubair Munshi
Student ID No: 331
Registration no: 20230657066
Course Title: Data Structures Laboratory
Course Code: CSE-156
Submission Date: 18.11.2024
C++ Code:
#include <iostream>
#include <string>
using namespace std;

int main() {
string text;
cout << "Enter a sentence: ";
getline(cin, text);

int choice;
do {
cout << "\nWord Processing Menu:\n";
cout << "1. Insert\n2. Delete\n3. Replace\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
string word;
int position;
cout << "Enter word to insert and position: ";
cin >> word >> position;
text.insert(position, word + " ");
cout << "Updated Text: " << text << endl;
break;
}
case 2: {
int start, length;
cout << "Enter starting index and length to delete: ";
cin >> start >> length;
text.erase(start, length);
cout << "Updated Text: " << text << endl;
break;
}
case 3: {
string oldWord, newWord;
cout << "Enter word to replace and new word: ";
cin >> oldWord >> newWord;
size_t pos = text.find(oldWord);
if (pos != string::npos) {
text.replace(pos, oldWord.length(), newWord);
}
cout << "Updated Text: " << text << endl;
break;
}
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 4);
return 0;
}

Output:
LAB REPORT
Data Structures Laboratory
Course Code : CSE 156

Topic: Lab 3 - Operations on Linear array

Submitted To
Dr. Md. Golam Moazzam, B.Sc(JU), MS(JU), PhD(JU)
Professor, Department of Computer Science and Engineering
Jahangirnagar University

Submitted By
Student Name: Jubair Munshi
Student ID No: 331
Registration no: 20230657066
Course Title: Data Structures Laboratory
Course Code: CSE-156
Submission Date: 18.11.2024
C++ 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]) {
swap(arr[j], arr[j + 1]);
}
}
}
}

int binarySearch(int arr[], int low, int high, int key) {


while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}

int main() {
int arr[100], n;
cout << "Enter number of elements: ";
cin >> n;

cout << "Enter elements:\n";


for (int i = 0; i < n; i++) {
cin >> arr[i];
}

int choice;
do {
cout << "\nArray Operations Menu:\n";
cout << "1. Sort (Bubble Sort)\n2. Search (Binary Search)\n3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
bubbleSort(arr, n);
cout << "Sorted Array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
break;
case 2: {
int key;
cout << "Enter element to search: ";
cin >> key;
int result = binarySearch(arr, 0, n - 1, key);
if (result != -1)
cout << "Element found at index: " << result << endl;
else
cout << "Element not found.\n";
break;
}
case 3:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 3);

return 0;
}

Output:
LAB REPORT
Data Structures Laboratory
Course Code : CSE 156

Topic: Lab 4 – Linked List Operations

Submitted To
Dr. Md. Golam Moazzam, B.Sc(JU), MS(JU), PhD(JU)
Professor, Department of Computer Science and Engineering
Jahangirnagar University

Submitted By
Student Name: Jubair Munshi
Student ID No: 331
Registration no: 20230657066
Course Title: Data Structures Laboratory
Course Code: CSE-156
Submission Date: 18.11.2024
C++ Code:
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

void insertAtBeginning(Node*& head, int data) {


Node* newNode = new Node();
newNode->data = data;
newNode->next = head;
head = newNode;
}

void deleteNode(Node*& head, int key) {


Node *temp = head, *prev = nullptr;
if (temp != nullptr && temp->data == key) {
head = temp->next;
delete temp;
return;
}
while (temp != nullptr && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == nullptr) return;
prev->next = temp->next;
delete temp;
}

void searchNode(Node* head, int key) {


while (head != nullptr) {
if (head->data == key) {
cout << "Element found: " << key << endl;
return;
}
head = head->next;
}
cout << "Element not found.\n";
}

void displayList(Node* head) {


while (head != nullptr) {
cout << head->data << " -> ";
head = head->next;
}
cout << "NULL\n";
}

int main() {
Node* head = nullptr;
int choice, value;
do {
cout << "\nLinked List Operations Menu:\n";
cout << "1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
insertAtBeginning(head, value);
break;
case 2:
cout << "Enter value to delete: ";
cin >> value;
deleteNode(head, value);
break;
case 3:
cout << "Enter value to search: ";
cin >> value;
searchNode(head, value);
break;
case 4:
displayList(head);
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 5);

return 0;
}

Output:
Linked List Operations Menu:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice: 1
Enter value to insert: 5

Linked List Operations Menu:


1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice: 1
Enter value to insert: 10

Linked List Operations Menu:


1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice: 1
Enter value to insert: 15

Linked List Operations Menu:


1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice: 3
Enter value to search: 5
Element found: 5

Linked List Operations Menu:


1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice: 4
15 -> 10 -> 5 -> NULL

Linked List Operations Menu:


1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice: 2
Enter value to delete: 5

Linked List Operations Menu:


1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice: 4
15 -> 10 -> NULL

Linked List Operations Menu:


1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice: 5
Exiting...
LAB REPORT
Data Structures Laboratory
Course Code : CSE 156

Topic: Lab 5 - Stack

Submitted To
Dr. Md. Golam Moazzam, B.Sc(JU), MS(JU), PhD(JU)
Professor, Department of Computer Science and Engineering
Jahangirnagar University

Submitted By
Student Name: Jubair Munshi
Student ID No: 331
Registration no: 20230657066
Course Title: Data Structures Laboratory
Course Code: CSE-156
Submission Date: 18.11.2024
C++ Code:
#include <iostream>
using namespace std;

#define MAX 100

class Stack {
int top;
int arr[MAX];

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

bool push(int x) {
if (top >= MAX - 1) {
cout << "Stack Overflow\n";
return false;
}
arr[++top] = x;
return true;
}

bool pop() {
if (top < 0) {
cout << "Stack Underflow\n";
return false;
}
cout << "Popped element: " << arr[top--] << endl;
return true;
}

void displayTop() {
if (top < 0) {
cout << "Stack is empty\n";
} else {
cout << "Top element: " << arr[top] << endl;
}
}
};

int main() {
Stack stack;
int choice, value;

do {
cout << "\nStack Operations Menu:\n";
cout << "1. Push\n2. Pop\n3. Display Top Element\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
stack.displayTop();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 4);

return 0;
}

Output:
Stack Operations Menu:
1. Push
2. Pop
3. Display Top Element
4. Exit
Enter your choice: 1
Enter value to push: 10

Stack Operations Menu:


1. Push
2. Pop
3. Display Top Element
4. Exit
Enter your choice: 1
Enter value to push: 20

Stack Operations Menu:


1. Push
2. Pop
3. Display Top Element
4. Exit
Enter your choice: 3
Top element: 20

Stack Operations Menu:


1. Push
2. Pop
3. Display Top Element
4. Exit
Enter your choice: 2
Popped element: 20

Stack Operations Menu:


1. Push
2. Pop
3. Display Top Element
4. Exit
Enter your choice: 3
Top element: 10

Stack Operations Menu:


1. Push
2. Pop
3. Display Top Element
4. Exit
Enter your choice: 4
Exiting...
LAB REPORT
Data Structures Laboratory
Course Code : CSE 156

Topic: Lab 6 - Queue

Submitted To
Dr. Md. Golam Moazzam, B.Sc(JU), MS(JU), PhD(JU)
Professor, Department of Computer Science and Engineering
Jahangirnagar University

Submitted By
Student Name: Jubair Munshi
Student ID No: 331
Registration no: 20230657066
Course Title: Data Structures Laboratory
Course Code: CSE-156
Submission Date: 18.11.2024
C++ Code:
#include <iostream>
using namespace std;

#define MAX 100

class Queue {
int front, rear, arr[MAX];

public:
Queue() {
front = rear = -1;
}

bool enqueue(int x) {
if (rear == MAX - 1) {
cout << "Queue Overflow\n";
return false;
}
if (front == -1) front = 0;
arr[++rear] = x;
return true;
}

bool dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow\n";
return false;
}
cout << "Dequeued element: " << arr[front++] << endl;
return true;
}

void displayFront() {
if (front == -1 || front > rear) {
cout << "Queue is empty\n";
} else {
cout << "Front element: " << arr[front] << endl;
}
}
};

int main() {
Queue queue;
int choice, value;

do {
cout << "\nQueue Operations Menu:\n";
cout << "1. Enqueue\n2. Dequeue\n3. Display Front Element\n4. 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:
queue.displayFront();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 4);

return 0;
}

Output:
Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Front Element
4. Exit
Enter your choice: 1
Enter value to enqueue: 10

Queue Operations Menu:


1. Enqueue
2. Dequeue
3. Display Front Element
4. Exit
Enter your choice: 1
Enter value to enqueue: 20

Queue Operations Menu:


1. Enqueue
2. Dequeue
3. Display Front Element
4. Exit
Enter your choice: 3
Front element: 10

Queue Operations Menu:


1. Enqueue
2. Dequeue
3. Display Front Element
4. Exit
Enter your choice: 2
Dequeued element: 10
Queue Operations Menu:
1. Enqueue
2. Dequeue
3. Display Front Element
4. Exit
Enter your choice: 4
Exiting...
LAB REPORT
Data Structures Laboratory
Course Code : CSE 156

Topic: Lab 7 - Tree

Submitted To
Dr. Md. Golam Moazzam, B.Sc(JU), MS(JU), PhD(JU)
Professor, Department of Computer Science and Engineering
Jahangirnagar University

Submitted By
Student Name: Jubair Munshi
Student ID No: 331
Registration no: 20230657066
Course Title: Data Structures Laboratory
Course Code: CSE-156
Submission Date: 18.11.2024
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* left;
Node* right;

Node(int value) : data(value), left(nullptr), right(nullptr) {}


};

class BinaryTree {
private:
Node* findMin(Node* root) {
while (root->left != nullptr) {
root = root->left;
}
return root;
}

public:
Node* root;

BinaryTree() : root(nullptr) {}

Node* insert(Node* root, int data) {


if (root == nullptr) return new Node(data);
if (data < root->data)
root->left = insert(root->left, data);
else
root->right = insert(root->right, data);
return root;
}

Node* deleteNode(Node* root, int data) {


if (root == nullptr) 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 == nullptr) {
Node* temp = root->right;
delete root;
return temp;
}
if (root->right == nullptr) {
Node* temp = root->left;
delete root;
return temp;
}
Node* temp = findMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}

bool search(Node* root, int data) {


if (root == nullptr) return false;
if (root->data == data) return true;
if (data < root->data) return search(root->left, data);
return search(root->right, data);
}

void inorder(Node* root) {


if (root != nullptr) {
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}

void preorder(Node* root) {


if (root != nullptr) {
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node* root) {


if (root != nullptr) {
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
}
};

int main() {
BinaryTree tree;
int choice, value;

do {
cout << "\nTree Operations Menu:\n";
cout << "1. Insert\n2. Delete\n3. Search\n4. Inorder Traversal\n";
cout << "5. Preorder Traversal\n6. Postorder Traversal\n7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
tree.root = tree.insert(tree.root, value);
break;
case 2:
cout << "Enter value to delete: ";
cin >> value;
tree.root = tree.deleteNode(tree.root, value);
break;
case 3:
cout << "Enter value to search: ";
cin >> value;
if (tree.search(tree.root, value)) {
cout << "Value found in the tree.\n";
} else {
cout << "Value not found in the tree.\n";
}
break;
case 4:
cout << "Inorder Traversal: ";
tree.inorder(tree.root);
cout << endl;
break;
case 5:
cout << "Preorder Traversal: ";
tree.preorder(tree.root);
cout << endl;
break;
case 6:
cout << "Postorder Traversal: ";
tree.postorder(tree.root);
cout << endl;
break;
case 7:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 7);

return 0;
}

Output:
Tree Operations Menu:
1. Insert
2. Delete
3. Search
4. Inorder Traversal
5. Preorder Traversal
6. Postorder Traversal
7. Exit
Enter your choice: 1
Enter value to insert: 1

Tree Operations Menu:


1. Insert
2. Delete
3. Search
4. Inorder Traversal
5. Preorder Traversal
6. Postorder Traversal
7. Exit
Enter your choice: 1
Enter value to insert: 2

Tree Operations Menu:


1. Insert
2. Delete
3. Search
4. Inorder Traversal
5. Preorder Traversal
6. Postorder Traversal
7. Exit
Enter your choice: 1
Enter value to insert: 3

Tree Operations Menu:


1. Insert
2. Delete
3. Search
4. Inorder Traversal
5. Preorder Traversal
6. Postorder Traversal
7. Exit
Enter your choice: 4
Inorder Traversal: 1 2 3

Tree Operations Menu:


1. Insert
2. Delete
3. Search
4. Inorder Traversal
5. Preorder Traversal
6. Postorder Traversal
7. Exit
Enter your choice: 5
Preorder Traversal: 1 2 3

Tree Operations Menu:


1. Insert
2. Delete
3. Search
4. Inorder Traversal
5. Preorder Traversal
6. Postorder Traversal
7. Exit
Enter your choice: 6
Postorder Traversal: 3 2 1

Tree Operations Menu:


1. Insert
2. Delete
3. Search
4. Inorder Traversal
5. Preorder Traversal
6. Postorder Traversal
7. Exit
Enter your choice: 7
Exiting...

You might also like