Document
Document
e. Exit
ALGORITHM:-
6. Current Status : Displays the status of the queue including front and rear
pointers.
SOURCE CODE:-
#include <stdio.h>
#include <stdlib.h>
#define max 3
typedef struct {
char queue[max];
int front;
int rear;
} circularq
cq->front = -1;
cq->rear = -1;
return (cq->front =
if (isfull(cq)) {
return;
if (isempty(cq)) {
cq->front = 0;
cq->queue[cq->rear] = element;
if (isempty(cq)) {
return ‘\0’;
}
if (cq->front == cq->rear) {
} else {
return element
if (isempty(cq)) {
printf(“queue is empty\n”);
return;
int i = cq->front;
while (1) {
printf(“%c “, cq->queue[i]);
if (i == cq->rear) break;
i = (i + 1) % max;
printf(“\n”);
printf(“queue status:\n”);
printf(“front: %d\n”, cq->front);
displayqueue(cq);
int main() {
circularqueue cq;
initializequeue(&cq);
int choice;
char element;
while (1) {
printf(“1. enqueue\n”);
printf(“2. dequeue\n”);
printf(“3. display\n”);
printf(“5. exit\n”);
scanf(“%d”, &choice);
switch (choice) {
case 1:
enqueue(&cq, element);
break;
case 2:
dequeue(&cq);
break;
case 3:
displayqueue(&cq);
break;
case 4:
currentstatus(&cq);
break;
case 5:
exit(0);
default:
return 0;
}
OUTPUT:-
PROBLEM STATEMENT:-21 Develop a C program that implements a linear search
algorithm to find the position of a given element in an array.
Test case 1:
LINEAR SEARCH
Enter 1 no-:25
Enter 2 no-:26
Enter 3 no-:27
ALGORITHM:-
1. Start
6. Iterate through the array from the first element to the last:
7. If the target element is not found after the iteration, return `-1`.
8. End
SOURCE CODE:-
#include <stdio.h>
int main() {
printf(“linear search\n”);
scanf(“%d”, &n);
int arr[n];
scanf(“%d”, &arr[i]);
scanf(“%d”, &target);
if (position != -1) {
printf(“the no %d is present in array at position %d\n”, target, position);
} else {
return 0;
OUTPUT:-
PROBLEM STATEMENT :-22 Develop a C program that implements a Binary
search algorithm to find the position of a given element in an array.
Test case 1:
BINARY SEARCH
Enter 1 no-:35
Enter 2 no-:36
Enter 3 no-:37
ALGORITHM:-
1. Input:
- Pass the array `arr`, its size `n`, and the target element `target` to the
`binarySearch` function
3. Output:
- If the result is not `-1`, print the position of the target element
- If the result is `-1`, print that the target element is not present in the array
SOURCE CODE:-
#include <stdio.h>
int left = 0;
if (arr[mid] == target)
left = mid + 1;
else
right = mid – 1;
return -1;
int main() {
printf(“binary search\n”);
scanf(“%d”, &n);
int arr[n];
scanf(“%d”, &arr[i]);
}
printf(“enter the no you want to do binary search on-: “);
scanf(“%d”, &target)
if (result != -1)
else
return 0;
OUTPUT:-
PROBLEM STATEMENT 23:-Develop a C program that implements a Selection
sorting algorithm to sort the elements in an array.
Test case 1:
SELECTION SORTING
Enter 1 no-:5
Enter 2 no-:3
Enter 3 no-:4
Enter 4 no-:1
Enter 5 no-:2
ALGORITHM:-
1. Input:
2. Process:
3. Output:
SOURCE CODE:-
#include <stdio.h>
// function to perform selection sort
minindex = i;
minindex = j;
temp = arr[minindex];
arr[minindex] = arr[i];
arr[i] = temp;
int main() {
int n;
printf(“selection sorting\n”);
scanf(“%d”, &n);
int arr[n];
scanf(“%d”, &arr[i]);
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
printf(“\n”);
selectionsort(arr, n);
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
printf(“\n”);
return 0;
OUTPUT:-
PROBLEM STATEMENT:-Develop a C program that implements a Bubble sorting
algorithm to sort the elements in an array
Enter 1 no-:55
Enter 2 no-:33
Enter 3 no-:44
Enter 4 no-:11
Enter 5 no-:22
ALGORITHM:-
SOURCE CODE:-
#include <stdio.h>
int i, j, temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
int main() {
int n;
printf(“bubble sorting\n”);
scanf(“%d”, &n);
int arr[n];
scanf(“%d”, &arr[i]);
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
printf(“\n”)
bubblesort(arr, n);
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
printf(“\n”);
return 0;
OUTPUT:-
PROBLEM STATEMENT:-25 Develop a C program that implements a Insertion
sorting algorithm to sort the elements in an array.
Test case 1:
INSERTION SORTING
Enter 1 no-:22
Enter 2 no-:23
Enter 3 no-:20
Enter 4 no-: 21
Enter 5 no-:24
ALGORITHM:-
1. Input:
2. Process:
3. Output:
SOURCE CODE:-
#include <stdio.h>
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i – 1;
arr[j + 1] = arr[j];
j = j – 1;
arr[j + 1] = key;
int main() {
int n;
printf(“insertion sorting\n”);
scanf(“%d”, &n);
int arr[n];
scanf(“%d”, &arr[i]);
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
}
}
printf(“\n”);
insertionsort(arr, n);
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
printf(“\n”);
return 0;
OUTPUT:-