0% found this document useful (0 votes)
89 views60 pages

Code Tantra Dsa Aktu

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 60

Exp.

Name: Write a C program to Sort the given


S.No: 1 Date: 2023-10-18
elements in Ascending order using Bubble Sort

Aim:

Page No: 1
Write a program to sort the given elements using Bubble sort technique ( Ascending order ) .
Source Code:

bubbleSort.c

ID: 2200330100243
#include<stdio.h>

int main(){
int n;
printf("n : ");
scanf("%d",&n);
int arr[n];
for(int i=0; i<n; i++){
printf("a[%d] = ",i);
scanf("%d",&arr[i]);
}
printf("Before sorting : \n");

2022-2026-CSE-D-D1
for(int i; i<n; i++){
printf("a[%d] = %d\n",i,arr[i]);
}

for (int i = 0; i < n - 1; i++) {


for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];

Raj Kumar Goel Institute Of Technology


arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("After sorting : \n");
for(int i=0; i<n;i++){
printf("a[%d] = %d\n",i,arr[i]);
}
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
n :
5
a[0] =
2
a[1] =
7
a[2] =
6
a[3] =
4
a[4] =

Page No: 2
1
Before sorting :
a[0] = 2
a[1] = 7

ID: 2200330100243
a[2] = 6
a[3] = 4
a[4] = 1
After sorting :
a[0] = 1
a[1] = 2
a[2] = 4
a[3] = 6
a[4] = 7

Test Case - 2

2022-2026-CSE-D-D1
User Output
n :
4
a[0] =
28
a[1] =
34

Raj Kumar Goel Institute Of Technology


a[2] =
26
a[3] =
29
Before sorting :
a[0] = 28
a[1] = 34
a[2] = 26
a[3] = 29
After sorting :
a[0] = 26
a[1] = 28
a[2] = 29
a[3] = 34

Test Case - 3

User Output
n :
5
a[0] =
-45
a[1] =
-12
a[2] =
-77
a[3] =

Page No: 3
-21
a[4] =
-100
Before sorting :

ID: 2200330100243
a[0] = -45
a[1] = -12
a[2] = -77
a[3] = -21
a[4] = -100
After sorting :
a[0] = -100
a[1] = -77
a[2] = -45
a[3] = -21
a[4] = -12

2022-2026-CSE-D-D1
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a C program to Sort the elements
S.No: 2 Date: 2023-10-18
using Insertion Sort Technique

Aim:

Page No: 4
Write a program to sort the given elements using Insertion sort technique ( Ascending order ).
Source Code:

insertionSort.c

ID: 2200330100243
#include<stdio.h>
void main() {
int a[20], i, n, j, temp;
printf("n = ");
scanf("%d", &n);
// Write the for loop to read array elements
for(int i=0; i < n; i++){
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}

printf("Before sorting : \n");

2022-2026-CSE-D-D1
// Write the for loop to display array elements before sorting
for (int i = 0; i < n; i++)
{
printf("a[%d] = %d\n",i,a[i]);
}
//Write the code to sort elements

for (int i = 1,j; i < n; i++) {

Raj Kumar Goel Institute Of Technology


int key = a[i];
j = i - 1;
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j--;
a[j + 1] = key;
}
}
printf("After sorting : \n");
// Write the for loop to display array elements after sorting

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


{
printf("a[%d] = %d\n",i,a[i]);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
n =
5
a[0] =
12
a[1] =
4
a[2] =

Page No: 5
27
a[3] =
37
a[4] =

ID: 2200330100243
41
Before sorting :
a[0] = 12
a[1] = 4
a[2] = 27
a[3] = 37
a[4] = 41
After sorting :
a[0] = 4
a[1] = 12
a[2] = 27

2022-2026-CSE-D-D1
a[3] = 37
a[4] = 41

Test Case - 2

User Output
n =
7

Raj Kumar Goel Institute Of Technology


a[0] =
3
a[1] =
8
a[2] =
2
a[3] =
1
a[4] =
4
a[5] =
9
a[6] =
4
Before sorting :
a[0] = 3
a[1] = 8
a[2] = 2
a[3] = 1
a[4] = 4
a[5] = 9
a[6] = 4
After sorting :
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4

Page No: 6
a[4] = 4
a[5] = 8
a[6] = 9

ID: 2200330100243
Test Case - 3

User Output
n =
5
a[0] =
-36
a[1] =
-100
a[2] =
-43

2022-2026-CSE-D-D1
a[3] =
54
a[4] =
0
Before sorting :
a[0] = -36
a[1] = -100

Raj Kumar Goel Institute Of Technology


a[2] = -43
a[3] = 54
a[4] = 0
After sorting :
a[0] = -100
a[1] = -43
a[2] = -36
a[3] = 0
a[4] = 54
Exp. Name: Write a C program to Sort given elements
S.No: 3 Date: 2023-10-18
using Selection sort largest element method

Aim:

Page No: 7
Write a program to sort the given array elements using Selection sort largest element
method( Ascending order ).
Source Code:

ID: 2200330100243
selectionSort.c

2022-2026-CSE-D-D1
Raj Kumar Goel Institute Of Technology
#include<stdio.h>

int main(){

Page No: 8
int n;
printf("n = ");
scanf("%d",&n);
int arr[20];

ID: 2200330100243
// reading element on array
for(int i = 0; i < n; i++)
{
printf("a[%d] = ",i);
scanf("%d",&arr[i]);
}

printf("Before sorting : \n");


// print unsorted array

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


{

2022-2026-CSE-D-D1
printf("a[%d] = %d\n",i,arr[i]);
}

// selection sort algorithm

for (int i = n - 1; i > 0; i--) {


int small = 0;
for (int j = 1; j <= i; j++) {

Raj Kumar Goel Institute Of Technology


if (arr[j] > arr[small]) {
small = j;
}
}
// Swap the maximum element with the last element
int temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}

printf("After sorting : \n");


// print sorted array

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


{
printf("a[%d] = %d\n",i,arr[i]);
}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output
n =
7
a[0] =
5

Page No: 9
a[1] =
2
a[2] =
3

ID: 2200330100243
a[3] =
4
a[4] =
6
a[5] =
1
a[6] =
9
Before sorting :
a[0] = 5

2022-2026-CSE-D-D1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 6
a[5] = 1
a[6] = 9
After sorting :
a[0] = 1

Raj Kumar Goel Institute Of Technology


a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 9

Test Case - 2

User Output
n =
5
a[0] =
45
a[1] =
25
a[2] =
67
a[3] =
89
a[4] =
44
Before sorting :
a[0] = 45
a[1] = 25
a[2] = 67
a[3] = 89

Page No: 10
a[4] = 44
After sorting :
a[0] = 25
a[1] = 44
a[2] = 45

ID: 2200330100243
a[3] = 67
a[4] = 89

Test Case - 3

User Output
n =
4
a[0] =
-9

2022-2026-CSE-D-D1
a[1] =
-54
a[2] =
-12
a[3] =
-369
Before sorting :

Raj Kumar Goel Institute Of Technology


a[0] = -9
a[1] = -54
a[2] = -12
a[3] = -369
After sorting :
a[0] = -369
a[1] = -54
a[2] = -12
a[3] = -9
Exp. Name: Write a C program to sort given elements
S.No: 4 Date: 2023-10-18
using Selection sort smallest element method

Aim:

Page No: 11
Write a program to sort the given array elements using Selection sort smallest element
method( Ascending order ).
Source Code:

selctionSmallest.c

ID: 2200330100243
#include <stdio.h>

int main()
{
int n;
printf("n = ");
scanf("%d", &n);
int arr[n];
// reading element on array
for (int i = 0; i < n; i++)
{

2022-2026-CSE-D-D1
printf("a[%d] = ",i);
scanf("%d", &arr[i]);
}
printf("Before sorting : \n");
// print unsorted array
for (int i = 0; i < n; i++)
{
printf("a[%d] = %d\n",i,arr[i]);

Raj Kumar Goel Institute Of Technology


}
// selection sort algorithm
for (int i = 0; i < n - 1; i++){
int small = i;
for (int j = i + 1; j < n; j++){
if (arr[j] < arr[small]){
small = j;
}
}
// Swap the minimum element with the first element
int temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
printf("After sorting : \n");
for (int i = 0; i < n; i++)
{
printf("a[%d] = %d\n", i, arr[i]);
}
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
n =
5

Page No: 12
a[0] =
15
a[1] =
45

ID: 2200330100243
a[2] =
1
a[3] =
20
a[4] =
30
Before sorting :
a[0] = 15
a[1] = 45
a[2] = 1
a[3] = 20

2022-2026-CSE-D-D1
a[4] = 30
After sorting :
a[0] = 1
a[1] = 15
a[2] = 20
a[3] = 30
a[4] = 45

Raj Kumar Goel Institute Of Technology


Test Case - 2

User Output
n =
4
a[0] =
-15
a[1] =
-12
a[2] =
-48
a[3] =
-79
Before sorting :
a[0] = -15
a[1] = -12
a[2] = -48
a[3] = -79
After sorting :
a[0] = -79
a[1] = -48
a[2] = -15
a[3] = -12

Test Case - 3

User Output

Page No: 13
n =
5
a[0] =
34

ID: 2200330100243
a[1] =
68
a[2] =
95
a[3] =
41
a[4] =
23
Before sorting :
a[0] = 34
a[1] = 68

2022-2026-CSE-D-D1
a[2] = 95
a[3] = 41
a[4] = 23
After sorting :
a[0] = 23
a[1] = 34
a[2] = 41

Raj Kumar Goel Institute Of Technology


a[3] = 68
a[4] = 95
Exp. Name: Write a C program to Sort given elements
S.No: 5 Date: 2023-11-22
using Merge sort

Aim:

Page No: 14
Write a program to sort ( Ascending order ) the given elements using merge sort technique.

At the time of execution, the program should print the message on the console as:

Enter array size :

ID: 2200330100243
For example, if the user gives the input as:

Enter array size : 5

Next, the program should print the following message on the console as:

Enter 5 elements :

if the user gives the input as:

Enter 5 elements : 34 67 12 45 22

2022-2026-CSE-D-D1
then the program should print the result as:

Before sorting the elements are : 34 67 12 45 22


After sorting the elements are : 12 22 34 45 67

Note: Do use the printf() function with a newline character ( \n ).


Source Code:

Raj Kumar Goel Institute Of Technology


MergeSortMain.c
#include <stdio.h>
#include "MergeSortFunctions.c"
void main() {
int arr[15], i, n;
printf("Enter array size : ");
scanf("%d", &n);
printf("Enter %d elements : ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Before sorting the elements are : ");
display(arr, n);
splitAndMerge(arr, 0, n - 1);
printf("After sorting the elements are : ");
display(arr, n);
}

MergeSortFunctions.c
void display(int arr[15], int n)
{
for(int i =0 ; i<n; i++)
{
printf("%d ",arr[i]);

Page No: 15
}
printf("\n");
}
void merge(int arr[], int low, int mid, int high)
{

ID: 2200330100243
int n1 = mid- low+ 1;
int n2 = high- mid;
//Creating temporary arrays
int L[n1], R[n2];
// Copy the data of array to temp arrays
for(int i = 0; i < n1; i++)
L[i] = arr[low+ i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid+ 1+ j];
//Merge the tqo arrays
int i, j, k;
i = 0;

2022-2026-CSE-D-D1
j = 0;
k = low;
while (i < n1 && j <n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;

Raj Kumar Goel Institute Of Technology


}
else
{
arr[k] = R[j];
j++;
}
k++;
}
//Copy the remaining elements of L[] if any
while(i < n1)
{
arr[k] = L[i];
i++;
k++;
}
//Copy the remaining elements of R[] if any
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void splitAndMerge(int arr[], int l, int r)
{
if (l<r)
{
//Find the middle point
int mid= (l+r) / 2;
//Rcursively sort the first and second halves
splitAndMerge(arr, l, mid);
splitAndMerge(arr, mid+ 1, r);

Page No: 16
//Merge the two halves
merge(arr, l, mid ,r);
}
}

ID: 2200330100243
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter array size :
5
Enter 5 elements :
34 67 12 45 22

2022-2026-CSE-D-D1
Before sorting the elements are : 34 67 12 45 22
After sorting the elements are : 12 22 34 45 67

Test Case - 2

User Output
Enter array size :

Raj Kumar Goel Institute Of Technology


8
Enter 8 elements :
77 55 22 44 99 33 11 66
Before sorting the elements are : 77 55 22 44 99 33 11 66
After sorting the elements are : 11 22 33 44 55 66 77 99

Test Case - 3

User Output
Enter array size :
5
Enter 5 elements :
-32 -45 -67 -46 -14
Before sorting the elements are : -32 -45 -67 -46 -14
After sorting the elements are : -67 -46 -45 -32 -14
Exp. Name: Write a C program to Sort given elements
S.No: 6 Date: 2023-11-22
using Quick sort

Aim:

Page No: 17
Write a program to sort ( Ascending order ) the given elements using quick sort technique.

Note: Pick the first element as pivot. You will not be awarded marks if you do not follow this instruction.

ID: 2200330100243
At the time of execution, the program should print the message on the console as:

Enter array size :

For example, if the user gives the input as:

Enter array size : 5

Next, the program should print the following message on the console as:

Enter 5 elements :

if the user gives the input as:

2022-2026-CSE-D-D1
Enter 5 elements : 34 67 12 45 22

then the program should print the result as:

Before sorting the elements are : 34 67 12 45 22


After sorting the elements are : 12 22 34 45 67

Note: Do use the printf() function with a newline character ( \n ).

Raj Kumar Goel Institute Of Technology


Source Code:

QuickSortMain.c
#include <stdio.h>
#include "QuickSortFunctions.c"
void main() {
int arr[15], i, n;
printf("Enter array size : ");
scanf("%d", &n);
printf("Enter %d elements : ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Before sorting the elements are : ");
display(arr, n);
quickSort(arr, 0, n - 1);
printf("After sorting the elements are : ");
display(arr, n);
}

QuickSortFunctions.c
void swap(int *a,int *b)
{
int t = *a;
*a = *b;
*b = t;

Page No: 18
}
int partition(int array[], int low, int high)
{
int pivot = array[high];
int i = (low-1);

ID: 2200330100243
for (int j = low; j < high; j++)
{
if (array[j] <= pivot)
{
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
}
void quickSort(int array[], int low, int high)

2022-2026-CSE-D-D1
{
if (low < high)
{
int pi = partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
}

Raj Kumar Goel Institute Of Technology


void display(int array[], int size)
{
for (int i= 0; i < size; ++i)
{
printf("%d ", array[i]);
}
printf("\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter array size :
5
Enter 5 elements :
34 67 12 45 22
Before sorting the elements are : 34 67 12 45 22
After sorting the elements are : 12 22 34 45 67

Test Case - 2
User Output
Enter array size :
8
Enter 8 elements :
77 55 22 44 99 33 11 66

Page No: 19
Before sorting the elements are : 77 55 22 44 99 33 11 66
After sorting the elements are : 11 22 33 44 55 66 77 99

ID: 2200330100243
Test Case - 3

User Output
Enter array size :
5
Enter 5 elements :
-32 -45 -67 -46 -14
Before sorting the elements are : -32 -45 -67 -46 -14
After sorting the elements are : -67 -46 -45 -32 -14

2022-2026-CSE-D-D1
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a C program to Search an element
S.No: 7 Date: 2023-10-18
using Linear Search process

Aim:

Page No: 20
Write a program to search a key element within the given array of elements using Linear search process.
Source Code:

linearSearch.c

ID: 2200330100243
#include <stdio.h>

int main() {
int n,index=-1,key;
printf("n = ");
scanf("%d",&n);
int arr[n];
// get the array element by user

for(int i=0; i<n; i++){


printf("a[%d] = ",i);

2022-2026-CSE-D-D1
scanf("%d",&arr[i]);
}

// searching request.
printf("Search key : ");
scanf("%d",&key);
for (int i = 0; i < n; i++) {

Raj Kumar Goel Institute Of Technology


if (arr[i] == key) {
index =i;
}
}

//result
if (index == -1) {
printf("Key %d is not found.\n",key);
}
else {
printf("Key %d is found at position %d.\n",key,index);
}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
n =
4
a[0] =
153
a[1] =
264
a[2] =

Page No: 21
357
a[3] =
598
Search key :

ID: 2200330100243
100
Key 100 is not found.

Test Case - 2

User Output
n =
5
a[0] =
-15

2022-2026-CSE-D-D1
a[1] =
-24
a[2] =
-36
a[3] =
-11
a[4] =
-20

Raj Kumar Goel Institute Of Technology


Search key :
-11
Key -11 is found at position 3.

Test Case - 3

User Output
n =
5
a[0] =
24
a[1] =
36
a[2] =
11
a[3] =
45
a[4] =
28
Search key :
11
Key 11 is found at position 2.
Exp. Name: Write a C program to Search an element
S.No: 8 Date: 2023-10-19
using Binary Search process

Aim:

Page No: 22
Write a program to search a key element in the given array of elements using Binary search .
Source Code:

binarySearch.c

ID: 2200330100243
2022-2026-CSE-D-D1
Raj Kumar Goel Institute Of Technology
#include <stdio.h>
int binarySearch(int arr[], int n, int x) {
int left = 0;
int right = n - 1;
while (left <= right) {

Page No: 23
int mid = left + (right - left) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] < x) {
left = mid + 1;

ID: 2200330100243
} else {
right = mid - 1;
}
}
return -1;
}
void main()
{
int n, key;
printf("n = ");
scanf("%d", &n);
int arr[n];

2022-2026-CSE-D-D1
// reading element on array
for (int i = 0; i < n; i++)
{
printf("a[%d] = ", i);
scanf("%d", &arr[i]);
}
// searching element
printf("Search key = ");

Raj Kumar Goel Institute Of Technology


scanf("%d", &key);
// sorting algoritm
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("After sorting :\n");
// print sorted element
for (int i = 0; i < n; i++)
{
printf("a[%d] = %d\n", i, arr[i]);
}
int sz=sizeof (arr)/sizeof(arr[0]);
int index = binarySearch(arr, sz, key);
if(index==-1){
printf("Key %d is not found in the array.\n",key);
}
else
{
printf("Key %d is found at position %d.\n",key,index);
}

Page No: 24
Execution Results - All test cases have succeeded!
Test Case - 1

ID: 2200330100243
User Output
n =
5
a[0] =
15
a[1] =
29
a[2] =
67

2022-2026-CSE-D-D1
a[3] =
10
a[4] =
23
Search key =
10
After sorting :
a[0] = 10

Raj Kumar Goel Institute Of Technology


a[1] = 15
a[2] = 23
a[3] = 29
a[4] = 67
Key 10 is found at position 0.

Test Case - 2

User Output
n =
4
a[0] =
-24
a[1] =
-36
a[2] =
-10
a[3] =
-87
Search key =
-10
After sorting :
a[0] = -87
a[1] = -36
a[2] = -24
a[3] = -10
Key -10 is found at position 3.

Page No: 25
Test Case - 3

User Output

ID: 2200330100243
n =
5
a[0] =
2
a[1] =
3
a[2] =
4
a[3] =
1
a[4] =

2022-2026-CSE-D-D1
5
Search key =
9
After sorting :
a[0] = 1
a[1] = 2
a[2] = 3

Raj Kumar Goel Institute Of Technology


a[3] = 4
a[4] = 5
Key 9 is not found in the array.
Exp. Name: Write a C program to implement different
S.No: 9 Date: 2023-11-22
Operations on Stack using Arrays.

Aim:

Page No: 26
Write a C program to implement stack operations using arrays.
Source Code:

StackUsingArray.c

ID: 2200330100243
#include <stdio.h>
#include <stdlib.h>
#define STACK_MAX_SIZE 10
#include "StackOperations.c"

int main() {
int op, x;
while(1) {
printf("1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {

2022-2026-CSE-D-D1
case 1:
printf("Enter element : ");
scanf("%d", &x);
push(x);
break;
case 2:
pop();
break;
case 3:

Raj Kumar Goel Institute Of Technology


display();
break;
case 4:
isEmpty();
break;
case 5:
peek();
break;
case 6:
exit(0);
}
}
}

StackOperations.c
#define MAX 10
int stack[MAX];
int top = -1;
void push(int data)
{

Page No: 27
if(top == MAX - 1)
{
printf("Stack is overflow.\n");
return;
}

ID: 2200330100243
top++;
stack[top] = data;
printf("Successfully pushed.\n");
}
void pop()
{
if(top == -1)
{
printf("Stack is underflow.\n");
return;
}
int data = stack[top];

2022-2026-CSE-D-D1
printf("Popped value = %d\n", data);
top--;
}
void peek()//done
{
if (top == -1)
{
printf("Stack is underflow.\n");

Raj Kumar Goel Institute Of Technology


return;
}
int data = stack[top];
printf("Peek value = %d\n", data);
}
void isEmpty()//done
{
if (top == -1)
{
printf("Stack is empty.\n");
}
else
{
printf("Stack is not empty.\n");
}
}
void display()//done
{
if (top == -1)
{
printf("Stack is empty.\n");
return;
}
printf("Elements of the stack are : ");
for (int i = top; i >=0; i--)
{
printf("%d ", stack[i]);
}
printf("\n");
}

Page No: 28
Execution Results - All test cases have succeeded!
Test Case - 1

ID: 2200330100243
User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
10
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1

2022-2026-CSE-D-D1
Enter element :
20
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
30

Raj Kumar Goel Institute Of Technology


Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Elements of the stack are : 30 20 10
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5
Peek value = 30
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 30
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 20
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Elements of the stack are : 10
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5
Peek value = 10
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
4

Page No: 29
Stack is not empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2

ID: 2200330100243
Popped value = 10
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
4
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

2022-2026-CSE-D-D1
6

Test Case - 2

User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

Raj Kumar Goel Institute Of Technology


1
Enter element :
1
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
2
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
3
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
4
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
5
Successfully pushed.

Page No: 30
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
6

ID: 2200330100243
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
7
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1

2022-2026-CSE-D-D1
Enter element :
8
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
9

Raj Kumar Goel Institute Of Technology


Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
10
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
11
Stack is overflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6

Test Case - 3

User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5
Stack is underflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6

Page No: 31
ID: 2200330100243
2022-2026-CSE-D-D1
Raj Kumar Goel Institute Of Technology
Exp. Name: Write a C program to implement different
S.No: 10 Date: 2023-11-22
Operations on Queue using Array representation

Aim:

Page No: 32
Write a program to implement queue using arrays.
Source Code:

QueueUsingArray.c

ID: 2200330100243
#include <conio.h>
#include <stdio.h>
#include "QueueOperations.c"
int main() {
int op, x;
while(1) {
printf("1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {
case 1:
printf("Enter element : ");

2022-2026-CSE-D-D1
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;

Raj Kumar Goel Institute Of Technology


case 4:
isEmpty();
break;
case 5:
size();
break;
case 6: exit(0);
}
}
}

QueueOperations.c
#define MAX_SIZE 100
int queue[MAX_SIZE];
int front = -1, rear = -1;
void enqueue(int value)
{

Page No: 33
if (rear == MAX_SIZE - 1)
{
printf("Queue is overflow\n");
}
else

ID: 2200330100243
{
if (front == -1)
{
front = 0;
}
rear++;
queue[rear] = value;
printf("Successfully inserted.\n");
}
}
void dequeue()
{

2022-2026-CSE-D-D1
if (front == -1)
{
printf("Queue is underflow.\n");
}
else
{
printf("Deleted element = %d\n", queue[front]);
if (front == rear)

Raj Kumar Goel Institute Of Technology


{
front = -1;
rear = -1;
}
else
{
front++;
}
}
}
void display()
{
if (front == -1)
{
printf("Queue is empty.\n");
}
else
{
printf("Elements in the queue : ");
for (int i = front; i <= rear; i++)
{
printf("%d ", queue[i]);
}
printf("\n");
}
}
int isEmpty()
{
if (front == -1)
{
printf("Queue is empty.\n");
}

Page No: 34
else
{
printf("Queue is not empty.\n");
}
}

ID: 2200330100243
int size()
{
if (front == -1)
{
printf("Queue size : %d\n",0);
}
else
{
printf("Queue size : %d\n",(rear - front + 1));
}
}

2022-2026-CSE-D-D1
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

Raj Kumar Goel Institute Of Technology


Enter your option :
2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
14
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
78
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

Page No: 35
Enter your option :
1
Enter element :
53

ID: 2200330100243
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Elements in the queue : 14 78 53
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 3
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

2022-2026-CSE-D-D1
6

Test Case - 2

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

Raj Kumar Goel Institute Of Technology


1
Enter element :
25
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 25
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
65
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Elements in the queue : 65
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

Page No: 36
4
Queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2

ID: 2200330100243
Deleted element = 65
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

2022-2026-CSE-D-D1
Enter your option :
1
Enter element :
63
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5

Raj Kumar Goel Institute Of Technology


Queue size : 1
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
6
Exp. Name: Implementation of Circular Queue using
S.No: 11 Date: 2023-11-22
Arrays

Aim:

Page No: 37
Write a C program to implement circular queue using arrays.

Note: Define the MAX value as 5.


Source Code:

ID: 2200330100243
CQueueUsingArray.c
#include <stdio.h>
#include <stdlib.h>
#include "CQueueOperations.c"

int main() {
int op, x;
while(1) {
printf("1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);

2022-2026-CSE-D-D1
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;

Raj Kumar Goel Institute Of Technology


case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
size();
break;
case 6: exit(0);
}
}
}

CQueueOperations.c
#define MAX_SIZE 5
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
void enqueue(int item) {

Page No: 38
if ((rear + 1) % MAX_SIZE == front) {
printf("Circular queue is overflow.\n");
}
else {
if (front == -1) {

ID: 2200330100243
front = rear = 0;
}
else {
rear = (rear + 1) % MAX_SIZE;
}
queue[rear] = item;
printf("Successfully inserted.\n");
}
}

int dequeue1() {
int removedItem;

2022-2026-CSE-D-D1
if (front == -1) {
return -1;
// Return a special value indicating an error or an invalid value
}
else {
removedItem = queue[front];
if (front == rear) {
front = rear = -1;

Raj Kumar Goel Institute Of Technology


}
else {
front = (front + 1) % MAX_SIZE;
}
return removedItem;
}
}
void dequeue(){
int item = dequeue1();
if(item != -1){
printf("Deleted element = %d\n",item);
}
else{
printf("Circular queue is underflow.\n");
}
}

void display() {
if (front == -1) {
printf("Circular queue is empty.\n");
} else {
printf("Elements in the circular queue : ");
int i = front;
do {
printf("%d ", queue[i]);
i = (i + 1) % MAX_SIZE;
} while (i != (rear + 1) % MAX_SIZE);
printf("\n");
}
}
void isEmpty() {
if (front == -1){

Page No: 39
printf("Circular queue is empty.\n");
}
else{
printf("Circular queue is not empty.\n");
}

ID: 2200330100243
}
int size1() {
if (front == -1) {
return 0;
} else {
return (rear - front + MAX_SIZE) % MAX_SIZE+1;
}
}
void size(){
printf("Circular queue size : %d\n",size1());
}

2022-2026-CSE-D-D1
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

Raj Kumar Goel Institute Of Technology


Enter your option :
2
Circular queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
12
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
34
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
56

Page No: 40
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3

ID: 2200330100243
Elements in the circular queue : 12 34 56
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
38
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :

2022-2026-CSE-D-D1
25
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Elements in the circular queue : 12 34 56 38 25
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :

Raj Kumar Goel Institute Of Technology


1
Enter element :
56
Circular queue is overflow.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 12
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 34
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Elements in the circular queue : 56 38 25
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5
Circular queue size : 3
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :

Page No: 41
11
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3

ID: 2200330100243
Elements in the circular queue : 56 38 25 11
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
6

Test Case - 2

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :

2022-2026-CSE-D-D1
5
Circular queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
34

Raj Kumar Goel Institute Of Technology


Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
55
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
26
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
77
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
38
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1

Page No: 42
Enter element :
59
Circular queue is overflow.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

ID: 2200330100243
Enter your option :
3
Elements in the circular queue : 34 55 26 77 38
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5
Circular queue size : 5
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 34

2022-2026-CSE-D-D1
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 55
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted element = 26

Raj Kumar Goel Institute Of Technology


1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Elements in the circular queue : 77 38
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5
Circular queue size : 2
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
6
Exp. Name: Write a C program to implement different
S.No: 12 Date: 2023-11-22
Operations on Stack using Linked Lists

Aim:

Page No: 43
Write a program to implement stack using linked lists.

Source Code:

ID: 2200330100243
StackUsingLL.c
#include <stdio.h>
#include <stdlib.h>
#include "StackOperationsLL.c"

int main() {
int op, x;
while(1) {
printf("1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {

2022-2026-CSE-D-D1
case 1:
printf("Enter element : ");
scanf("%d", &x);
push(x);
break;
case 2:
pop();
break;

Raj Kumar Goel Institute Of Technology


case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
peek();
break;
case 6:
exit(0);
}
}
}

StackOperationsLL.c
// Node structure for a stack element
struct Node {
int data;
struct Node* next;
};

Page No: 44
typedef struct Node Node;
// Global variable for the top of the stack
Node* top = NULL;
// Function to push an element onto the stack
void push(int item) {

ID: 2200330100243
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Heap Memory Is Full Can't Allocate, Memory Allocation Is Failed\n");
return;
}
newNode->data = item;
newNode->next = top;
top = newNode;
printf("Successfully pushed.\n");
}
// Function to pop an element from the stack
void pop() {

2022-2026-CSE-D-D1
if (top == NULL) {
printf("Stack is underflow.\n");
return;
}
Node* temp = top;
top = top->next;
int poppedItem = temp->data;
free(temp);

Raj Kumar Goel Institute Of Technology


printf("Popped value = %d\n", poppedItem);
}
// Function to display the stack
void display() {
if (top == NULL) {
printf("Stack is empty.\n");
return;
}
printf("Elements of the stack are : ");
Node* current = top;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Function to check if the stack is empty
int isEmpty1() {
return top == NULL;
}
void isEmpty(){
if (isEmpty1()) {
printf("Stack is empty.\n");
}
else {
printf("Stack is not empty.\n");
}
}
// Function to peek at the top element of the stack
void peek() {
if (top == NULL) {
printf("Stack is underflow.\n");

Page No: 45
return;
}
printf("Peek value = %d\n", top->data);
}

ID: 2200330100243
Execution Results - All test cases have succeeded!
Test Case - 1

User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :

2022-2026-CSE-D-D1
33
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
22

Raj Kumar Goel Institute Of Technology


Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
55
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
66
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Elements of the stack are : 66 55 22 33
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 66
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 55
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3

Page No: 46
Elements of the stack are : 22 33
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5

ID: 2200330100243
Peek value = 22
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
4
Stack is not empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6

Test Case - 2

2022-2026-CSE-D-D1
User Output
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Stack is underflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

Raj Kumar Goel Institute Of Technology


3
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
5
Stack is underflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
4
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
23
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
1
Enter element :
24
Successfully pushed.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
3
Elements of the stack are : 24 23
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :

Page No: 47
5
Peek value = 24
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2

ID: 2200330100243
Popped value = 24
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Popped value = 23
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
2
Stack is underflow.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit

2022-2026-CSE-D-D1
Enter your option :
4
Stack is empty.
1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit
Enter your option :
6

Raj Kumar Goel Institute Of Technology


Exp. Name: Write a C program to implement different
S.No: 13 Date: 2023-11-22
Operations on Queue using Linked Lists

Aim:

Page No: 48
Write a program to implement queue using linked lists.

Sample Input and Output:


1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

ID: 2200330100243
Enter your option : 1
Enter element : 57
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 1
Enter element : 87
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 5
Queue size : 2
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 3

2022-2026-CSE-D-D1
Elements in the queue : 57 87
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 2
Deleted value = 57
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 2
Deleted value = 87
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 3

Raj Kumar Goel Institute Of Technology


Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 5
Queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option : 6

Source Code:

QueueUsingLL.c
#include <conio.h>
#include <stdio.h>
#include "QueueOperationsLL.c"
int main() {
int op, x;

Page No: 49
while(1) {
printf("1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {

ID: 2200330100243
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:

2022-2026-CSE-D-D1
isEmpty();
break;
case 5:
size();
break;
case 6: exit(0);
}
}

Raj Kumar Goel Institute Of Technology


}

QueueOperationsLL.c
//Node structure for a queue element
struct Node {
int data;
struct Node* next;
};

Page No: 50
typedef struct Node Node;
// Global variables for the front and rear of the queue
Node* front = NULL;
Node* rear = NULL;
// Function to enqueue an element into the queue

ID: 2200330100243
void enqueue(int item) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed. Cannot enqueue.\n");
return;
}
newNode->data = item;
newNode->next = NULL;
if (rear == NULL) {
// If the queue is empty
front = rear = newNode;
} else {

2022-2026-CSE-D-D1
rear->next = newNode;
rear = newNode;
}
printf("Successfully inserted.\n", item);
}
// Function to dequeue an element from the queue
void dequeue() {
if (front == NULL) {

Raj Kumar Goel Institute Of Technology


printf("Queue is underflow.\n");
return;
}
Node* temp = front;
int dequeuedItem = temp->data;
if (front == rear) {
// If there is only one element in the queue
front = rear = NULL;
} else {
front = front->next;
}
free(temp);
printf("Deleted value = %d\n", dequeuedItem);
}
// Function to display the queue
void display() {
if (front == NULL) {
printf("Queue is empty.\n");
return;
}
printf("Elements in the queue : ");
Node* current = front;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Function to check if the queue is empty
int is_empty() {
return front == NULL;
}

Page No: 51
// Function to get the size of the queue
int size1() {
int count = 0;
Node* current = front;
while (current != NULL) {

ID: 2200330100243
count++;
current = current->next;
}
return count;
}
void isEmpty(){
if (is_empty()) {
printf("Queue is empty.\n");
} else {
printf("Queue is not empty.\n");
}
}

2022-2026-CSE-D-D1
void size(){
printf("Queue size : %d\n", size1());
}

Execution Results - All test cases have succeeded!

Raj Kumar Goel Institute Of Technology


Test Case - 1

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4
Queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
44
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1

Page No: 52
Enter element :
55
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit

ID: 2200330100243
Enter your option :
1
Enter element :
66
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
67
Successfully inserted.

2022-2026-CSE-D-D1
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Elements in the queue : 44 55 66 67
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 44

Raj Kumar Goel Institute Of Technology


1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 55
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 2
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4
Queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
6

Test Case - 2

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
23
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :

Page No: 53
1
Enter element :
234
Successfully inserted.

ID: 2200330100243
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
45
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
1
Enter element :
456

2022-2026-CSE-D-D1
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 23
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3

Raj Kumar Goel Institute Of Technology


Elements in the queue : 234 45 456
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 234
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
3
Elements in the queue : 45 456
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
4
Queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
5
Queue size : 2
1.Enqueue 2.Dequeue 3.Display 4.Is Empty 5.Size 6.Exit
Enter your option :
6
Exp. Name: Implementation of Circular Queue using
S.No: 14 Date: 2023-11-22
Linked List

Aim:

Page No: 54
Write a program to implement circular queue using linked lists.

Sample Input and Output:


1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

ID: 2200330100243
Enter your option : 1
Enter element : 15
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 1
Enter element : 16
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 1
Enter element : 17
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

2022-2026-CSE-D-D1
Enter your option : 3
Elements in the circular queue : 15 16 17
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 5
Circular queue size : 3
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 2
Deleted value = 15

Raj Kumar Goel Institute Of Technology


1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 2
Deleted value = 16
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 2
Deleted value = 17
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 3
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 4
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 5
Circular queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option : 6

Source Code:

CQueueLL.c
#include <stdlib.h>
#include <stdio.h>
#include "CQueueOperationsLL.c"
int main() {
int op, x;

Page No: 55
while(1) {
printf("1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit\n");
printf("Enter your option : ");
scanf("%d",&op);
switch(op) {

ID: 2200330100243
case 1:
printf("Enter element : ");
scanf("%d",&x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:

2022-2026-CSE-D-D1
isEmpty();
break;
case 5:
size();
break;
case 6: exit(0);
}
}

Raj Kumar Goel Institute Of Technology


}

CQueueOperationsLL.c
// Node structure for a circular queue element
struct Node {
int data;
struct Node* next;
};

Page No: 56
typedef struct Node Node;
// Global variables for the front and rear of the circular queue
Node* front = NULL;
Node* rear = NULL;
// Function to enqueue an element into the circular queue

ID: 2200330100243
void enqueue(int item) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed. Cannot enqueue.\n");
return;
}
newNode->data = item;
newNode->next = NULL;
if (rear == NULL) {
// If the circular queue is empty
front = rear = newNode;
} else {

2022-2026-CSE-D-D1
rear->next = newNode;
rear = newNode;
}
// Make the circular connection
rear->next = front;
printf("Successfully inserted.\n");
}
// Function to dequeue an element from the circular queue

Raj Kumar Goel Institute Of Technology


void dequeue() {
if (front == NULL) {
printf("Circular queue is underflow.\n");
return;
}
Node* temp = front;
int dequeuedItem = temp->data;
if (front == rear) {
// If there is only one element in the circular queue
front = rear = NULL;
} else {
front = front->next;
rear->next = front; // Update rear's next to maintain the circular
connection
}
free(temp);
printf("Deleted value = %d\n", dequeuedItem);
}
// Function to display the circular queue
void display() {
if (front == NULL) {
printf("Circular queue is empty.\n");
return;
}
printf("Elements in the circular queue : ");
Node* current = front;
do {
printf("%d ", current->data);
current = current->next;
} while (current != front);
printf("\n");
}

Page No: 57
// Function to check if the circular queue is empty
int is_empty() {
return front == NULL;
}
// Function to get the size of the circular queue

ID: 2200330100243
int size_queue() {
if (front == NULL) {
return 0;
}
int count = 0;
Node* current = front;
do {
count++;
current = current->next;
} while (current != front);
return count;
}

2022-2026-CSE-D-D1
void isEmpty(){
if (is_empty()) {
printf("Circular queue is empty.\n");
} else {
printf("Circular queue is not empty.\n");
}
}
void size(){

Raj Kumar Goel Institute Of Technology


printf("Circular queue size : %d\n", size_queue());
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
15
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
16
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
17
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

Page No: 58
Enter your option :
3
Elements in the circular queue : 15 16 17
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

ID: 2200330100243
Enter your option :
5
Circular queue size : 3
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 15
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 16

2022-2026-CSE-D-D1
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 17
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Circular queue is empty.

Raj Kumar Goel Institute Of Technology


1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5
Circular queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
6

Test Case - 2

User Output
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Circular queue is underflow.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5
Circular queue size : 0
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is empty.

Page No: 59
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Circular queue is empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

ID: 2200330100243
Enter your option :
1
Enter element :
143
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
153

2022-2026-CSE-D-D1
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
1
Enter element :
163
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit

Raj Kumar Goel Institute Of Technology


Enter your option :
1
Enter element :
173
Successfully inserted.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
3
Elements in the circular queue : 143 153 163 173
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 143
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
2
Deleted value = 153
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
5
Circular queue size : 2
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
4
Circular queue is not empty.
1.Enqueue 2.Dequeue 3.Display 4.Is empty 5.Size 6.Exit
Enter your option :
6

Page No: 60
ID: 2200330100243
2022-2026-CSE-D-D1
Raj Kumar Goel Institute Of Technology

You might also like