C++ Lab Manual: Data Structures & Algorithms
C++ Lab Manual: Data Structures & Algorithms
SEMESTER N0: 3
DEPARTMENT OF CYBER
SECURITY
LAB TAK 1: Basic Programming Practices
Part 1: Operators
1. Write a program to take two numbers as input and display:
2. Sum, difference, product, division, modulus
3. Compare which number is greater/smaller
4. Check if both numbers are even using logical operators
Answer:
#include
<iostream> using
namespace std;
int main() {
int a, b;
cout << "Enter two
b;
b << endl;
if (b != 0) {
cout << "Division: " << (float)a / b
<< endl; cout << "Modulus: " <<
a % b << endl;
} else {
cout << "Cannot divide by zero." << endl;
}
if (a > b)
cout << a << " is greater than " << b
if (a % 2 == 0 CC b % 2 == 0)
cout << "Both numbers are even."
endl; return 0;
}
OUTPUT:
Part 2: Loops:
1. Write a program using for loop to print multiplication table of a given
number.
Answer:
#include
<iostream> using
namespace std;
int main()
{ int
num;
num;
return 0;
OUPUT:
2. }Write a program using while loop to calculate factorial of a number.
Answer:
#include<iostrea
m> using
namespace std;
int main(){
fact = fact * i;
i++;
}
cout<<"Factorial:
"<<fact<<endl; return 0;
}
OUPUT:
3. Write a program using do-while loop to repeatedly ask user for a
number until they enter 0.
Answer:
#include
<iostream> using
namespace std;
int
main()
{ int
num;
do {
}
OUPUT:
Part 3: Functions
<iostream> using
namespace std;
void
checkPrime(int n)
{ int cnt = 0;
if (n <= 1) {
cout << n << " is NOT
prime"; return;
i++) { if (n % i
== 0)
cnt++;
}
if (cnt == 2)
cout << n << " is
prime"; else
{ int
num;
num;
checkPrime(num);
return 0;
}
OUPUT:
2. Write a function that takes a number and returns its factorial.
Answer:
#include<iostrea
m> using
namespace std;
void
findFact(){ int
n , fact=1 , i=1;
cout<<"Enter number:
fact = fact * i;
i++;
}
cout<<"Factorial: "<<fact<<endl;
};
int
main(){
findFact(
); return
0;
}
OUPUT:
3. Write a function that takes two numbers and returns the greater number.
Answer:
#include
<iostream> using
namespace std;
b) { if (a > b) {
return a;
} else {
return
b;}
}
int main()
{ int x,
y;
<iostream> using
namespace std;
int main() {
const int days = 7;
double temperatures[days];
double sum = 0.0,
average; double
i++) {
sum += temperatures[i];
}
cout << "Temperatures for the
i++) {
cout << temperatures[i] << " "<< endl;
}
hottest = coldest =
if (temperatures[i] >
hottest) { hottest =
temperatures[i];
}
if (temperatures[i] < coldest)
{ coldest =
temperatures[i];
}
}
average = sum / days;
cout << "Average temperature of the week: " << average << endl;
return 0;
}
<iostream> using
namespace std;
int main() {
int arr[10], i, j, count;
cout << "Enter 10
numbers:\n"; for (i = 0; i
cout <<
"\nFrequencies:\n"; for
count = 1;
if (arr[i] != -1) {
for (j = i + 1; j < 10;
j++) { if (arr[i] ==
arr[j]) {
count++;
arr[j] = -1;
}
}
cout << arr[i] << " occurs " << count << " time(s)\n";
}
}
return 0;
}
Task 3: Reverse an
program to:
<iostream> using
namespace std;
int
main()
{ int
arr[8];
0; i < 8; i++) {
cin >> arr[i];
}
i >= 0; i--) {
return 0;
}
<iostream> using
namespace std;
int main()
{ int N;
arr[i];
= 0; i < N; i++) {
if (arr[i] % 2 ==
0) { cout <<
}
}
0; i < N; i++) {
if (arr[i] % 2 !=
0) { cout <<
}
}
return 0;
}
Lab 03
2D Arrays
Traversal→ visiting each element.
Insertion→ Adding a new element.
Deletion→ Removing a new element.
Searching→ Finding an element.
Sorting→ Arranging Elements.
Algorithm:
A step-by-step procedure to solve a problem in a finite amount of time.
Example:
Adding 2 numbers
Complexity Analysis
Time Complexity
How much time an algorithm takes as input size grows.
Expressed as number of steps, not actual seconds.
Space Complexity
How much memory an algorithm uses.
1. Linear Time Complexity – O(n)
An algorithm has linear time complexity if the time it takes increases
directly in proportion to the input size.
If input doubles → the number of steps also doubles.
sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
2. Quadratic Time Complexity – O(n²)
An algorithm has quadratic time complexity if the time taken is
proportional to the square of the input size.
If input doubles → the number of steps becomes four times.
Bubble sort:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Some operation
}
}
Traversal:
#include<iostream>
using namespace std;
int main(){
int arr[2][3]={{1,2,3},{4,5,6}};
for(int i=0;i<2;i++){
for(int j=0;j<3;j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
return 0;
}
Lab 03
2D Arrays
Traversal→ visiting each element.
Insertion→ Adding a new element.
Deletion→ Removing a new element.
Searching→ Finding an element.
Sorting→ Arranging Elements.
Algorithm:
A step-by-step procedure to solve a problem in a finite amount of time.
Example:
Adding 2 numbers
Complexity Analysis
Time Complexity
How much time an algorithm takes as input size grows.
Expressed as number of steps, not actual seconds.
Space Complexity
How much memory an algorithm uses.
2. Linear Time Complexity – O(n)
An algorithm has linear time complexity if the time it takes increases
directly in proportion to the input size.
If input doubles → the number of steps also doubles.
sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
2. Quadratic Time Complexity – O(n²)
An algorithm has quadratic time complexity if the time taken is
proportional to the square of the input size.
If input doubles → the number of steps becomes four times.
Bubble sort:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Some operation
}
}
Traversal:
#include<iostream>
using namespace std;
int main(){
int arr[2][3]={{1,2,3},{4,5,6}};
for(int i=0;i<2;i++){
for(int j=0;j<3;j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
return 0;
}
Output
Insertion:
#include<iostream>
using namespace std;
int main(){
int arr[2][3]={{1,2,3},{4,5,6}};
int r,c,v;
cout<<"Enter the row coulome and value";
cin>>r>>c>>v;
arr[r][c]=v;
for(int i=0;i<2;i++){
for(int j=0;j<3;j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
return 0;
}
Output
Deleting
#include<iostream>
using namespace std;
int main(){
int arr[2][3]={{1,2,3},{4,5,6}},r,c;
cin>>r>>c;
arr[r][c]=0;
for(int i=0;i<2;i++){
for(int j=0;j<3;j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
return 0;
}
Output
Searching
#include<iostream>
using namespace std;
int main(){
int arr[2][3]={{1,2,3},{4,5,6}};
int x;
int f=0;
Output:
Sorting
#include<iostream>
using namespace std;
int main(){
int arr[2][3]={{5,2,8},{1,7,3}};
int temp;
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
for(int k=0;k<2;k++){
for(int l=0;l<3;l++){
if(arr[i][j]<arr[k][l]){
temp=arr[i][j];
arr[i][j]=arr[k][l];
arr[k][l]=temp;
}
}
}
}
}
Output
Output
2- Write a C++ program to count the number of even and odd elements in a
2D array.
Requirements:
Take rows & columns from the user.
Input elements into a 2D array.
Count how many even and odd numbers are there.
Print the counts.
#include<iostream>
using namespace std;
int main(){
int r,c;
cout<<"Enter rows: ";
cin>>r;
cout<<"Enter columns: ";
cin>>c;
int arr[50][50];
cout<<"Enter elements:\n";
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin>>arr[i][j];
}
}
int even=0,odd=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(arr[i][j]%2==0)
even++;
else
odd++;
}
}
cout<<"Even count: "<<even<<endl;
cout<<"Odd count: "<<odd<<endl;
return 0;
}
Output
Lab 04
Implementation of Single linked list & operations
Singly linked list :
A singly linked list is the simplest kind of linked lists.
It takes up less space in memory because each node has only one address to
the next node, like in the image below.
Applications of Arrays in Real Life:
1. Music & Video Playlists
Benefit:
➢ Easy to move to the next or previous song/video.
➢ Songs can be added or removed without shifting all others like
in arrays.
2. Undo/Redo Operations in Text Editors
Example: In MS Word or Notepad, each action is stored in a linked list of
states.
Benefit:
➢ Pressing Ctrl+Z moves to the previous state (undo).
➢ Pressing Ctrl+Y moves forward (redo).
Operations performed on arrays :
Basic Structure + display→ display list.
Insertion → Adding a new element.
Deletion→ Removing a new element.
Searching→ Finding an element.
Reversing→ Reverse Elements.
Basic Structure & Traversal
#include<iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
int main(){
Node* head=nullptr;
Node* first=nullptr;
Node* second=nullptr;
Node* third=nullptr;
head=new Node();
first=new Node();
second=new Node();
third=new Node();
head->data=5;
head->next=first;
first->data=10;
first->next=second;
second->data=15;
second->next=third;
third->data=20;
third->next=nullptr;
Node* a=head;
while(a!=nullptr){
cout<<a->data<<endl;
a=a->next;
}
return 0;
}
Output:
Output
Output:
#define MAX 5
class Stack {
int arr[MAX];
int top;
public:
Stack() {
top = -1;
}
void display() {
if (top < 0) {
cout << "Stack is empty!" << endl;
} else {
cout << "Stack elements are: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main() {
Stack s;
[Link]();
return 0;
}
Insertion
ANSWER:
#include<iostream>
using namespace std;
#define MAX 5
class Stack {
int arr[MAX];
int top;
public:
Stack() {
top = -1;
}
void insertion(int value) {
if (top >= MAX - 1) {
cout << "Stack Overflow! Cannot push " << value << endl;
} else {
arr[++top] = value;
cout << value << " pushed into stack.\n";
}
}
void display() {
if (top < 0) {
cout << "Stack is empty!\n";
} else {
cout << "Stack elements are: ";
for (int i = top; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
}
}
};
int main() {
Stack s;
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
[Link](60);
[Link]();
return 0;
}
Deletion
ANSWER:
#include <iostream>
using namespace std;
#define MAX 5
class Stack {
int arr[MAX];
int top;
public:
Stack() {
top = -1;
}
void insertion(int value) {
if (top >= MAX - 1) {
cout << "Stack overflow! Cannot push " << value << endl;
} else {
arr[++top] = value;
cout << value << " pushed into stack." << endl;
}
}
void deletion() {
if (top < 0) {
cout << "Stack underflow! Cannot pop." << endl;
} else {
cout << arr[top--] << " popped from stack." << endl;
}
}
void display() {
if (top < 0) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack elements are:" << endl;
for (int i = top; i >= 0; i--) {
cout << arr[i] << endl;
}
}
}
};
int main() {
Stack s;
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link]();
return 0;
}
Searching
ANSWER:
#include <iostream>
using namespace std;
#define MAX 5
class Stack {
int arr[MAX];
int top;
public:
Stack() {
top = -1;
}
void deletion() {
if (top < 0) {
cout << "Stack underflow! Cannot pop." << endl;
} else {
cout << arr[top--] << " popped from stack." << endl;
}
}
void searching(int value)
{
if (top < 0)
{
cout << "Stack is empty." << endl;
return;
}
bool found = false;
for (int i = top; i >= 0; i--)
{
if (arr[i] == value)
{
cout << "Element " << value << " found at position "
<< (top - i + 1) << " from top." << endl;
found = true;
break;
}
}
if (!found){
cout << "Element " << value << " not found in stack." << endl;
}
}
void display() {
if (top < 0) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack elements are:" << endl;
for (int i = top; i >= 0; i--) {
cout << arr[i] << endl;
}
}
}
};
int main() {
Stack s;
[Link](10);
[Link](20);
[Link](30);
[Link](20);
[Link]();
[Link]();
return 0;
}
Sorting
ANSWER:
#include <iostream>
using namespace std;
#define MAX 5
class Stack {
int arr[MAX];
int top;
public:
Stack() {
top = -1;
}
void deletion() {
if (top < 0) {
cout << "Stack underflow! Cannot pop." << endl;
} else {
cout << arr[top--] << " popped from stack." << endl;
}
}
void sortStack()
{
if (top < 0)
{
cout << "Stack is empty. Nothing to sort." << endl;
return;
}
for (int i = 0; i <= top; i++)
{
for (int j = 0; j < top - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
void display() {
if (top < 0) {
cout << "Stack is empty." << endl;
} else {
cout << "Stack elements are:" << endl;
for (int i = top; i >= 0; i--) {
cout << arr[i] << endl;
}
}
}
};
int main() {
Stack s;
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link]();
return 0;
}
LAB TASK 6: Linear Queue and Circular Queue Implementation
using Arrays
CLASSWORK:
Queue Operations:
1. Display
ANSWER:
#include<iostream>
using namespace std;
#define SIZE 5
class LinearQueue{
int front ; int end; int arr[SIZE];
public:
LinearQueue(){
front = -1; end = -1;
}
void display(){
if (front ==-1 || front >end){
cout<<"Queue is empty "<<endl;
}
else{
cout<<"Queue elements are: "<<endl;
for(int i = front; i<=end;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
}
};
int main(){
LinearQueue que;
[Link]();
return 0;
}
2. Insertion
ANSWER:
#include<iostream>
using namespace std;
#define SIZE 5
class LinearQueue{
int front ; int end; int arr[SIZE];
public:
LinearQueue(){
front = -1; end = -1;
}
void insertion(int val)
{
if (end == SIZE - 1)
{
cout << "queue overflow" << endl;
}
else
{
if (front == -1)
{
front = 0;
arr[++end] = val;
cout << val << " inserted in queue successfully" << endl;
}
else
{
arr[++end] = val;
cout << val << " inserted in queue successfully" << endl;
}
}
}
void display(){
if (front ==-1 || front >end){
cout<<"Queue is empty "<<endl;
}
else{
cout<<"Queue elements are: "<<endl;
for(int i = front; i<=end;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
}
};
int main(){
LinearQueue que;
[Link](70);
[Link](20);
[Link](40);
[Link](50);
[Link]();
return 0;
}
3. Deletion
ANSWER:
#include<iostream>
using namespace std;
#define SIZE 5
class LinearQueue{
int front ; int end; int arr[SIZE];
public:
LinearQueue(){
front = -1; end = -1;
}
void insertion(int val)
{
if (end == SIZE - 1)
{
cout << "queue overflow" << endl;
}
else
{
if (front == -1)
{
front = 0;
arr[++end] = val;
cout << val << " inserted in queue successfully" << endl;
}
else
{
arr[++end] = val;
cout << val << " inserted in queue successfully" << endl;
}
}
}
void deletion()
{
if (front == -1 || front > end)
{
cout << "queue underflow" << endl;
}
else
{
int del = arr[front++];
cout << del << " is deleted successfully" << endl;
}
}
void display(){
if (front ==-1 || front >end){
cout<<"Queue is empty "<<endl;
}
else{
cout<<"Queue elements are: "<<endl;
for(int i = front; i<=end;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
}
};
int main()
{
LinearQueue que;
[Link](70);
[Link](20);
[Link](40);
[Link](50);
[Link]();
[Link]();
[Link]();
return 0;
}
4. Searching
ANSWER:
#include<iostream>
using namespace std;
#define SIZE 5
class LinearQueue{
int front ; int end; int arr[SIZE];
public:
LinearQueue(){
front = -1; end = -1;
}
void insertion(int val)
{
if (end == SIZE - 1)
{
cout << "queue overflow" << endl;
}
else
{
if (front == -1)
{
front = 0;
arr[++end] = val;
cout << val << " inserted in queue successfully" << endl;
}
else
{
arr[++end] = val;
cout << val << " inserted in queue successfully" << endl;
}
}
}
void deletion()
{
if (front == -1 || front > end)
{
cout << "queue underflow" << endl;
}
else
{
int del = arr[front++];
cout << del << " is deleted successfully" << endl;
}
}
void searching(int val)
{
for (int i = front; i <= end; i++)
{
if (arr[i] == val)
{
cout << val << " found at position " << i - front + 1 << endl;
return;
}
}
void display(){
if (front ==-1 || front >end){
cout<<"Queue is empty "<<endl;
}
else{
cout<<"Queue elements are: "<<endl;
for(int i = front; i<=end;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
}
};
int main()
{
LinearQueue que;
[Link](70);
[Link](20);
[Link](40);
[Link](50);
[Link]();
[Link]();
[Link]();
[Link](20);
return 0;
}
5. Sorting
ANSWER:
#include<iostream>
using namespace std;
#define SIZE 5
class LinearQueue{
int front ; int end; int arr[SIZE];
public:
LinearQueue(){
front = -1; end = -1;
}
void insertion(int val)
{
if (end == SIZE - 1)
{
cout << "queue overflow" << endl;
}
else
{
if (front == -1)
{
front = 0;
arr[++end] = val;
cout << val << " inserted in queue successfully" << endl;
}
else
{
arr[++end] = val;
cout << val << " inserted in queue successfully" << endl;
}
}
}
void sorting()
{
if (front == -1 || front > end)
{
cout << "Queue is empty. Nothing to sort." << endl;
return;
}
int main(){
LinearQueue que;
[Link](70);
[Link](20);
[Link](40);
[Link](50);
[Link]();
[Link]();
return 0;
}
6. Display in Circular Queue:
ANSWER:
#include <iostream>
using namespace std;
#define SIZE 5
class Circularqueue{
int front;
int end;
int arr[SIZE];
public:
Circularqueue() {
front = -1;
end = -1;
}
void display(){
if (front == -1)
cout << "Queue is empty" << endl;
else{
cout << "Queue elements: " << endl;
int i = front;
while (true){
cout << arr[i] << " ";
if (i == end)
break;
i = (i + 1) % SIZE;
}
cout << endl;
}
}
};
int main(){
Circularqueue que;
[Link]();
return 0;
}
void display(){
if (front == -1)
cout << "Queue is empty" << endl;
else{
cout << "Queue elements: " << endl;
int i = front;
while (true){
cout << arr[i] << " ";
if (i == end)
break;
i = (i + 1) % SIZE;
}
cout << endl;
}
}
};
int main(){
Circularqueue que;
[Link]();
[Link](40);
[Link](50);
[Link](10);
[Link](60);
[Link](80);
[Link]();
return 0;
}
void deletion()
{
if (front == -1)
cout << "Queue Underflow" << endl;
else
{
cout << arr[front] << " deleted" << endl;
if (front == end)
front = end = -1;
else
front = (front + 1) % SIZE;
}
}
void display(){
if (front == -1)
cout << "Queue is empty" << endl;
else{
cout << "Queue elements: " << endl;
int i = front;
while (true){
cout << arr[i] << " ";
if (i == end)
break;
i = (i + 1) % SIZE;
}
cout << endl;
}
}
};
int main(){
Circularqueue que;
[Link]();
[Link](40);
[Link](50);
[Link](10);
[Link](60);
[Link](80);
[Link]();
[Link]();
[Link]();
return 0;
}
Home Tasks:
1. Add a search() function that checks whether a given element
exists in the circular queue.
ANSWER:
#include <iostream>
using namespace std;
class CircularQueue {
private:
int *arr;
int front, rear, capacity, currentSize;
public:
CircularQueue(int size) {
capacity = size;
arr = new int[capacity];
front = 0;
rear = -1;
currentSize = 0;
}
void display() {
if (isEmpty()) return;
int index = front;
cout << "Queue: ";
for (int i = 0; i < currentSize; i++) {
cout << arr[index] << " ";
index = (index + 1) % capacity;
}
cout << endl;
}
};
int main() {
CircularQueue q(5);
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link]();
[Link]();
key = 60;
if ([Link](key)) {
cout << "Element " << key << " found in the queue.\n";
}
return 0;
}
2. Add a sortQueue() function that sorts the circular queue in
ascending order.
ANSWER:
#include <iostream>
using namespace std;
class CircularQueue {
private:
int *arr;
int front, rear, capacity, currentSize;
public:
CircularQueue(int size) {
capacity = size;
arr = new int[capacity];
front = 0;
rear = -1;
currentSize = 0;
}
~CircularQueue() {
delete[] arr;
}
int main() {
int size = 5;
CircularQueue cq(size);
[Link](45);
[Link](12);
[Link](78);
[Link](3);
[Link](25);
[Link]();
[Link]();
[Link]();
if ([Link](3))
cout << "Found 3 in the queue." << endl;
return 0;
}
class PriorityQueue{
int arr[size];
int priority[size];
int count;
public:
PriorityQueue(){
count = 0;
}
void display()
{
if (count == 0){
cout << "queue is empty" << endl;
}
else{
cout << "Elements\tPriority\n";
for (int i = 0; i < count; i++){
cout << arr[i] << "\t\t" << priority[i] << endl;
}
}
}
};
int main(){
PriorityQueue pp;
[Link]();
return 0;
}
➢ Insertion
ANSWER:
#include <iostream>
using namespace std;
#define size 5
class PriorityQueue{
int arr[size];
int priority[size];
int count;
public:
PriorityQueue(){
count = 0;
}
//Insertion
void insertion(int val, int p)
{
if (count == size)
{
cout << "Overflow occure" << endl;
return;
}
else
{
arr[count] = val;
priority[count] = p;
count++;
cout << val << " inserted with priority " << p << endl;
}
}
void display()
{
if (count == 0){
cout << "queue is empty" << endl;
}
else{
cout << "Elements\tPriority\n";
for (int i = 0; i < count; i++){
cout << arr[i] << "\t\t" << priority[i] << endl;
}
}
}
};
int main(){
PriorityQueue pp;
[Link]();
[Link](10,1);
[Link](40,4);
[Link](30,5);
[Link](20,2);
[Link](60,3);
}
➢ Deletion
ANSWER:
#include <iostream>
using namespace std;
#define size 5
class PriorityQueue{
int arr[size];
int priority[size];
int count;
public:
PriorityQueue(){
count = 0;
}
//Inserion
void insertion(int val, int p)
{
if (count == size)
{
cout << "Overflow occure" << endl;
return;
}
else
{
arr[count] = val;
priority[count] = p;
count++;
cout << val << " inserted with priority " << p << endl;
}
}
// deletion
void deletion()
{
if (count == 0)
{
cout << "under occure" << endl;
return;
}
cout << arr[0] << " deleted from " << priority[0] << endl;
int main(){
PriorityQueue pp;
[Link]();
[Link](10,1);
[Link](40,4);
[Link](30,5);
[Link](20,2);
[Link](60,3);
[Link]();
[Link]();
return 0;
}
➢ Searching
ANSWER:
#include <iostream>
using namespace std;
#define size 5
class PriorityQueue{
int arr[size];
int priority[size];
int count;
public:
PriorityQueue(){
count = 0;
}
//Inserion
void insertion(int val, int p)
{
if (count == size)
{
cout << "Overflow occure" << endl;
return;
}
else
{
arr[count] = val;
priority[count] = p;
count++;
cout << val << " inserted with priority " << p << endl;
}
}
// deletion
void deletion()
{
if (count == 0)
{
cout << "under occure" << endl;
return;
}
cout << arr[0] << " deleted from " << priority[0] << endl;
for (int i = 0; i < count - 1; i++)
{
arr[i] = arr[i + 1];
priority[i] = priority[i + 1];
}
count--;
}
// searching
void searching(int val){
bool found=false;
for(int i=0;i<count;i++){
if(arr[i]==val){
cout<<val<<" with priority "<<priority[i]<<" found at position
"<<i+1<<endl;
found=true;
return;
}
}
if(!found){
cout<<" value not found"<<endl;
}
}
void display()
{
if (count == 0){
cout << "queue is empty" << endl;
}
else{
cout << "Elements\tPriority\n";
for (int i = 0; i < count; i++){
cout << arr[i] << "\t\t" << priority[i] << endl;
}
}
}
};
int main(){
PriorityQueue pp;
[Link]();
[Link](10,1);
[Link](40,4);
[Link](30,5);
[Link](20,2);
[Link](60,3);
[Link]();
[Link]();
[Link](30);
return 0;
}
➢ Sorting
ANSWER:
#include <iostream>
using namespace std;
#define size 5
class PriorityQueue{
int arr[size];
int priority[size];
int count;
public:
PriorityQueue(){
count = 0;
}
//Inserion
void insertion(int val, int p)
{
if (count == size)
{
cout << "Overflow occure" << endl;
return;
}
else
{
arr[count] = val;
priority[count] = p;
count++;
cout << val << " inserted with priority " << p << endl;
}
}
// deletion
void deletion()
{
if (count == 0)
{
cout << "under occure" << endl;
return;
}
cout << arr[0] << " deleted from " << priority[0] << endl;
// swap data
int tempD = arr[i];
arr[i] = arr[j];
arr[j] = tempD;
}
}
}
int main(){
PriorityQueue pp;
[Link]();
[Link](10,1);
[Link](40,4);
[Link](30,5);
[Link](20,2);
[Link](60,3);
[Link]();
[Link]();
[Link]();
return 0;
}
1) Display double linkedlist:
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
int priority;
Node* prev;
Node* next;
public:
DoublyLinkedList() {
head = nullptr;
count = 0;
}
void sorting() {
if (head == nullptr) return;
// swap data
int tempD = iNode->data;
iNode->data = jNode->data;
jNode->data = tempD;
}
jNode = jNode->next;
}
iNode = iNode->next;
}
int main() {
DoublyLinkedList dll;
[Link](10, 3);
[Link](20, 1);
[Link](30, 4);
[Link](40, 2);
return 0;
}
2) Display circular linkedlist:
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
class CircularLinkedList {
Node* head;
public:
CircularLinkedList() {
head = nullptr;
}
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (head == nullptr) {
head = newNode;
newNode->next = head;
return;
}
Node* temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = newNode;
newNode->next = head;
}
void display() {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
cout << "Circular Linked List: ";
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
CircularLinkedList cll;
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link]();
return 0;
}
LAB TASK 8: Double LinkedList operations
➢ Display double LinkedList:
ANSWER:
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *prev;
Node *next;
Node(int val)
{
data = val;
prev = nullptr;
next = nullptr;
}
};
class DoublyLinkedList {
public:
Node *head;
DoublyLinkedList()
{
head = nullptr;
}
// display Forward
void displayForward()
{
Node *temp = head;
cout << "Forward: ";
while (temp != nullptr)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main()
{
DoublyLinkedList dll;
[Link]();
return 0;
}
➢ Insert at start in double LinkedList:
ANSWER:
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *prev;
Node *next;
Node(int val)
{
data = val;
prev = nullptr;
next = nullptr;
}
};
class DoublyLinkedList {
public:
Node *head;
DoublyLinkedList()
{
head = nullptr;
}
void insertAtBeginning(int val)
{
Node* newNode = new Node(val);
if (head == nullptr)
{
head = newNode;
return;
}
newNode->next = head;
head->prev = newNode;
head = newNode;
}
// display Forward
void displayForward()
{
Node *temp = head;
cout << "Forward: ";
while (temp != nullptr)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main()
{
DoublyLinkedList dll;
[Link](10); [Link](20);
[Link]();
return 0;
}
➢ Insert at end in double LinkedList:
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int val) {
data = val;
prev = nullptr;
next = nullptr;
}
};
class DoublyLinkedList {
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
}
int main() {
DoublyLinkedList dll;
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link]();
return 0;
}
➢ Insert at specific position in double LinkedList:
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int val) {
data = val;
prev = nullptr;
next = nullptr;
}
};
class DoublyLinkedList {
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
}
if (temp == nullptr)
{
cout << "Node not found!\n";
return;
}
if (temp->next != nullptr)
temp->next->prev = newNode;
temp->next = newNode;
}
void displayForward() {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
cout << "Doubly Linked List : ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
DoublyLinkedList dll;
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](10,40);
[Link]();
return 0;
}
➢ Delete from start in double LinkedList:
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int val) {
data = val;
prev = nullptr;
next = nullptr;
}
};
class DoublyLinkedList {
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
}
if (temp == nullptr)
{
cout << "Node not found!\n";
return;
}
if (temp->next != nullptr)
temp->next->prev = newNode;
temp->next = newNode;
}
void deleteFromBeginning()
{
if (head == nullptr)
{
cout << "List is empty!\n";
return;
}
if (head != nullptr)
head->prev = nullptr;
delete temp;
}
void displayForward() {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
cout << "Doubly Linked List : ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
DoublyLinkedList dll;
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](10,40);
[Link]();
[Link]();
return 0;
}
➢ Delete from end in double LinkedList:
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int val) {
data = val;
prev = nullptr;
next = nullptr;
}
};
class DoublyLinkedList {
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
}
if (temp->next != nullptr)
temp->next->prev = newNode;
temp->next = newNode;
}
void deleteFromBeginning()
{
if (head == nullptr)
{
cout << "List is empty!\n";
return;
}
if (head != nullptr)
head->prev = nullptr;
delete temp;
}
void deleteFromEnd()
{
if (head == nullptr)
{
cout << "List is empty!\n";
return;
}
Node* temp = head;
int main() {
DoublyLinkedList dll;
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](10,40);
[Link]();
[Link]();
[Link]();
return 0;
}
➢ Delete from specific position in double LinkedList:
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int val) {
data = val;
prev = nullptr;
next = nullptr;
}
};
class DoublyLinkedList {
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
}
// Function to insert node at end
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (head == nullptr) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
void insertAfter(int key, int val)
{
Node* temp = head;
while (temp != nullptr && temp->data != key)
temp = temp->next;
if (temp == nullptr)
{
cout << "Node not found!\n";
return;
}
if (temp->next != nullptr)
temp->next->prev = newNode;
temp->next = newNode;
}
void deleteFromBeginning()
{
if (head == nullptr)
{
cout << "List is empty!\n";
return;
}
if (head != nullptr)
head->prev = nullptr;
delete temp;
}
void deleteFromEnd()
{
if (head == nullptr)
{
cout << "List is empty!\n";
return;
}
delete temp;
}
void displayForward() {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
cout << "Doubly Linked List : ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
DoublyLinkedList dll;
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](10,40);
[Link]();
[Link]();
[Link](10);
[Link]();
return 0;
}
➢ Searching in double LinkedList:
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int val) {
data = val;
prev = nullptr;
next = nullptr;
}
};
class DoublyLinkedList {
Node* head;
public:
DoublyLinkedList() {
head = nullptr;
}
if (temp == nullptr)
{
cout << "Node not found!\n";
return;
}
if (temp->next != nullptr)
temp->next->prev = newNode;
temp->next = newNode;
}
void deleteFromBeginning()
{
if (head == nullptr)
{
cout << "List is empty!\n";
return;
}
if (head != nullptr)
head->prev = nullptr;
delete temp;
}
void deleteFromEnd()
{
if (head == nullptr)
{
cout << "List is empty!\n";
return;
}
delete temp;
}
void search(int key)
{
Node* temp = head;
int pos = 1;
int main() {
DoublyLinkedList dll;
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](10,40);
[Link]();
[Link]();
[Link](10);
[Link](40);
[Link]();
return 0;
}
LAB TASK -9: Circular LinkedList operations
➢ Display
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
class CircularLinkedList {
Node* head;
public:
CircularLinkedList() {
head = nullptr;
}
void display() {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
cout << "Circular Linked List: ";
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
CircularLinkedList cl1;
[Link]();
return 0;
}
➢ Insertion
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
class CircularLinkedList {
Node* head;
public:
CircularLinkedList() {
head = nullptr;
}
int main() {
CircularLinkedList cl1;
[Link]();
[Link](5); [Link](15); [Link](20);
return 0;
}
➢ Deletion
ANSWER:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
class CircularLinkedList {
Node* head;
public:
CircularLinkedList() {
head = nullptr;
}
if (current == head)
cout << "Value not found!" << endl;
else {
prev->next = current->next;
delete current;
cout << "Deleted " << val << endl;
}
}
void display() {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
cout << "Circular Linked List: ";
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
CircularLinkedList cl1;
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
class CircularLinkedList {
Node* head;
public:
CircularLinkedList() {
head = nullptr;
}
if (current == head)
cout << "Value not found!" << endl;
else {
prev->next = current->next;
delete current;
cout << "Deleted " << val << endl;
}
}
void search(int val) {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
int pos = 1;
bool found = false;
do {
if (temp->data == val) {
cout << val << " found at position " << pos << endl;
found = true;
break;
}
temp = temp->next;
pos++;
} while (temp != head);
if (!found)
cout << val << " not found in the list!" << endl;
}
void display() {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
cout << "Circular Linked List: ";
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
CircularLinkedList cl1;
END !