DAA File Print
DAA File Print
- 1
Aim: Write a program to implement push and pop operations on a queue using linked
list.
Code:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class Queue {
private:
Node* front;
Node* rear;
public:
Queue() {
front = rear = nullptr;
}
void pop() {
if (front == nullptr) {
cout << "Queue is empty" << endl;
return;
}
Node* temp = front;
front = front->next;
if (front == nullptr) {
rear = nullptr;
}
delete temp;
}
void display() {
if (front == nullptr) {
cout << "Queue is empty" << endl;
return;
}
Node* temp = front;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
int main() {
Queue q;
q.push(10);
q.push(20);
q.push(30);
q.pop();
cout << "Queue after one pop: ";
q.display();
return 0;
}
Output :
Practical No. - 14
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
return 0;
}
Output :
Practical No. - 15
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
return 0;
}
Output :
Practical No. - 16
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
return 0;
}
Output :
Practical No. - 17
Aim: Write a program to sort an array of integers in ascending order using quick sort.
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 arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
Output :
Practical No. - 18
Aim: Write a program to traverse a Binary search tree in Pre-order, In-order and Post-
order.
return 0;
}
Output :
Practical No. - 19
#include <iostream>
#include <list>
#include <queue>
using namespace std;
class Graph {
int V;
list<int>* adj;
public:
Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
queue<int> q;
visited[start] = true;
q.push(start);
int main() {
Graph g(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.addEdge(3, 4);
return 0;
}
Practical No. - 20
class Graph {
int V;
list<int>* adj;
public:
Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
queue<int> q;
visited[start] = true;
q.push(start);
while (!q.empty()) {
delete[] visited;
}
~Graph() {
delete[] adj;
}
};
int main() {
Graph g(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.addEdge(3, 4);
return 0;
}
Output :