Data Structures Lab Report - CSE 2024
Data Structures Lab Report - CSE 2024
2022BCSE051
LABORATORY REPORT
Data Structures Lab (CST251)
Submitted by
Yash Modi
2022BCSE051
II [Link]. CSE (4th Semester – Section ‘B’)
Submitted to
Dr. VENINGSTON K
Assistant Professor
Department of Computer Science and Engineering
National Institute of Technology Srinagar
Hazratbal, Srinagar, Jammu and Kashmir - 190006, India.
SPRING 2024
Yash Modi
2022BCSE051
<<Your Name>>
Yash Modi
2022BCSE051No.>>
<<Enrollment
CONTENTS
Page Faculty
Week # Date Experiment
No. Sign
Self-Declaration
I have done all the above programs on my own to the best of my knowledge.
Student’s Signature
Yash Modi
2022BCSE051
Yash Modi
2022BCSE051
WEEK 1
26-02-2024
#include<bits/stdc++.h>
for(int i=0;i<n;i++){
for(int j=i+1;j<n+1;j++){
if(ar[i]>ar[j]){
int temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;}}}}
return ar[length-n];}
return ar[n-1];}
int main(){
int n,length;
cin>>length;
int ar[length];
for(int i=0;i<length;i++){
cin>>ar[i];
cin>>n;
selection_sort(ar,length);
cout<<nth_largest(ar,n,length)<<endl;
cout<<nth_smallest(ar,n)<<endl;
return 0;
}
Yash Modi
2022BCSE051
#include<bits/stdc++.h>
int ar_new[n]={0};
for(int i=0;i<n;i++){
ar_new[(i-step)%n]=ar[i];
for(int i=0;i<n;i++){
cout<<ar_new[i]<<" ";
int main() {
int ar[5]={1,2,3,4,5};
left_shift(ar,5,2);
return 0;
#include<bits/stdc++.h>
int ar_new[n]={0};
for(int i=0;i<n;i++){
ar_new[(i+step)%n]=ar[i];
for(int i=0;i<n;i++){
cout<<ar_new[i]<<" ";
}
Yash Modi
2022BCSE051
int main() {
int ar[5]={1,2,3,4,5};
left_shift(ar,5,2);
return 0;
#include<iostream>
for(int i=0;i<size;i++) {
for(int j=0;j<size;j++) {
if(ar1[i] == ar2[j]){
cout<<ar1[i]<<" ";
int main(){
int ar1[6]={1,2,5,6,8,7};
int ar2[6]={1,77,3,7,0,3};
intersection_of_array(ar1,ar2,6);
#include<bits/stdc++.h>
int start = 0;
while(start<=end) {
swap(arr[start],arr[end]);
start++;
end--;
Yash Modi
2022BCSE051
int main() {
int size;
cin>>size;
int ar[size];
for(int i=0;i<size;i++){
cin>>ar[i];
reverse_array(ar,size);
for(int i=0;i<size;i++){
cout<<ar[i]<<" ";
return 0;
6. Design a program to manage employee records using structures in C++. The program should
Ensure that the program implements proper error handling and validation for user inputs. Use
appropriate data structures and algorithms to efficiently manage and manipulate employee
records.
#include<bits/stdc++.h>
struct Student {
int roll_number;
string name;
int age;
float grade;
};
int main() {
int num_students;
float average_grade;
Yash Modi
2022BCSE051
Student students[num_students];
cout << endl << "Enter details for student " << i + 1 << ":" << endl;
total_grade += students[i].grade;
cout << endl << "Average Grade of all students: " << average_grade << endl;
return 0;
} break;
case 2:
displayAll();
break;
case 3:
displayJoined(date, true);
break;
case 4:
Yash Modi
2022BCSE051
displayJoined(date, false);
break;
case 5:
searchByID(id);
break;
case 6:
updateSal(id, newSal);
break;
case 7:
deleteByID(id);
break;
case 8:
calculateStats();
break;
case 9:
return 0;
default:
return 0;
}
Yash Modi
2022BCSE051
WEEK 2
04-03-2024
Searching Algorithm
AIM: Linear Search: Sequentially checks each element until target found.
Q1 Given an unsorted array of elements and a key value, write a function to determine if the key
#include<bits/stdc++.h>
for(int i=0;i<n;i++){
if(ar[i]==key){
return i;
return -1;
int main(){
int n,key;
cin>>n;
int ar[n];
for(int i=0;i<n;i++){
cin>>ar[i];
cin>>key;
cout<<search_ar(ar,n,key);
return 0;
Q2 Given a sorted array of elements and a key value, write a function to determine if the key
#include<bits/stdc++.h>
if(start>end)
return -1;
int mid=(start+end)/2;
if(key==ar[mid])
return mid;
else if(key<ar[mid])
binary_search(ar,start,mid-1,key);
else
binary_search(ar,mid+1,end,key);
int main() {
int ar[6]={2,1,10,9,2,0};
cout<<binary_search(ar,0,5,10);
cout<<binary_search(ar,0,5,1100);
#include <bits/stdc++.h>
struct Emp {
int id;
string nm;
string dep;
string des;
string dob;
string doj;
double sal;
};
vector<Emp> emps;
void add() {
Emp e;
cin >> [Link] >> [Link] >> [Link] >> [Link] >> [Link] >> [Link] >> [Link];
emps.push_back(e);
Yash Modi
2022BCSE051
void displayAll() {
cout << [Link] << " " << [Link] << " " << [Link] << " " << [Link] << " " << [Link] << " " << [Link] << " " << [Link] << endl;
if ((before && [Link] < date) || (!before && [Link] > date)) {
cout << [Link] << " " << [Link] << " " << [Link] << " " << [Link] << " " << [Link] << " " << [Link] << " " << [Link] << endl;
if (it != [Link]()) {
cout << it->id << " " << it->nm << " " << it->dep << " " << it->des << " " << it->dob << " " << it->doj << " " << it->sal <<
endl;
} else {
if (it != [Link]()) {
it->sal = newSal;
} else {
if (it != [Link]()) {
[Link](it);
} else {
Yash Modi
2022BCSE051
void calculateStats() {
if ([Link]()) {
return;
double totalSal = 0;
totalSal += [Link];
cout << "Avg salary: " << fixed << setprecision(2) << totalSal / [Link]() << endl;
int main() {
double newSal;
string date;
while (true) {
cout << "1. Add\n2. Display all\n3. Display joined before a date\n4. Display joined after a date\n5. Search by ID\n6.
Update salary\n7. Delete by ID\n8. Calculate stats\n9. Exit\n";
switch (ch) {
case 1:
add();
break;
Yash Modi
2022BCSE051
case 2:
displayAll();
break;
case 3:
displayJoined(date, true);
break;
case 4:
displayJoined(date, false);
break;
case 5:
searchByID(id);
break;
case 6:
updateSal(id, newSal);
break;
case 7:
deleteByID(id);
break;
case 8:
calculateStats();
break;
case 9:
return 0;
default:
return 0;
Q4 Design a structure called Car with attributes such as car_ID, make, model, year,
rental_price_per_day, from_date, and to_date. Write a program to allow users to input rental
car data, store them in an array of structures, and then calculate and display the total rental
#include<bits/stdc++.h>
struct Car{
string car_ID,make,model,year;
};
int main() {
int n;
cin>>n;
Car cars[n];
for(int i=0;i<n;i++){
cout<<"Car ID";
cin>>cars[i].car_ID;
cout<<"Car Make";
cin>>cars[i].make;
cout<<"Car Model";
cin>>cars[i].model;
cout<<"Car Year";
cin>>cars[i].year;
Yash Modi
2022BCSE051
cout<<"Car rent_per_day";
cin>>cars[i].rental_price_per_day;
cout<<"Car from_date";
cin>>cars[i].from_date;
cout<<"Car to_date";
cin>>cars[i].to_date;
string carID;
cin>>carID;
for(int i=0;i<n;i++){
if(carID==cars[i].car_ID)
return 0;
Q5 Given a 2D array of integers where each row and column are sorted in ascending order, write
a function to efficiently search for a target value in the array. Print the # of comparisons
required before finding a target value through row-based and column-based searches.
#include<bits/stdc++.h>
int row=[Link]();
int col=ar[0].size();
int start=0;
int end=row*col-1;
int comparisons=0;
Yash Modi
2022BCSE051
while(start<=end){
int mid=(start+end)/2;
comparisons++;
if(ar[mid/col][mid%col]==key){
return;
else if(ar[mid/col][mid%col]<key)
start=mid+1;
else
end=mid-1;
int main() {
vector<vector<int>> ar={{1,2,3},{4,5,6},{7,8,9}};
binary_search_2d(ar,8);
return 0;
Q6 Given a sorted array of integers and a target value, write efficient functions to find the first
#include<bits/stdc++.h>
int left=0,right=n-1;
int first_pos=-1,mid=0;
while(left<=right){
mid=(left+right)/2;
if(ar[mid]==key){
first_pos=mid;
right=mid-1;
else if(ar[mid]<key){
Yash Modi
2022BCSE051
left=mid+1;
else{
right=mid-1;
return first_pos;
int left=0,right=n-1;
int last_pos=-1,mid=0;
while(left<=right){
mid=(left+right)/2;
if(ar[mid]==key){
last_pos=mid;
left=mid+1;
else if(ar[mid]<key){
left=mid+1;
else{
right=mid-1;
return last_pos;
int main() {
int ar[8]={3,4,4,4,5,5,8,9};
cout<<first_occu(ar,8,4);
cout<<last_occu(ar,8,4);
return 0;
}
Yash Modi
2022BCSE051
Q7 Given a rotated/wrapped sorted array of integers and a key value, write an efficient function
#include<bits/stdc++.h>
if(start>end)
return -1;
int mid=(start+end)/2;
if(key==ar[mid])
return mid;
else if(key<ar[mid])
binary_search(ar,start,mid-1,key);
else
binary_search(ar,mid+1,end,key);
int shift=1,i=0;
while(ar[i]<ar[i+1]){
shift++;
i++;
if(key<=ar[shift-1])
return binary_search(ar,0,shift-1,key);
else
return binary_search(ar,shift-1,n-1,key);
int main() {
int ar[8]={39,42,95,11,15,21,30,33};
cout<<search_shifted(ar,8,95);
return 0;
Q8 Write a function to find all peak elements from an array of integers. A peak element is an
element that is greater than or equal to its neighbours. Consider that the input array is
wrapped around.
Yash Modi
2022BCSE051
#include<bits/stdc++.h>
for(int i=0;i<n;i++){
cout<<ar[i]<<" ";
cout<<ar[i]<<" ";
int main() {
check_peak(ar,7);
return 0;
}
Yash Modi
2022BCSE051
Yash Modi
2022BCSE051
WEEK 3
11-03-2024
Sorting Algorithm
Q1 Implement the generic bubble sort algorithm to sort an array of elements in ascending order.
Modify the bubble sort implementation to count the number of swaps made during the
sorting process. Print the total number of swaps after sorting the array.
#include<bits/stdc++.h>
int count_swap=0;
for(int i=0;i<n;i++){
int flag=0;
for(int j=0;j<n-i-1;j++){
if(ar[j]>ar[j+1]){
swap(ar[j],ar[j+1]);
flag=1;
count_swap++;
if(flag==0){
break;
int main() {
int ar[10]={6,1,9,0,2,5,1,2,50,65};
bubble_sort(ar,10);
for(int i=0;i<10;i++){
cout<<ar[i]<<" ";
return 0;
}
Yash Modi
2022BCSE051
Q2 Implement the insertion sort algorithm to sort an array of integers in ascending order. Modify
#include<bits/stdc++.h>
int i=0;
string key;
for(int j=1;j<n;j++){
key=ar[j];
i=j-1;
ar[i+1]=ar[i];
i--;
ar[i+1]=key;
int main() {
insertion_sort(names,6);
for(int i=0;i<6;i++){
cout<<names[i]<<" ";
return 0;
Q3 Modify the insertion sort implementation to employ binary search to find the correct position
of the element to be placed in the sorted subarray to decrease the # of comparisons required.
Run the native and modified insertion sort on the same inputs and assess the performance of
#include <iostream>
if (arr[mid] == key)
Yash Modi
2022BCSE051
return mid;
else
return left;
int j = i -1;
j--;
int main() {
insertion_sort2(arr,5);
return 0;
Q4 Implement the selection sort algorithm to sort an array of integers in ascending order. Modify
the selection sort implementation to find the kth smallest element in an array.
#include<bits/stdc++.h>
for(int i=0;i<n;i++){
Yash Modi
2022BCSE051
for(int j=i+1;j<n+1;j++){
if(ar[i]>ar[j]){
int temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
return ar[k-1];
int main(){
int ar[6]={2,6,1,88,2,0};
cout<<selection_sort_kth(ar,6,2);
Q5 Compare the performance of bubble, insertion, and selection sort algorithms based on their
sorting time and sensitivity to the randomly generated input data distribution. Identify the
strengths and weaknesses of each algorithm under different scenarios(i.e., random collection,
#include<bits/stdc++.h>
vector<int> vec(n);
return vec;
}
Yash Modi
2022BCSE051
int n = [Link]();
int swap = 0;
swap++;
arr[j] = arr[j+1];
arr[j+1] = temp;
return swap;
int n = [Link]();
int swap = 0;
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
swap++;
arr[j + 1] = key;
return swap;
}
Yash Modi
2022BCSE051
int n = [Link]();
int swap = 0;
int min_idx = i;
min_idx = j;
swap++;
arr[min_idx] = arr[i];
arr[i] = temp;
return swap;
int main() {
printVector(values);
// Bubble Sort
// Insertion Sort
// Selection Sort
cout << "Bubble Sort - Time taken: " << bubble_duration.count() << " microseconds, Swaps: " << bubble_swaps << endl;
cout << "Insertion Sort - Time taken: " << insertion_duration.count() << " microseconds, Swaps: " << insertion_swaps <<
endl;
cout << "Selection Sort - Time taken: " << selection_duration.count() << " microseconds, Swaps: " << selection_swaps <<
endl;
return 0;
Q6 Implement a strategy to terminate the selection sorting process early if the array becomes
sorted before completing all iterations. Track whether swaps were made in each iteration and
#include <iostream>
bool swapped;
swapped = false;
swapped = true;
if (!swapped) {
return;
int main() {
bubbleSort(arr,5);
return 0;
Q7 Implement the counting sort algorithm to sort an array of positive integers in ascending order.
Also, the counting sort implementation should be modified to sort an array of characters in
lexicographical order. Test the implementation with various strings and verify its correctness.
#include<bits/stdc++.h>
int main() {
int arr[7]={4,2,2,8,3,3,1};
countingSort(arr,7,8);
for(int i=0;i<7;i++){
Yash Modi
2022BCSE051
cout<<arr[i]<<" ";
return 0;
Q8 Implement the radix sort algorithm to sort an array of positive integers in ascending order.
Modify the radix sort implementation to sort an array of strings in lexicographical order.
#include <iostream>
#include <algorithm>
int max_length = 0;
string output[n];
for (int j = n - 1; j >= 0; --j) output[--count[i < arr[j].length() ? arr[j][i] : 0]] = arr[j];
int main() {
radixSort(arr,8);
for(int i=0;i<8;i++){
cout<<arr[i]<<" ";
cout<<endl;
radixSort(str_arr,5);
for(int i=0;i<8;i++){
cout<<str_arr[i]<<" ";
return 0;
Q9 Implement the Radix Sort algorithm that handles input arrays with a mix of positive and
negative integers.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
count[index + radix]++;
count[index + radix]--;
arr = output;
countSort(arr, exp);
if (min_val < 0) {
int idx = 0;
if (arr[i] < 0) {
swap(arr[i], arr[idx]);
idx++;
} else {
break;
int main() {
radixSort(arr);
}
Yash Modi
2022BCSE051
return 0;
}
Yash Modi
2022BCSE051
WEEK 4
18-03-2024
Sorting Algorithms II
1]Quick Sort
2]Merge Sort
Q1 Implement the Merge Sort algorithm as a function that takes an array of integers and sorts it.
#include<bits/stdc++.h>
int i = 0, j = 0, k = l;
if(l < r) {
int mid = l + (r - l) / 2;
merge_sort(ar, l, mid);
int main() {
merge_sort(ar, 0, 5);
Yash Modi
2022BCSE051
return 0;
Q2 Modify the Merge Sort implementation to sort an array of strings in lexicographical order.
Write a function that takes an array of strings as input and sorts it using Merge Sort.
#include<bits/stdc++.h>
int i = 0, j = 0, k = l;
if(l < r) {
int mid = l + (r - l) / 2;
merge_sort(ar, l, mid);
int main() {
merge_sort(ar, 0, 5);
return 0;
Q3 Implement the Quick Sort algorithm as a function that takes an array of integers and sorts it.
#include<bits/stdc++.h>
int i = l - 1;
i++;
swap(ar[i], ar[j]);
return i + 1;
if(l < r) {
int main() {
quick_sort(ar, 0, 5);
return 0;
Q4 Modify the Quick Sort implementation to randomly select the pivot element instead of always
Yash Modi
2022BCSE051
choosing the last element as the pivot. Implement a function that chooses a random pivot for
each partitioning step. Evaluate the performance of the randomized Quick Sort algorithm and
#include <bits/stdc++.h>
#include <chrono>
int i = l - 1;
i++;
swap(ar[i], ar[j]);
return i + 1;
swap(ar[random_index], ar[r]);
if (l < r) {
}
Yash Modi
2022BCSE051
int main() {
srand(time(0));
int n = 100000;
int ar[n];
quick_sort_randomized(ar, 0, n - 1);
cout << "Randomized Quick Sort execution time: " << duration_randomized.count() << " microseconds" << endl;
return 0;
Q5 Modify the Quick Sort implementation to handle arrays with duplicate elements efficiently
#include<iostream>
int p = arr[s], l = s, g = e, i = s;
while (i <= g) {
else i++;
le = l - 1;
ge = g + 1;
Yash Modi
2022BCSE051
if (s >= e) return;
QuickSort(arr, s, le);
for (int i = 0; i < size; i++) cout << arr[i] << " ";
int main() {
printArray(arr, size);
printArray(arr, size);
return 0;
Q6 Use Quick sort to develop an algorithm to find median of an array of integers. The median of
a set of numbers is the middle value when the numbers are arranged in ascending or
descending order.
#include<iostream>
swap(arr[i], arr[r]);
return i;
swap(arr[pivot], arr[r]);
if (l <= r) {
if (partitionIndex == k) {
b = arr[partitionIndex];
if (a != -1) return;
} else if (partitionIndex == k - 1) {
a = arr[partitionIndex];
if (b != -1) return;
if (n % 2 == 1) MedianUtil(arr, 0, n - 1, n / 2, a, b);
int ans = (n % 2 == 1) ? b : (a + b) / 2;
int main() {
findMedian(arr, n);
Yash Modi
2022BCSE051
return 0;
Q8Write a function to find the median of an array of integers using the Median of
Medians algorithm. Employ this median of medians finding algorithm to revise the quick sort
#include<iostream>
#include <chrono>
int count = 0;
count++;
swap(arr[pivotIndex], arr[s]);
int i = s, j = e;
i++;
j--;
swap(arr[i++], arr[j--]);
return pivotIndex;
}
Yash Modi
2022BCSE051
if(s >= e) {
return;
QuickSortFirstPivot(arr, s, p - 1);
QuickSortFirstPivot(arr, p + 1, e);
int pivotIndex = s;
swap(arr[i], arr[pivotIndex]);
pivotIndex++;
swap(arr[pivotIndex], arr[e]);
return pivotIndex;
if(s >= e) {
return;
QuickSortLastPivot(arr, s, p - 1);
QuickSortLastPivot(arr, p + 1, e);
}
Yash Modi
2022BCSE051
return arr;
int main() {
QuickSortFirstPivot(values, 0, numValues);
cout << "Time for choosing first element as pivot: " << [Link]() << " microseconds" << endl;
QuickSortLastPivot(values, 0, numValues);
cout << "Time for choosing last element as pivot: " << [Link]() << " microseconds" << endl;
delete[] values;
Yash Modi
2022BCSE051
WEEK 5
01-04-2024
Q1 Write a program to create a single linked list. Include methods to insert elements at the
beginning, end, a specific position of the list, and a specific element of the list, delete
elements, search for a value, and display the contents of the list.
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data=data;
next=NULL;
};
if(head==NULL)
head=temp;
else{
temp->next=head;
head=temp;
node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
temp->next=n;
Yash Modi
2022BCSE051
int position=1;
node* temp=head;
temp=temp->next;
position++;
node* temp2=temp->next;
temp->next=n;
n->next=temp2;
int position=1;
bool flag=true;
while(head->next!=NULL){
position++;
if(head->data==value){
flag=false;
break;
head=head->next;
if(flag)
node* temp=head;
int count=0;
while(count!=pos){
temp=temp->next;
count++;
Yash Modi
2022BCSE051
return temp->data;
while(head->next!=NULL){
cout<<head->data<<" ";
head=head->next;
cout<<endl;
// int position=1;
while(head->data!=value){
head=head->next;
node* temp=head->next;
head->next=head->next->next;
// delete temp;
free(temp);
int position=1;
while(position=pos-1){
head=head->next;
node* temp=head->next;
head->next=head->next->next;
// delete temp;
free(temp);
int main() {
insert_at_tail(2,head);
insert_at_tail(3,head);
Yash Modi
2022BCSE051
insert_at_tail(4,head);
insert_at_tail(5,head);
insert_at_tail(6,head);
search_by_value(head,5);
insert_at_position(10,head,3);
show_list(head);
delete_at_position(head,5);
show_list(head);
cout<<search_by_position(head,3);
return 0;
Q2 Write a function to reverse a single linked list in-place. Implement an iterative solution to
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data=data;
next=NULL;
};
node* temp=head;
while(temp->next!=NULL){
temp=temp->next;
temp->next=n;
while(head!=NULL){
cout<<head->data<<" ";
head=head->next;
cout<<endl;
node* prev=NULL;
node* curr=head;
node* next=head->next;
while(curr!=NULL){
next = curr->next;
curr->next=prev;
prev=curr;
curr=next;
head=prev;
int main() {
insert_at_tail(2,head);
insert_at_tail(3,head);
insert_at_tail(4,head);
insert_at_tail(5,head);
insert_at_tail(6,head);
list_reversal(head);
show_list(head);
return 0;
Q3 Write a function that takes two sorted single linked lists as input and merges them into a single
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;}
while(head != NULL){
head = head->next;
void merge_list(node* head1, node* head2, node* &dummy, int size1, int size2){
int i = 0, j = 0;
temp3->next = temp2;
temp2 = temp2->next;
i++;
else{
Yash Modi
2022BCSE051
temp3->next = temp1;
temp1 = temp1->next;
j++;
temp3 = temp3->next;
temp3->next = temp1;
temp1 = temp1->next;
temp3 = temp3->next;
i++;
temp3->next = temp2;
temp2 = temp2->next;
temp3 = temp3->next;
j++;
int main() {
insert_at_tail(20, head);
insert_at_tail(31, head);
insert_at_tail(49, head);
insert_at_tail(50, head);
insert_at_tail(69, head);
insert_at_tail(1, head2);
insert_at_tail(44, head2);
insert_at_tail(45, head2);
insert_at_tail(60, head2);
insert_at_tail(90, head2);
insert_at_tail(100, head2);
show_list(dummy->next);
return 0;
Q4 Write a function to remove the nth node from the end of a single linked list and return the head of the modified list.
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;
while(head != NULL){
head = head->next;
}
Yash Modi
2022BCSE051
int position = 1;
head = head->next;
position++;
head->next = head->next->next;
delete temp;
int main() {
insert_at_tail(20, head);
insert_at_tail(31, head);
insert_at_tail(49, head);
insert_at_tail(50, head);
insert_at_tail(69, head);
delete_at_position(head,2,6);
show_list(head);
return 0;
Q5 Write a function to find the intersection of two singly linked lists. If the lists do not
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
}
Yash Modi
2022BCSE051
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;
while(head != NULL){
head = head->next;
node* temp=head;
int size=0,count=0;
int mid;
while(temp->next!=NULL){
temp=temp->next;
size++;
if(size%2==0){
mid=size/2;
if(size%2!=0){
mid=size/2;
mid++;
while(count!=mid){
temp=temp->next;
count++;
}
Yash Modi
2022BCSE051
cout<<"The position of the mid value is :"<<mid<<" and the data value is :"<<temp->data;
int main() {
insert_at_tail(20, head);
insert_at_tail(31, head);
insert_at_tail(49, head);
insert_at_tail(50, head);
insert_at_tail(69, head);
show_list(head);
return 0;
Q6 Write a function to find the intersection of two singly linked lists. If the lists do not
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;
int count=0;
while(head!=NULL){
head=head->next;
count++;
return count;
int flag=0;
int count=abs(size_checker(head1)-size_checker(head2));
if(size_checker(head1)>size_checker(head2)){
while(count!=0){
head1=head1->next;
count--;
if(head1==head2){
flag=1;
break;
head1=head1->next;
head2=head2->next;
else{
while(count!=0){
head2=head2->next;
count--;
if(head1==head2){
flag=1;
break;
head1=head1->next;
head2=head2->next;
if(!flag)
cout<<"No intersection";
else
cout<<"Intersection exists";
int main() {
head1->next = commonNode;
head2->next = commonNode;
insert_at_tail(20, head1);
insert_at_tail(30, head2);
intersection(head1, head2);
return 0;
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
Yash Modi
2022BCSE051
next = NULL;
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;
while(head != NULL){
head = head->next;
if(!head)
return;
temp->next = dummy;
dummy = temp;
reversal(head->next, dummy);
int flag=1;
while(dummy->next){
if(head->data==dummy->data)
flag=0;
else{
flag=1;
}
Yash Modi
2022BCSE051
head=head->next;
dummy=dummy->next;
if(flag==0)
cout<<"Pallindrome exists";
else{
cout<<"NO palindrome";
int main() {
insert_at_tail(2, head);
insert_at_tail(1, head);
insert_at_tail(1, head);
insert_at_tail(2, head);
insert_at_tail(1, head);
reversal(head,dummy);
pallindrome(head,dummy);
return 0;
Q8 Define the function moveToFront(struct node *head) to move a last node to the front of a
#include<bits/stdc++.h>
class node{
public:
int data;
node* next;
node(int data){
this->data = data;
next = NULL;
Yash Modi
2022BCSE051
};
while(temp->next != NULL){
temp = temp->next;
temp->next = n;
while(head != NULL){
head = head->next;
node* temp=head;
while(temp->next->next!=NULL){
temp=temp->next;
temp->next->next=head;
head=temp->next;
temp->next=NULL;
int main() {
insert_at_tail(20, head);
insert_at_tail(31, head);
insert_at_tail(49, head);
insert_at_tail(50, head);
insert_at_tail(69, head);
Yash Modi
2022BCSE051
tail_to_head(head);
show_list(head);
#include <bits/stdc++.h>
class node {
public:
int data;
node* next;
node(int data) {
this->data = data;
next = NULL;
};
if (head == NULL) {
return;
temp = temp->next;
temp->next = n;
head = head->next;
}
Yash Modi
2022BCSE051
return true;
return false;
current = current->next;
return true;
if (isNonDecreasing(head)) {
} else {
int main() {
insert_at_tail(20, head);
insert_at_tail(31, head);
insert_at_tail(49, head);
insert_at_tail(50, head);
insert_at_tail(69, head);
non_decreasing(head);
return 0;
}
Yash Modi
2022BCSE051
WEEK 6
08-04-2024
Aim:
Q1 Implement a static stack using an array with push, pop, peek, peep, isEmpty, size, and display operations.
#include<iostream>
class Stack{
public:
int data;
Stack* next;
Stack(int data1){data=data1;next=NULL;}
Stack* top=NULL;
if(temp==NULL){cout<<"Stack Overflow"<<endl;}
temp->data=data;
temp->next=top;
top=temp;
void pop(){
Stack* temp;
if(top==NULL){cout<<"Stack Underflow"<<endl;}
temp=top;
top=top->next;
delete temp;
int peek(){
return top->data;
Stack* temp=top;
if(temp==NULL){return -1;}
Yash Modi
2022BCSE051
if(i==1){return top->data;}
int count=0;
while(temp!=NULL){
count++;
if(count==i){return temp->data;}
temp=temp->next;
return -1;
int size(){
int count=0;
Stack* temp=top;
while(temp!=NULL){count++;temp=temp->next;}
return count;
void display(){
Stack* temp=top;
if(top==NULL){cout<<"Stack Underflow"<<endl;}
while(temp!=NULL){cout<<temp->data<<" ";temp=temp->next;}
cout<<endl;
};
int main(){
Stack s(100);
[Link](1);[Link](5);[Link](10);
cout<<[Link]()<<endl;
[Link]();
cout<<[Link]()<<endl;
return 0;
}
Yash Modi
2022BCSE051
Q2 Write a program for the dynamic implementation of stack using a single linked list with the
above functionalities.
#include<iostream>
class Stack{
public:
int data;
Stack* next;
Stack* top=NULL;
if(temp==NULL){cout<<"Stack Overflow"<<endl;}
temp->data=data;
temp->next=top;
top=temp;
void pop() {
Stack* temp;
if(top==NULL){cout<<"Stack Underflow"<<endl;}
temp=top;
top=top->next;
delete temp;
int peek() {
return top->data;
int peep(int i) {
Stack* temp=top;
if(temp==NULL)return -1;
if(i==1)return top->data;
int count=0;
while(temp!=NULL){count++;if(count==i)return temp->data;temp=temp->next;}
return -1;
Yash Modi
2022BCSE051
};
int main() {
Stack s(NULL);
[Link](1);[Link](5);[Link](10);
cout<<[Link]()<<endl;
[Link]();
cout<<[Link]()<<endl;
return 0;}
Q3 Write a program for the static implementation of a circular queue using an array with enqueue, dequeue, front, isEmpty,
and size operations.
#include<bits/stdc++.h>
class Queue{
public:
int *ar;
int size;
int front,rear;
Queue(int size){
this->size=size;
front=0;
rear=0;
rear=(rear+1)%size;
Yash Modi
2022BCSE051
else{ar[rear]=data;}
int dqueue(){
if(front==rear)cout<<"Queue, Underflow"<<endl;
void display(){
if(front==rear){cout<<"Queue is empty"<<endl;}
};
int main() {
Queue q(8);
[Link](1);[Link](2);[Link](3);[Link](4);[Link](5);[Link](6);[Link](7);[Link]();
[Link]();[Link]();[Link]();[Link]();[Link]();
return 0;
Q4 Write a program for the dynamic implementation of a queue using a single linked list with the
above functionalities.
#include <bits/stdc++.h>
class Queue {
public:
int data;
Queue* next;
int dequeue() {
return data;
int peek() {
return front->data;
void display() {
Queue* temp = front;do {cout << temp->data << " ";temp = temp->next;} while (temp != front);
};
int main() {
Queue q(1);[Link](10);[Link](20);[Link](30);[Link](40);[Link]();
return 0;