New Program
New Program
CODE-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
struct Stack {
};
stack->top = NULL;
return stack;
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
if (stack->top == NULL) {
printf("Stack is empty!\n");
return -1;
stack->top = stack->top->next;
free(temp);
return poppedValue;
if (current == NULL) {
printf("Stack is empty!\n");
return;
current = current->next;
printf("\n");
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(stack, value);
break;
case 2:
value = pop(stack);
if (value != -1) {
break;
case 3:
display(stack);
break;
case 4:
free(stack);
exit(0);
default:
return 0; }
OUTPUT-
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
Stack elements: 24
1. Push
2. Pop
3. Display
4. Exit
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;
};
q->front = -1;
q->rear = -1;
if (q->rear == MAX - 1)
return 1;
return 0;
if (q->front == -1)
return 1;
return 0;
if (isFull(q)) {
} else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
int item;
if (isEmpty(q)) {
return -1;
} else {
item = q->items[q->front];
if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
} else {
q->front++;
return item;
if (isEmpty(q)) {
printf("Queue is empty.\n");
} else {
printf("\n");
}
}
int main() {
struct Queue q;
initQueue(&q);
do {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting...\n");
break;
default:
}
}
return 0;
OUTPUT-
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enqueued 24
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Queue elements: 24
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Exiting...
PROGRAM - 08
OBJECTIVE- To search an element in array using binary search.
CODE-
#include <stdio.h>
if (arr[mid] == target) {
} else {
int main() {
int size;
scanf("%d", &size);
int arr[size];
scanf("%d", &arr[i]);
int target;
scanf("%d", &target);
else {
return 0;
OUTPUT-
25
42
55
67
68
69
70
75
88
89
PROGRAM-09
OBJECTIVE- To sort integer number in decreasing order using selection sort.
CODE-
#include <stdio.h>
int maxIndex = i;
maxIndex = j;
if (maxIndex != i) {
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
int main() {
int size;
scanf("%d", &size);
int arr[size];
scanf("%d", &arr[i]);
selectionSort(arr, size);
printf("\n");
return 0;
OUTPUT-
Enter 4 elements:
34
25
66
PROGRAM-10
OBECTIVE- To sort integer number in decreasing order using insertion sort.
CODE-
#include <stdio.h>
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int main() {
int size;
scanf("%d", &size);
int arr[size];
scanf("%d", &arr[i]);
insertionSort(arr, size);
printf("\n");
return 0;
OUTPUT-
Enter 4 elements:
77
82
10
964