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

Data Structure Lab Manual

Uploaded by

mohit18gamer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Data Structure Lab Manual

Uploaded by

mohit18gamer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1.

Write a c++ program for Array Operations

#include <iostream>
using namespace std;

void printArray(int arr[], int size) {


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

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);

cout << "Array elements: ";


printArray(arr, n);

return 0;
}
2. Write a c++ program for Linked List Implementation

#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
};

void printList(Node* n) {
while (n != nullptr) {
cout << n->data << " ";
n = n->next;
}
}

int main() {
Node* head = nullptr;
Node* second = nullptr;
Node* third = nullptr;

// Allocate 3 nodes in the heap


head = new Node();
second = new Node();
third = new Node();

head->data = 1;
head->next = second;

second->data = 2;
second->next = third;

third->data = 3;
third->next = nullptr;

printList(head);
return 0;
}

3. Write a c++ program for Stack Using Array

#include <iostream>
using namespace std;

#define MAX 1000

class Stack {
int top;
public:
int arr[MAX];
Stack() { top = -1; }
bool push(int x);
int pop();
bool isEmpty();
};

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

int Stack::pop() {
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = arr[top--];
return x;
}
}

bool Stack::isEmpty() {
return (top < 0);
}

int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " popped from stack\n";
return 0;
}
4. Write a c++ program for Queue Using Array

#include <iostream>
using namespace std;

#define MAX 100

class Queue {
int front, rear;
public:
int arr[MAX];
Queue() { front = rear = -1; }
void enqueue(int x);
int dequeue();
bool isEmpty();
};

void Queue::enqueue(int x) {
if (rear == MAX - 1) {
cout << "Queue Overflow";
}
else {
if (front == -1) front = 0;
arr[++rear] = x;
cout << x << " enqueued to queue\n";
}
}

int Queue::dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow\n";
return 0;
}
else {
int x = arr[front++];
return x;
}
}

bool Queue::isEmpty() {
return (front == -1 || front > rear);
}

int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
cout << q.dequeue() << " dequeued from queue\n";
return 0;
}
5. Write a c++ program for Binary Search Algorithm

#include <iostream>
using namespace std;

int binarySearch(int arr[], int left, int right, int x) {


while (left <= right) {
int mid = left + (right - left) / 2;

if (arr[mid] == x)
return mid;

if (arr[mid] < x)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}

int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
if (result != -1)
cout << "Element is present at index " << result << endl;
else
cout << "Element is not present in array\n";
return 0;
}
6. Write a c++ program for Bubble Sort

#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]);
}

void printArray(int arr[], int size) {


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

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}
7. Write a c++ program for Selection Sort

#include <iostream>
using namespace std;

void selectionSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
swap(arr[minIndex], arr[i]);
}
}

void printArray(int arr[], int size) {


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

int main() {
int arr[] = {29, 10, 14, 37, 13};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}

8. Write a c++ program for Insertion Sort

#include <iostream>
using namespace std;

void insertionSort(int arr[], int n) {


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

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

void printArray(int arr[], int size) {


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

int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}

9. Write a c++ program for Merge Sort

#include <iostream>
using namespace std;

void merge(int arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];

for (int i = 0; i < n1; i++)


L[i] = arr[left + i];
for (int i = 0; i < n2; i++)
R[i] = arr[mid + 1 + i];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

void printArray(int arr[], int size) {


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

int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);

mergeSort(arr, 0, n - 1);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}

10. Write a c++ program for Quick Sort

#include <iostream>
using namespace std;

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = (low - 1);

for (int j = low; j < high; j++) {


if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
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);
}
}

void printArray(int arr[], int size) {


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

int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;}

11. Write a c++ program for Binary Tree Traversal (Inorder,


Preorder, Postorder)

#include <iostream>
using namespace std;

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

Node(int val) {
data = val;
left = right = nullptr;
}
};
void inorderTraversal(Node* root) {
if (root == nullptr)
return;

inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}

void preorderTraversal(Node* root) {


if (root == nullptr)
return;

cout << root->data << " ";


preorderTraversal(root->left);
preorderTraversal(root->right);
}

void postorderTraversal(Node* root) {


if (root == nullptr)
return;

postorderTraversal(root->left);
postorderTraversal(root->right);
cout << root->data << " ";}
int main() {
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);

cout << "Inorder traversal: ";


inorderTraversal(root);
cout << "\nPreorder traversal: ";
preorderTraversal(root);
cout << "\nPostorder traversal: ";
postorderTraversal(root);
return 0;
}

12. Write a c++ program for Depth First Search (DFS) for Graph

#include <iostream>
#include <list>
using namespace std;

class Graph {
int V;
list<int>* adj;
void DFSUtil(int v, bool visited[]);
public:
Graph(int V);
void addEdge(int v, int w);
void DFS(int v);
};

Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int v, int w) {


adj[v].push_back(w);
}

void Graph::DFSUtil(int v, bool visited[]) {


visited[v] = true;
cout << v << " ";

for (auto i = adj[v].begin(); i != adj[v].end(); ++i)


if (!visited[*i])
DFSUtil(*i, visited);
}

void Graph::DFS(int v) {
bool* visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;

DFSUtil(v, visited);
}
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

cout << "Depth First Traversal (starting from vertex 2):\n";


g.DFS(2);

return 0;
}

13. Write a c++ program for Breadth First Search (BFS) for Graph

#include <iostream>
#include <list>
using namespace std;

class Graph {
int V;
list<int>* adj;
public:
Graph(int V);
void addEdge(int v, int w);
void BFS(int s);
};

Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int v, int w) {


adj[v].push_back(w);
}

void Graph::BFS(int s) {
bool* visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;

list<int> queue;
visited[s] = true;
queue.push_back(s);

while (!queue.empty()) {
s = queue.front();
cout << s << " ";
queue.pop_front();

for (auto i = adj[s].begin(); i != adj[s].end(); ++i) {


if (!visited[*i]) {
visited[*i] = true;
queue.push_back(*i);
}
}
}
}
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

cout << "Breadth First Traversal (starting from vertex 2):\n";


g.BFS(2);

return 0;
}

You might also like