PROGRAM -06
OBJECTIVE- Program using stack operation in linked list.
CODE-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Stack {
struct Node* top;
};
struct Stack* createStack() {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = NULL;
return stack;
void push(struct Stack* stack, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
int pop(struct Stack* stack) {
if (stack->top == NULL) {
printf("Stack is empty!\n");
return -1;
struct Node* temp = stack->top;
int poppedValue = temp->data;
stack->top = stack->top->next;
free(temp);
return poppedValue;
void display(struct Stack* stack) {
struct Node* current = stack->top;
if (current == NULL) {
printf("Stack is empty!\n");
return;
printf("Stack elements: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
printf("\n");
int main() {
struct Stack* stack = createStack();
int choice, value;
while (1) {
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(stack, value);
break;
case 2:
value = pop(stack);
if (value != -1) {
printf("Popped value: %d\n", value);
break;
case 3:
display(stack);
break;
case 4:
free(stack);
exit(0);
default:
printf("Invalid choice! Please try again.\n");}}
return 0; }
OUTPUT-
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 24
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements: 24
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4
PROGRAM -07
OBJECTIVE- To implement operations in a queue using array.
CODE-
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
struct Queue {
int items[MAX];
int front;
int rear;
};
void initQueue(struct Queue *q) {
q->front = -1;
q->rear = -1;
int isFull(struct Queue *q) {
if (q->rear == MAX - 1)
return 1;
return 0;
int isEmpty(struct Queue *q) {
if (q->front == -1)
return 1;
return 0;
void enqueue(struct Queue *q, int value) {
if (isFull(q)) {
printf("Queue is full. Cannot enqueue %d\n", value);
} else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
printf("Enqueued %d\n", value);
int dequeue(struct Queue *q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
} else {
item = q->items[q->front];
if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
} else {
q->front++;
printf("Dequeued %d\n", item);
return item;
void display(struct Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
} else {
printf("Queue elements: ");
for (int i = q->front; i <= q->rear; i++) {
printf("%d ", q->items[i]);
printf("\n");
}
}
int main() {
struct Queue q;
initQueue(&q);
int choice, value;
do {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
}
while (choice != 4);
return 0;
OUTPUT-
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 24
Enqueued 24
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue elements: 24
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...
PROGRAM - 08
OBJECTIVE- To search an element in array using binary search.
CODE-
#include <stdio.h>
int binarySearch(int arr[], int size, int target) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid; // Target found
if (arr[mid] < target) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
return -1; // Target not found
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array in sorted order:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
int target;
printf("Enter the element to search: ");
scanf("%d", &target);
int result = binarySearch(arr, size, target);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
else {
printf("Element %d not found in the array.\n", target);
return 0;
OUTPUT-
Enter the size of the array: 12
Enter 4 elements of the array in sorted order:
25
42
55
67
68
69
70
75
88
89
Enter the element to search: 25
Element 45 found at index 2.
PROGRAM-09
OBJECTIVE- To sort integer number in decreasing order using selection sort.
CODE-
#include <stdio.h>
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
// Swap the found maximum element with the first element
if (maxIndex != i) {
int temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
selectionSort(arr, size);
printf("Array sorted in decreasing order: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
return 0;
OUTPUT-
Enter the size of the array: 4
Enter 4 elements:
34
25
66
Array sorted in decreasing order:66 ,34,25,1
PROGRAM-10
OBECTIVE- To sort integer number in decreasing order using insertion sort.
CODE-
#include <stdio.h>
void insertionSort(int arr[], int size) {
for (int i = 1; i < size; i++) {
int key = arr[i];
int j = i - 1;
// Move elements of arr[0..i-1] that are less than key
// to one position ahead of their current position
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
insertionSort(arr, size);
printf("Array sorted in decreasing order: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
return 0;
OUTPUT-
Enter the size of the array: 4
Enter 4 elements:
77
82
10
964
Array sorted in decreasing order: 964 82 77 10