Data Structure Lab report
Data Structure Lab report
INDEX
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
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
cout << "Move disk " << n << " from " << from << " to " << to << endl;
int main()
{
int n;
cout << "Enter the number of disks: ";
cin >> n;
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
int main()
{
string text, toReplace, replacement;
cout << "Enter the text: ";
getline(cin, text);
return 0;
}
Source Code:
#include <iostream>
using namespace std;
bubbleSort(arr, n);
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
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
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];
}
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;
}
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;
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;
}
class Stack {
int top;
int arr[100];
int maxSize;
public:
Stack(int size) {
top = -1;
maxSize = size;
}
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);
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
class Queue
{
private:
int front, rear, size;
int* queue;
public:
Queue(int s) {
size = s;
queue = new int[size];
front = -1;
rear = -1;
}
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
Source Code:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = right = nullptr;
}
};
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
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