Data Structure Lab Manual
Data Structure Lab Manual
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
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;
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = nullptr;
printList(head);
return 0;
}
#include <iostream>
using namespace std;
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;
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;
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;
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;
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;
}
#include <iostream>
using namespace std;
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
#include <iostream>
using namespace std;
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++;
}
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;
}
#include <iostream>
using namespace std;
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;}
#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);
}
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);
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::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);
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::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();
return 0;
}