Code Tantra Dsa Aktu
Code Tantra Dsa Aktu
Code Tantra Dsa Aktu
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]);
}
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
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]);
}
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
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
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
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]);
}
2022-2026-CSE-D-D1
printf("a[%d] = %d\n",i,arr[i]);
}
return 0;
}
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
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 :
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]);
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
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
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:
ID: 2200330100243
For example, if the user gives the input as:
Next, the program should print the following message on the console as:
Enter 5 elements :
Enter 5 elements : 34 67 12 45 22
2022-2026-CSE-D-D1
then the program should print the result as:
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++;
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 :
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:
Next, the program should print the following message on the console as:
Enter 5 elements :
2022-2026-CSE-D-D1
Enter 5 elements : 34 67 12 45 22
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);
}
}
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
2022-2026-CSE-D-D1
scanf("%d",&arr[i]);
}
// searching request.
printf("Search key : ");
scanf("%d",&key);
for (int i = 0; i < n; 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;
}
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
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 = ");
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
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
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:
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");
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
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 :
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
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;
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)
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
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 :
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
Aim:
Page No: 37
Write a C program to implement circular queue using arrays.
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;
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;
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
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 :
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
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
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;
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);
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
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 :
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
Aim:
Page No: 48
Write a program to implement queue using linked lists.
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
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);
}
}
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) {
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());
}
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
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
Aim:
Page No: 54
Write a program to implement circular queue using linked lists.
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
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);
}
}
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
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(){
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.
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
Page No: 60
ID: 2200330100243
2022-2026-CSE-D-D1
Raj Kumar Goel Institute Of Technology